GitLabgitlab-org
cli
main
1package events
3import (
4 "fmt"
6 "github.com/MakeNowJust/heredoc/v2"
7 "github.com/spf13/cobra"
9 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
11 "gitlab.com/gitlab-org/cli/internal/api"
12 "gitlab.com/gitlab-org/cli/internal/cmdutils"
13 "gitlab.com/gitlab-org/cli/internal/iostreams"
14 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
15 "gitlab.com/gitlab-org/cli/internal/utils"
16)
18func NewCmdEvents(f cmdutils.Factory) *cobra.Command {
19 cmd := &cobra.Command{
20 Use: "events",
21 Short: "View user events.",
22 Long: heredoc.Docf(`
23 By default, lists events for the current project. Use %[1]s--all%[1]s to
24 include events from every project you can access.
25 `, "`"),
26 Example: heredoc.Doc(`
27 glab user events
28 glab user events --all
29 glab user events -F json`),
30 Args: cobra.NoArgs,
31 ValidArgsFunction: cobra.NoFileCompletions,
32 Annotations: map[string]string{
33 mcpannotations.Safe: "true",
34 },
35 RunE: func(cmd *cobra.Command, args []string) error {
36 c, err := f.ApiClient("")
37 if err != nil {
38 return err
39 }
40 client := c.Lab()
42 repo, err := f.BaseRepo()
43 if err != nil {
44 return err
45 }
47 l := &gitlab.ListContributionEventsOptions{}
49 if p, _ := cmd.Flags().GetInt("page"); p != 0 {
50 l.Page = int64(p)
51 }
52 if p, _ := cmd.Flags().GetInt("per-page"); p != 0 {
53 l.PerPage = int64(p)
54 }
56 if l.PerPage == 0 {
57 l.PerPage = api.DefaultListLimit
58 }
60 events, resp, err := client.Events.ListCurrentUserContributionEvents(l)
61 if err != nil {
62 return err
63 }
65 if err := f.IO().StartPager(); err != nil {
66 return err
67 }
68 defer f.IO().StopPager()
70 outputFormat, err := cmd.Flags().GetString("output")
71 if err != nil {
72 return nil
73 }
75 if outputFormat != "json" && outputFormat != "text" {
76 return fmt.Errorf("--output must be either 'json' or 'text'. Received: %s", outputFormat)
77 }
79 if outputFormat == "json" {
80 return f.IO().PrintJSON(&events)
81 }
83 if lb, _ := cmd.Flags().GetBool("all"); lb {
84 projects := make(map[int64]*gitlab.Project)
85 for _, e := range events {
86 project, err := api.GetProject(client, e.ProjectID)
87 if err != nil {
88 return err
89 }
90 projects[e.ProjectID] = project
91 }
93 title := utils.NewListTitle("user event")
94 title.Page = int(l.Page)
95 title.CurrentPageTotal = len(events)
96 if resp != nil {
97 title.Total = int(resp.TotalItems)
98 }
99 title.RepoName = "all projects"
101 f.IO().LogInfof("%s\n", title.Describe())
102 DisplayAllEvents(f.IO(), events, projects)
103 return nil
104 }
106 project, err := api.GetProject(client, repo.FullName())
107 if err != nil {
108 return err
109 }
111 DisplayProjectEvents(f.IO(), events, project)
112 return nil
113 },
114 }
116 cmd.Flags().BoolP("all", "a", false, "Get events from all projects.")
117 cmd.Flags().IntP("page", "p", 1, "Page number.")
118 cmd.Flags().IntP("per-page", "P", 30, "Number of items to list per page.")
119 cmd.Flags().StringP("output", "F", "text", "Format output as: 'text', 'json'.")
120 cmdutils.AddJQFlag(cmd, f.IO())
121 return cmd
122}
124func DisplayProjectEvents(io *iostreams.IOStreams, events []*gitlab.ContributionEvent, project *gitlab.Project) {
125 for _, e := range events {
126 if e.ProjectID != project.ID {
127 continue
128 }
129 printEvent(io, e, project)
130 }
131}
133func DisplayAllEvents(io *iostreams.IOStreams, events []*gitlab.ContributionEvent, projects map[int64]*gitlab.Project) {
134 for _, e := range events {
135 printEvent(io, e, projects[e.ProjectID])
136 }
137}
139func printEvent(io *iostreams.IOStreams, e *gitlab.ContributionEvent, project *gitlab.Project) {
140 switch e.ActionName {
141 case "pushed to":
142 io.LogInfof("Pushed to %s %s at %s\n%q.\n", e.PushData.RefType, e.PushData.Ref, project.NameWithNamespace, e.PushData.CommitTitle)
143 case "deleted":
144 io.LogInfof("Deleted %s %s at %s.\n", e.PushData.RefType, e.PushData.Ref, project.NameWithNamespace)
145 case "pushed new":
146 io.LogInfof("Pushed new %s %s at %s.\n", e.PushData.RefType, e.PushData.Ref, project.NameWithNamespace)
147 case "commented on":
148 io.LogInfof("Commented on %s #%s at %s.\n%q\n", e.Note.NoteableType, e.Note.Title, project.NameWithNamespace, e.Note.Body)
149 case "accepted":
150 io.LogInfof("Accepted %s %s at %s.\n", e.TargetType, e.TargetTitle, project.NameWithNamespace)
151 case "opened":
152 io.LogInfof("Opened %s %s at %s.\n", e.TargetType, e.TargetTitle, project.NameWithNamespace)
153 case "closed":
154 io.LogInfof("Closed %s %s at %s.\n", e.TargetType, e.TargetTitle, project.NameWithNamespace)
155 case "joined":
156 io.LogInfof("Joined %s.\n", project.NameWithNamespace)
157 case "left":
158 io.LogInfof("Left %s.\n", project.NameWithNamespace)
159 case "created":
160 targetType := e.TargetType
161 if e.TargetType == "WikiPage::Meta" {
162 targetType = "Wiki page"
163 }
164 io.LogInfof("Created %s %s at %s.\n", targetType, e.TargetTitle, project.NameWithNamespace)
165 default:
166 io.LogInfof("%s %q", e.TargetType, e.Title)
167 }
168 io.LogInfo() // to leave a blank line
169}
Diagnostics8 git calls · 239 ms
CallStatusSizeTime0239 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:02001.8 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=1857 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=dce174ff8c632a19d17eea99c8a9b7c37fb2b592 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1857 git.pkt.payload.max=1830 git.sideband.band1.bytes=1830 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=1830 git.pack.trailer=fb03183b7909876778f740d196d7516811003517Open pack map
fetchtree:0200170 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=170 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=e0b77b67bfa307fb89d197a89af0612fbb41cb0a git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=170 git.pkt.payload.max=143 git.sideband.band1.bytes=143 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=143 git.pack.trailer=9abc249900c5f5d57041c7816cdf5569e50efaa8Open pack map
fetchtree:0200138 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=223 git.response.bytes=138 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=dcb284da05682484fb21bf2b09985af03ce533d2 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=138 git.pkt.payload.max=111 git.sideband.band1.bytes=111 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=111 git.pack.trailer=7a4d0da4e4ee908018ca5c45049f5e5b636e126dOpen pack map
fetch2001.6 KiB223 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=1673 git.response.cache=MISS git.response.duration_ms=223 git.fetch.wants=5482e9193fc3d98b79dd520ce9cc2d91b2c05a4d git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1673 git.pkt.payload.max=1646 git.sideband.band1.bytes=1646 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e2e9f459ec4bc2
- Request
- 04060420…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format