fix(release create): Handle latest flag value when updating the release after assets are uploaded (#8207)

Signed-off-by: Arun <arun@arun.blog>
Co-authored-by: William Martin <williammartin@github.com>
This commit is contained in:
Arun Sathiya 2023-11-01 07:17:05 -07:00 committed by GitHub
parent 534f6d7978
commit 5eff7a529a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 2 deletions

View file

@ -499,7 +499,7 @@ func createRun(opts *CreateOptions) error {
}
if draftWhileUploading {
rel, err := publishRelease(httpClient, newRelease.APIURL, opts.DiscussionCategory)
rel, err := publishRelease(httpClient, newRelease.APIURL, opts.DiscussionCategory, opts.IsLatest)
if err != nil {
return cleanupDraftRelease(err)
}

View file

@ -754,6 +754,69 @@ func Test_createRun(t *testing.T) {
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n",
wantStderr: ``,
},
{
name: "publish after uploading files, but do not mark as latest",
isTTY: true,
opts: CreateOptions{
TagName: "v1.2.3",
Name: "",
Body: "",
BodyProvided: true,
Draft: false,
IsLatest: boolPtr(false),
Target: "",
Assets: []*shared.AssetForUpload{
{
Name: "ball.tgz",
Open: func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewBufferString(`TARBALL`)), nil
},
},
},
Concurrency: 1,
},
runStubs: func(rs *run.CommandStubber) {
rs.Register(`git tag --list`, 0, "")
},
httpStubs: func(t *testing.T, reg *httpmock.Registry) {
reg.Register(httpmock.REST("HEAD", "repos/OWNER/REPO/releases/tags/v1.2.3"), httpmock.StatusStringResponse(404, ``))
reg.Register(httpmock.REST("POST", "repos/OWNER/REPO/releases"), httpmock.RESTPayload(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"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"tag_name": "v1.2.3",
"draft": true,
"prerelease": false,
"make_latest": "false",
}, params)
}))
reg.Register(httpmock.REST("POST", "assets/upload"), func(req *http.Request) (*http.Response, error) {
q := req.URL.Query()
assert.Equal(t, "ball.tgz", q.Get("name"))
assert.Equal(t, "", q.Get("label"))
return &http.Response{
StatusCode: 201,
Request: req,
Body: io.NopCloser(bytes.NewBufferString(`{}`)),
Header: map[string][]string{
"Content-Type": {"application/json"},
},
}, nil
})
reg.Register(httpmock.REST("PATCH", "releases/123"), httpmock.RESTPayload(201, `{
"html_url": "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final"
}`, func(params map[string]interface{}) {
assert.Equal(t, map[string]interface{}{
"draft": false,
"make_latest": "false",
}, params)
}))
},
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3-final\n",
wantStderr: ``,
},
{
name: "upload files but release already exists",
isTTY: true,

View file

@ -189,12 +189,16 @@ func createRelease(httpClient *http.Client, repo ghrepo.Interface, params map[st
return &newRelease, err
}
func publishRelease(httpClient *http.Client, releaseURL string, discussionCategory string) (*shared.Release, error) {
func publishRelease(httpClient *http.Client, releaseURL string, discussionCategory string, isLatest *bool) (*shared.Release, error) {
params := map[string]interface{}{"draft": false}
if discussionCategory != "" {
params["discussion_category_name"] = discussionCategory
}
if isLatest != nil {
params["make_latest"] = fmt.Sprintf("%v", *isLatest)
}
bodyBytes, err := json.Marshal(params)
if err != nil {
return nil, err