diff --git a/pkg/cmd/release/create/create.go b/pkg/cmd/release/create/create.go index f4f04ac48..01fc2e33c 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 in case the git tag doesn't already exist in the remote repository") return cmd } @@ -224,6 +228,17 @@ func createRun(opts *CreateOptions) error { } } + 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 +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 == "" { + if tagDescription != "" && !existingTag && opts.Target == "" && !opts.VerifyTag { 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()