GitLabgitlab-org
cli
main
1package generate
3import (
4 "errors"
5 "fmt"
6 "time"
8 "github.com/MakeNowJust/heredoc/v2"
9 "github.com/spf13/cobra"
11 gitlab "gitlab.com/gitlab-org/api/client-go/v3"
13 "gitlab.com/gitlab-org/cli/internal/cmdutils"
14 "gitlab.com/gitlab-org/cli/internal/git"
15 "gitlab.com/gitlab-org/cli/internal/mcpannotations"
16)
18func NewCmdGenerate(f cmdutils.Factory) *cobra.Command {
19 changelogGenerateCmd := &cobra.Command{
20 Use: "generate [flags]",
21 Short: `Generate a changelog for the current project.`,
22 Long: heredoc.Docf(`
23 Generates a changelog from the commits in your project's Git
24 repository. If you do not pass %[1]s--version%[1]s, glab determines
25 the version by running %[1]sgit describe%[1]s against your local tags.
27 By default, GitLab reads the changelog configuration from
28 %[1]s.gitlab/changelog_config.yml%[1]s in the project. To use a
29 different file, pass %[1]s--config-file%[1]s.
31 To limit the range of commits, use %[1]s--from%[1]s and %[1]s--to%[1]s.
32 glab excludes the %[1]s--from%[1]s commit from the range and includes
33 the %[1]s--to%[1]s commit. The %[1]s--to%[1]s commit defaults to
34 %[1]sHEAD%[1]s of the project's default branch.
35 `, "`"),
36 Example: heredoc.Doc(`
37 # Generate a changelog for the version detected by 'git describe'
38 glab changelog generate
40 # Generate a changelog for a specific version
41 glab changelog generate --version 1.2.0
43 # Generate a changelog for commits between two SHAs
44 glab changelog generate --from abc123 --to def456
45 `),
46 Args: cobra.NoArgs,
47 ValidArgsFunction: cobra.NoFileCompletions,
48 Annotations: map[string]string{
49 mcpannotations.Destructive: "true",
50 },
51 RunE: func(cmd *cobra.Command, args []string) error {
52 client, err := f.GitLabClient()
53 if err != nil {
54 return err
55 }
57 repo, err := f.BaseRepo()
58 if err != nil {
59 return err
60 }
62 opts := gitlab.GenerateChangelogDataOptions{}
64 // Set the version
65 if s, _ := cmd.Flags().GetString("version"); s != "" {
66 opts.Version = new(s)
67 } else {
68 tags, err := git.ListTags()
69 if err != nil {
70 return err
71 }
73 if len(tags) == 0 {
74 return errors.New("no tags found; either fetch tags, or pass a version with --version instead")
75 }
77 version, err := git.DescribeByTags()
78 if err != nil {
79 return fmt.Errorf("failed to determine version from `git describe`: %w", err)
80 }
81 opts.Version = new(version)
82 }
84 // Set the config file
85 if s, _ := cmd.Flags().GetString("config-file"); s != "" {
86 opts.ConfigFile = new(s)
87 }
89 // Set the date
90 if s, _ := cmd.Flags().GetString("date"); s != "" {
91 parsedDate, err := time.Parse(time.RFC3339, s)
92 if err != nil {
93 return err
94 }
96 t := gitlab.ISOTime(parsedDate)
97 opts.Date = new(t)
98 }
100 // Set the "from" attribute
101 if s, _ := cmd.Flags().GetString("from"); s != "" {
102 opts.From = new(s)
103 }
105 // Set the "to" attribute
106 if s, _ := cmd.Flags().GetString("to"); s != "" {
107 opts.To = new(s)
108 }
110 // Set the trailer
111 if s, _ := cmd.Flags().GetString("trailer"); s != "" {
112 opts.Trailer = new(s)
113 }
115 changelog, _, err := client.Repositories.GenerateChangelogData(repo.FullName(), opts)
116 if err != nil {
117 return err
118 }
120 f.IO().LogInfof("%s", changelog.Notes)
122 return nil
123 },
124 }
126 // The options mimic the ones from the REST API.
127 // https://docs.gitlab.com/api/repositories/#generate-changelog-data
128 changelogGenerateCmd.Flags().StringP("version", "v", "", "Version to generate the changelog for. Must follow semantic versioning. Defaults to the version detected by 'git describe'.")
129 changelogGenerateCmd.Flags().StringP("config-file", "", "", "Path to the changelog configuration file in the project's Git repository. Defaults to '.gitlab/changelog_config.yml'.")
130 changelogGenerateCmd.Flags().StringP("date", "", "", "Date and time of the release, in ISO 8601 format (2016-03-11T03:45:40Z). Defaults to the current time.")
131 changelogGenerateCmd.Flags().StringP("from", "", "", "Start of the range of commits to use when generating the changelog, as a SHA. This commit is not included in the range.")
132 changelogGenerateCmd.Flags().StringP("to", "", "", "End of the range of commits to use when generating the changelog, as a SHA. This commit is included in the range. Defaults to the HEAD of the project's default branch.")
133 changelogGenerateCmd.Flags().StringP("trailer", "", "", "The Git trailer to use to include commits. Defaults to 'Changelog'.")
135 return changelogGenerateCmd
136}
Diagnostics8 git calls · 415 ms
CallStatusSizeTime0415 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 KiB189 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=171 git.response.bytes=59849 git.response.cache=EXPIRED git.response.duration_ms=189 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 · 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=4273 git.response.cache=HIT git.response.duration_ms=2 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:0200145 B · 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=145 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=e99fb9ba112bfbe6464eff0898dcfc2ca603063e 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=145 git.pkt.payload.max=118 git.sideband.band1.bytes=118 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=118 git.pack.trailer=a9fa321389f9b11cfb35392dc5ee2fceca794171Open pack map
fetchtree:0200150 B · 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=150 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=5bc40e3f68c9778a5a106ea652b5033da449ad49 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=150 git.pkt.payload.max=123 git.sideband.band1.bytes=123 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=123 git.pack.trailer=7afcb2c200665b7131c380674e728910f23ed880Open pack map
fetch2001.6 KiB215 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=215 git.response.bytes=1645 git.response.cache=MISS git.response.duration_ms=215 git.fetch.wants=311b3d6ecebfbf4723b24afd53e43fa61ac16c23 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=1645 git.pkt.payload.max=1618 git.sideband.band1.bytes=1618 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
- a5e342c2ced46e2a
- Request
- 6e2a7ebb…
- 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