GitLabgitlab-org
cli
main
1package whatsnew
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "strings"
10 "github.com/MakeNowJust/heredoc/v2"
11 "github.com/hashicorp/go-version"
12 "github.com/spf13/cobra"
14 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
16 "gitlab.com/gitlab-org/cli/internal/api"
17 "gitlab.com/gitlab-org/cli/internal/cmdutils"
18 "gitlab.com/gitlab-org/cli/internal/commands/update"
19 "gitlab.com/gitlab-org/cli/internal/config"
20 "gitlab.com/gitlab-org/cli/internal/iostreams"
21 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
22 "gitlab.com/gitlab-org/cli/internal/utils"
23)
25const (
26 projectPath = "gitlab-org/cli"
27 maxReleases = 10
28 // Separate from update.LastSeenVersionKey on purpose: the banner needs
29 // `marker >= current` (to stop repeating), the default whatsnew view
30 // needs `marker < current` (to still include the just-installed version).
31 // Sharing one marker produces TestWhatsnew_bannerNudgeThenWhatsnew.
32 LastWhatsnewVersionKey = "last_whatsnew_version"
33)
35var clientCreator = update.CreateUnauthenticatedClient
37type options struct {
38 tagName string
39 sinceVersion string
40 showLatest bool
41 isDefault bool
43 io *iostreams.IOStreams
44 cfg func() config.Config
45 buildInfo api.BuildInfo
46}
48func NewCmd(f cmdutils.Factory) *cobra.Command {
49 opts := &options{
50 io: f.IO(),
51 cfg: f.Config,
52 buildInfo: f.BuildInfo(),
53 }
55 cmd := &cobra.Command{
56 Use: "whatsnew [version]",
57 Short: "Show release notes for new versions of glab.",
58 Long: heredoc.Doc(`
59 With no arguments, shows release notes for every glab release
60 published since the last time you ran 'whatsnew' or saw the
61 post-upgrade banner — capped at the most recent 10 releases.
63 Pass a version argument to view notes for a specific release,
64 or use --since to set an explicit baseline.
65 `),
66 Example: heredoc.Doc(`
67 # Show release notes for every release since you last looked
68 glab whatsnew
70 # Show notes for the latest published release
71 glab whatsnew --latest
73 # Show notes for a specific version
74 glab whatsnew v1.85.0
76 # Show notes for every release published after a given version
77 glab whatsnew --since v1.80.0
78 `),
79 Args: cobra.MaximumNArgs(1),
80 Annotations: map[string]string{
81 mcpannotations.Exclude: "true",
82 },
83 RunE: func(cmd *cobra.Command, args []string) error {
84 opts.complete(cmd, args)
85 if err := opts.validate(); err != nil {
86 return err
87 }
88 return opts.run(cmd.Context())
89 },
90 }
92 fl := cmd.Flags()
93 fl.StringVar(&opts.sinceVersion, "since", "", "Show release notes for every release newer than this version.")
94 fl.BoolVar(&opts.showLatest, "latest", false, "Show release notes for the latest published release only.")
95 cmd.MarkFlagsMutuallyExclusive("since", "latest")
97 return cmd
98}
100func (o *options) complete(cmd *cobra.Command, args []string) {
101 if len(args) == 1 {
102 o.tagName = args[0]
103 }
104 fl := cmd.Flags()
105 o.isDefault = o.tagName == "" && !fl.Changed("since") && !fl.Changed("latest")
106}
108func (o *options) validate() error {
109 // cobra's MarkFlagsMutuallyExclusive can't express conflicts between
110 // flags and a positional argument, so we cover those here.
111 if o.tagName != "" && (o.sinceVersion != "" || o.showLatest) {
112 return cmdutils.WrapError(errors.New("flag conflict"), "cannot combine a version argument with --since or --latest.")
113 }
114 return nil
115}
117func (o *options) run(ctx context.Context) error {
118 client, err := clientCreator(o.buildInfo.UserAgent())
119 if err != nil {
120 return err
121 }
122 releases, err := o.fetchReleases(ctx, client.Lab())
123 if err != nil {
124 return err
125 }
126 if len(releases) == 0 {
127 o.io.LogError("No new releases since you last checked.")
128 return nil
129 }
131 if err := o.renderReleases(releases); err != nil {
132 return err
133 }
135 // Explicit views (--latest, version arg) shouldn't dismiss releases
136 // the user hasn't actually read, so only the default advances.
137 if o.isDefault {
138 _ = setLastWhatsnewVersion(o.cfg(), strings.TrimSpace(o.buildInfo.Version))
139 }
140 return nil
141}
143func (o *options) fetchReleases(ctx context.Context, client *gitlab.Client) ([]*gitlab.Release, error) {
144 switch {
145 case o.tagName != "":
146 r, resp, err := client.Releases.GetRelease(projectPath, normalizeTag(o.tagName), gitlab.WithContext(ctx))
147 if err != nil {
148 if resp != nil && resp.StatusCode == http.StatusNotFound {
149 return nil, cmdutils.WrapError(err, fmt.Sprintf("release %q not found.", o.tagName))
150 }
151 return nil, cmdutils.WrapError(err, "failed to fetch release.")
152 }
153 return []*gitlab.Release{r}, nil
155 case o.showLatest:
156 releases, _, err := client.Releases.ListReleases(projectPath, &gitlab.ListReleasesOptions{
157 ListOptions: gitlab.ListOptions{Page: 1, PerPage: 1},
158 }, gitlab.WithContext(ctx))
159 if err != nil {
160 return nil, cmdutils.WrapError(err, "failed to fetch latest release.")
161 }
162 return releases, nil
164 default:
165 sinceStr := strings.TrimSpace(o.sinceVersion)
166 if sinceStr == "" {
167 seen, _ := o.cfg().Get("", LastWhatsnewVersionKey)
168 sinceStr = strings.TrimSpace(seen)
169 }
170 releases, _, err := client.Releases.ListReleases(projectPath, &gitlab.ListReleasesOptions{
171 ListOptions: gitlab.ListOptions{Page: 1, PerPage: int64(maxReleases)},
172 }, gitlab.WithContext(ctx))
173 if err != nil {
174 return nil, cmdutils.WrapError(err, "failed to fetch releases.")
175 }
176 return filterNewer(releases, sinceStr), nil
177 }
178}
180func (o *options) renderReleases(releases []*gitlab.Release) error {
181 cfg := o.cfg()
182 glamourStyle, _ := cfg.Get("", "glamour_style")
183 o.io.ResolveBackgroundColor(glamourStyle)
185 if err := o.io.StartPager(); err != nil {
186 return err
187 }
188 defer o.io.StopPager()
190 c := o.io.Color()
191 for i, r := range releases {
192 if i > 0 {
193 o.io.LogInfo()
194 }
195 o.io.LogInfo(c.Bold(fmt.Sprintf("## %s", r.TagName)))
196 body := strings.TrimSpace(r.Description)
197 if body == "" {
198 o.io.LogInfo("(no release notes)")
199 continue
200 }
201 rendered, err := utils.RenderMarkdown(body, o.io.BackgroundColor())
202 if err != nil {
203 o.io.LogInfo(body)
204 continue
205 }
206 o.io.LogInfof("%s", rendered)
207 }
208 return nil
209}
211func filterNewer(releases []*gitlab.Release, sinceStr string) []*gitlab.Release {
212 since, err := version.NewVersion(sinceStr)
213 if err != nil {
214 return releases
215 }
216 out := make([]*gitlab.Release, 0, len(releases))
217 for _, r := range releases {
218 v, err := version.NewVersion(r.TagName)
219 if err != nil {
220 continue
221 }
222 if v.GreaterThan(since) {
223 out = append(out, r)
224 }
225 }
226 return out
227}
229func normalizeTag(v string) string {
230 v = strings.TrimSpace(v)
231 if v == "" {
232 return v
233 }
234 if !strings.HasPrefix(v, "v") {
235 return "v" + v
236 }
237 return v
238}
240func setLastWhatsnewVersion(cfg config.Config, v string) error {
241 if err := cfg.Set("", LastWhatsnewVersionKey, v); err != nil {
242 return err
243 }
244 return cfg.Write()
245}
Diagnostics7 git calls · 34 ms
CallStatusSizeTime034 ms
discover200185 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs 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 KiB · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=HIT git.response.duration_ms=3 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=2 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=3 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 · hit4 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=1857 git.response.cache=HIT git.response.duration_ms=4 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:0200140 B · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=140 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=6223101ab66ee56ea7d3bc317e170fac70525d6e 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=140 git.pkt.payload.max=113 git.sideband.band1.bytes=113 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=113 git.pack.trailer=f7b59eebea99044ec3c93984706fcd64a57f940aOpen pack map
fetch2002.4 KiB · hit22 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=22 git.response.bytes=2467 git.response.cache=HIT git.response.duration_ms=22 git.fetch.wants=a62a9a3b496d620dfd0cc4511efb2b0342d8ab00 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=2467 git.pkt.payload.max=2440 git.sideband.band1.bytes=2440 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
- a5e3248ebb9798f1
- Request
- 5da7ed05…
- 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