From be41a9bd20c597231e5f32494f47bbc313b16f40 Mon Sep 17 00:00:00 2001 From: Keith Bailey Date: Mon, 11 Sep 2023 14:17:49 -0400 Subject: [PATCH] moved remoteTagExists under if NotesFromTag exists and added three tests --- pkg/cmd/release/create/create.go | 6 +- pkg/cmd/release/create/create_test.go | 83 +++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/release/create/create.go b/pkg/cmd/release/create/create.go index 07e40b708..aa021748b 100644 --- a/pkg/cmd/release/create/create.go +++ b/pkg/cmd/release/create/create.go @@ -248,7 +248,6 @@ func createRun(opts *CreateOptions) error { var tagDescription string if opts.RepoOverride == "" { tagDescription, _ = gitTagInfo(opts.GitClient, opts.TagName) - remoteExists, err := remoteTagExists(httpClient, baseRepo, opts.TagName) // If there is a local tag with the same name as specified // the user may not want to create a new tag on the remote // as the local one might be annotated or signed. @@ -258,6 +257,7 @@ func createRun(opts *CreateOptions) error { // 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 == "" && !opts.VerifyTag { + remoteExists, err := remoteTagExists(httpClient, baseRepo, opts.TagName) if err != nil { return err } @@ -268,6 +268,10 @@ func createRun(opts *CreateOptions) error { } if opts.NotesFromTag { + remoteExists, err := remoteTagExists(httpClient, baseRepo, opts.TagName) + if err != nil { + return err + } if !remoteExists { return fmt.Errorf("tag %s doesn't exist in the repo %s, cannot populate release notes with annotated git tag message using the `--notes-from-tag` flag", opts.TagName, ghrepo.FullName(baseRepo)) diff --git a/pkg/cmd/release/create/create_test.go b/pkg/cmd/release/create/create_test.go index 1787c0931..53af2168b 100644 --- a/pkg/cmd/release/create/create_test.go +++ b/pkg/cmd/release/create/create_test.go @@ -921,6 +921,89 @@ func Test_createRun(t *testing.T) { wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n", wantStderr: ``, }, + { + name: "with tag and --notes-from-tag", + isTTY: false, + opts: CreateOptions{ + TagName: "v1.2.3", + Target: "", + Name: "", + Body: "", + BodyProvided: true, + Draft: false, + Prerelease: false, + RepoOverride: "", + Concurrency: 5, + Assets: []*shared.AssetForUpload(nil), + GenerateNotes: false, + NotesFromTag: true, + }, + httpStubs: func(t *testing.T, 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"), 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" + }`)) + }, + wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n", + wantStderr: "", + }, + { + name: "with tag and --notes-from-tag and --notes", + isTTY: false, + opts: CreateOptions{ + TagName: "v1.2.3", + Target: "", + Name: "", + Body: "Notes from --notes here", + BodyProvided: true, + Draft: false, + Prerelease: false, + RepoOverride: "", + Concurrency: 5, + Assets: []*shared.AssetForUpload(nil), + GenerateNotes: false, + NotesFromTag: true, + }, + httpStubs: func(t *testing.T, 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"), 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" + }`)) + }, + wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n", + wantStderr: "", + }, + { + name: "tag and --notes-from-tag but tag does not exist in repo", + isTTY: false, + opts: CreateOptions{ + TagName: "v1.2.4", + Target: "", + Name: "", + Body: "", + BodyProvided: true, + Draft: false, + Prerelease: false, + RepoOverride: "", + Concurrency: 5, + Assets: []*shared.AssetForUpload(nil), + GenerateNotes: false, + NotesFromTag: true, + }, + httpStubs: func(t *testing.T, reg *httpmock.Registry) { + reg.Register(httpmock.GraphQL("RepositoryFindRef"), + httpmock.StringResponse(`{"data":{"repository":{"ref": {"id": ""}}}}`)) + }, + wantStdout: "", + wantStderr: "", + wantErr: "tag v1.2.4 doesn't exist in the repo OWNER/REPO, cannot populate release notes with annotated git tag message using the `--notes-from-tag` flag", + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {