From 11bef8c3b5df8bef6c7ebed2f02a44800c063466 Mon Sep 17 00:00:00 2001 From: Luan Vieira Date: Wed, 16 Nov 2022 13:45:55 -0500 Subject: [PATCH 1/3] Add --verify-tag flag for release creation command Fixes https://github.com/cli/cli/issues/6566 When running `gh release create --verify-tag`, we query among repository tags via the GitHub API before creating the release, and abort the command if the tag was not found. --- pkg/cmd/release/create/create.go | 20 +++++++- pkg/cmd/release/create/create_test.go | 72 +++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/release/create/create.go b/pkg/cmd/release/create/create.go index f4f04ac48..ad7ca4993 100644 --- a/pkg/cmd/release/create/create.go +++ b/pkg/cmd/release/create/create.go @@ -53,6 +53,7 @@ type CreateOptions struct { DiscussionCategory string GenerateNotes bool NotesStartTag string + VerifyTag bool } func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command { @@ -78,7 +79,9 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co display label for an asset, append text starting with %[1]s#%[1]s after the file name. If a matching git tag does not yet exist, one will automatically get created - from the latest state of the default branch. Use %[1]s--target%[1]s to override this. + from the latest state of the default branch. + Use %[1]s--target%[1]s to point to a different branch or commit for the automatic tag creation. + Use %[1]s--verify-tag%[1]s to abort the release if the tag doesn't already exist. To fetch the new tag locally after the release, do %[1]sgit fetch --tags origin%[1]s. To create a release from an annotated git tag, first create one locally with @@ -166,6 +169,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co cmd.Flags().BoolVarP(&opts.GenerateNotes, "generate-notes", "", false, "Automatically generate title and notes for the release") cmd.Flags().StringVar(&opts.NotesStartTag, "notes-start-tag", "", "Tag to use as the starting point for generating release notes") cmdutil.NilBoolFlag(cmd, &opts.IsLatest, "latest", "", "Mark this release as \"Latest\" (default: automatic based on date and version)") + cmd.Flags().BoolVarP(&opts.VerifyTag, "verify-tag", "", false, "Abort the release if the tag doesn't already exist") return cmd } @@ -224,6 +228,18 @@ func createRun(opts *CreateOptions) error { } } + var remoteTagPresent bool + if opts.VerifyTag && !existingTag { + remoteTagPresent, err = remoteTagExists(httpClient, baseRepo, opts.TagName) + if err != nil { + return err + } + if !remoteTagPresent { + return fmt.Errorf("tag %s doesn't exist in the repo %s, aborting due to --verify-tag flag", + opts.TagName, ghrepo.FullName(baseRepo)) + } + } + var tagDescription string if opts.RepoOverride == "" { tagDescription, _ = gitTagInfo(opts.GitClient, opts.TagName) @@ -235,7 +251,7 @@ func createRun(opts *CreateOptions) error { // of local tag status. // If a remote tag with the same name as specified exists already // then a new tag will not be created so ignore local tag status. - if tagDescription != "" && !existingTag && opts.Target == "" { + if tagDescription != "" && !existingTag && opts.Target == "" && !remoteTagPresent { remoteExists, err := remoteTagExists(httpClient, baseRepo, opts.TagName) if err != nil { return err diff --git a/pkg/cmd/release/create/create_test.go b/pkg/cmd/release/create/create_test.go index 1a7a43c60..8c842945e 100644 --- a/pkg/cmd/release/create/create_test.go +++ b/pkg/cmd/release/create/create_test.go @@ -60,6 +60,7 @@ func Test_NewCmdCreate(t *testing.T) { RepoOverride: "", Concurrency: 5, Assets: []*shared.AssetForUpload(nil), + VerifyTag: false, }, }, { @@ -83,6 +84,7 @@ func Test_NewCmdCreate(t *testing.T) { RepoOverride: "", Concurrency: 5, Assets: []*shared.AssetForUpload(nil), + VerifyTag: false, }, }, { @@ -300,6 +302,25 @@ func Test_NewCmdCreate(t *testing.T) { NotesStartTag: "", }, }, + { + name: "with verify-tag", + args: "v1.1.0 --verify-tag", + isTTY: true, + want: CreateOptions{ + TagName: "v1.1.0", + Target: "", + Name: "", + Body: "", + BodyProvided: false, + Draft: false, + Prerelease: false, + RepoOverride: "", + Concurrency: 5, + Assets: []*shared.AssetForUpload(nil), + GenerateNotes: false, + VerifyTag: true, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -353,6 +374,7 @@ func Test_NewCmdCreate(t *testing.T) { assert.Equal(t, tt.want.GenerateNotes, opts.GenerateNotes) assert.Equal(t, tt.want.NotesStartTag, opts.NotesStartTag) assert.Equal(t, tt.want.IsLatest, opts.IsLatest) + assert.Equal(t, tt.want.VerifyTag, opts.VerifyTag) require.Equal(t, len(tt.want.Assets), len(opts.Assets)) for i := range tt.want.Assets { @@ -1125,6 +1147,56 @@ func Test_createRun_interactive(t *testing.T) { }, wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n", }, + { + name: "create a release when remote tag exists and verify-tag flag is set", + opts: &CreateOptions{ + TagName: "v1.2.3", + VerifyTag: true, + }, + askStubs: func(as *prompt.AskStubber) { + as.StubPrompt("Title (optional)").AnswerWith("") + as.StubPrompt("Release notes"). + AssertOptions([]string{"Write my own", "Write using generated notes as template", "Write using git tag message as template", "Leave blank"}). + AnswerWith("Leave blank") + as.StubPrompt("Is this a prerelease?").AnswerWith(false) + as.StubPrompt("Submit?").AnswerWith("Publish release") + }, + runStubs: func(rs *run.CommandStubber) { + rs.Register(`git tag --list`, 0, "tag exists") + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL("RepositoryFindRef"), + httpmock.StringResponse(`{"data":{"repository":{"ref": {"id": "tag id"}}}}`)) + reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases/generate-notes"), + httpmock.StatusStringResponse(200, `{ + "name": "generated name", + "body": "generated body" + }`)) + reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.StatusStringResponse(201, `{ + "url": "https://api.github.com/releases/123", + "upload_url": "https://api.github.com/assets/upload", + "html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3" + }`)) + }, + wantParams: map[string]interface{}{ + "draft": false, + "prerelease": false, + "tag_name": "v1.2.3", + }, + wantOut: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n", + }, + { + name: "error when remote tag does not exist and verify-tag flag is set", + opts: &CreateOptions{ + TagName: "v1.2.3", + VerifyTag: true, + }, + httpStubs: func(reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL("RepositoryFindRef"), + httpmock.StringResponse(`{"data":{"repository":{"ref": {"id": ""}}}}`)) + }, + wantErr: "tag v1.2.3 doesn't exist in the repo OWNER/REPO, aborting due to --verify-tag flag", + }, } for _, tt := range tests { ios, _, stdout, stderr := iostreams.Test() From f1a067ff182decbdf45d80ecc8ea03fb7586909b Mon Sep 17 00:00:00 2001 From: Luan Vieira Date: Wed, 16 Nov 2022 15:09:21 -0500 Subject: [PATCH 2/3] Clarify --verify-tag flag description MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- pkg/cmd/release/create/create.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/release/create/create.go b/pkg/cmd/release/create/create.go index ad7ca4993..26e70b76d 100644 --- a/pkg/cmd/release/create/create.go +++ b/pkg/cmd/release/create/create.go @@ -169,7 +169,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co cmd.Flags().BoolVarP(&opts.GenerateNotes, "generate-notes", "", false, "Automatically generate title and notes for the release") cmd.Flags().StringVar(&opts.NotesStartTag, "notes-start-tag", "", "Tag to use as the starting point for generating release notes") cmdutil.NilBoolFlag(cmd, &opts.IsLatest, "latest", "", "Mark this release as \"Latest\" (default: automatic based on date and version)") - cmd.Flags().BoolVarP(&opts.VerifyTag, "verify-tag", "", false, "Abort the release if the tag doesn't already exist") + cmd.Flags().BoolVarP(&opts.VerifyTag, "verify-tag", "", false, "Abort in case the git tag doesn't already exist in the remote repository") return cmd } From fad72b788de367ee2ec6a54eccafb51250121c50 Mon Sep 17 00:00:00 2001 From: Luan Vieira Date: Wed, 16 Nov 2022 15:10:46 -0500 Subject: [PATCH 3/3] Avoid duplicate remote tag lookups MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mislav Marohnić --- pkg/cmd/release/create/create.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/release/create/create.go b/pkg/cmd/release/create/create.go index 26e70b76d..01fc2e33c 100644 --- a/pkg/cmd/release/create/create.go +++ b/pkg/cmd/release/create/create.go @@ -228,9 +228,8 @@ func createRun(opts *CreateOptions) error { } } - var remoteTagPresent bool if opts.VerifyTag && !existingTag { - remoteTagPresent, err = remoteTagExists(httpClient, baseRepo, opts.TagName) + remoteTagPresent, err := remoteTagExists(httpClient, baseRepo, opts.TagName) if err != nil { return err } @@ -251,7 +250,7 @@ func createRun(opts *CreateOptions) error { // of local tag status. // If a remote tag with the same name as specified exists already // then a new tag will not be created so ignore local tag status. - if tagDescription != "" && !existingTag && opts.Target == "" && !remoteTagPresent { + if tagDescription != "" && !existingTag && opts.Target == "" && !opts.VerifyTag { remoteExists, err := remoteTagExists(httpClient, baseRepo, opts.TagName) if err != nil { return err