X Tutup
Skip to content

Commit 4fa984a

Browse files
authored
Merge pull request cli#4003 from despreston/3381-release-discussions
add --discussion-category flag to release cmd
2 parents c5371d5 + bdc5b55 commit 4fa984a

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

pkg/cmd/release/create/create.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package create
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
67
"net/http"
78
"os"
@@ -48,6 +49,8 @@ type CreateOptions struct {
4849

4950
// maximum number of simultaneous uploads
5051
Concurrency int
52+
53+
DiscussionCategory string
5154
}
5255

5356
func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Command {
@@ -92,9 +95,16 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
9295
9396
Upload a release asset with a display label
9497
$ gh release create v1.2.3 '/path/to/asset.zip#My display label'
98+
99+
Create a release and start a discussion
100+
$ gh release create v1.2.3 --discussion-category "General"
95101
`),
96102
Args: cmdutil.MinimumArgs(1, "could not create: no tag name provided"),
97103
RunE: func(cmd *cobra.Command, args []string) error {
104+
if cmd.Flags().Changed("discussion-category") && opts.Draft {
105+
return errors.New("Discussions for draft releases not supported")
106+
}
107+
98108
// support `-R, --repo` override
99109
opts.BaseRepo = f.BaseRepo
100110
opts.RepoOverride, _ = cmd.Flags().GetString("repo")
@@ -132,6 +142,7 @@ func NewCmdCreate(f *cmdutil.Factory, runF func(*CreateOptions) error) *cobra.Co
132142
cmd.Flags().StringVarP(&opts.Name, "title", "t", "", "Release title")
133143
cmd.Flags().StringVarP(&opts.Body, "notes", "n", "", "Release notes")
134144
cmd.Flags().StringVarP(&notesFile, "notes-file", "F", "", "Read release notes from `file`")
145+
cmd.Flags().StringVarP(&opts.DiscussionCategory, "discussion-category", "", "", "Start a discussion of the specified category")
135146

136147
return cmd
137148
}
@@ -268,6 +279,13 @@ func createRun(opts *CreateOptions) error {
268279
}
269280
}
270281

282+
if opts.Draft && len(opts.DiscussionCategory) > 0 {
283+
return fmt.Errorf(
284+
"%s Discussions not supported with draft releases",
285+
opts.IO.ColorScheme().FailureIcon(),
286+
)
287+
}
288+
271289
params := map[string]interface{}{
272290
"tag_name": opts.TagName,
273291
"draft": opts.Draft,
@@ -278,6 +296,9 @@ func createRun(opts *CreateOptions) error {
278296
if opts.Target != "" {
279297
params["target_commitish"] = opts.Target
280298
}
299+
if opts.DiscussionCategory != "" {
300+
params["discussion_category_name"] = opts.DiscussionCategory
301+
}
281302

282303
hasAssets := len(opts.Assets) > 0
283304

pkg/cmd/release/create/create_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,30 @@ func Test_NewCmdCreate(t *testing.T) {
160160
isTTY: true,
161161
wantErr: "could not create: no tag name provided",
162162
},
163+
{
164+
name: "discussion category",
165+
args: "v1.2.3 --discussion-category 'General'",
166+
isTTY: true,
167+
want: CreateOptions{
168+
TagName: "v1.2.3",
169+
Target: "",
170+
Name: "",
171+
Body: "",
172+
BodyProvided: false,
173+
Draft: false,
174+
Prerelease: false,
175+
RepoOverride: "",
176+
Concurrency: 5,
177+
Assets: []*shared.AssetForUpload(nil),
178+
DiscussionCategory: "General",
179+
},
180+
},
181+
{
182+
name: "discussion category for draft release",
183+
args: "v1.2.3 -d --discussion-category 'General'",
184+
isTTY: true,
185+
wantErr: "Discussions for draft releases not supported",
186+
},
163187
}
164188
for _, tt := range tests {
165189
t.Run(tt.name, func(t *testing.T) {
@@ -209,6 +233,7 @@ func Test_NewCmdCreate(t *testing.T) {
209233
assert.Equal(t, tt.want.Prerelease, opts.Prerelease)
210234
assert.Equal(t, tt.want.Concurrency, opts.Concurrency)
211235
assert.Equal(t, tt.want.RepoOverride, opts.RepoOverride)
236+
assert.Equal(t, tt.want.DiscussionCategory, opts.DiscussionCategory)
212237

213238
require.Equal(t, len(tt.want.Assets), len(opts.Assets))
214239
for i := range tt.want.Assets {
@@ -249,6 +274,28 @@ func Test_createRun(t *testing.T) {
249274
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
250275
wantStderr: ``,
251276
},
277+
{
278+
name: "with discussion category",
279+
isTTY: true,
280+
opts: CreateOptions{
281+
TagName: "v1.2.3",
282+
Name: "The Big 1.2",
283+
Body: "* Fixed bugs",
284+
BodyProvided: true,
285+
Target: "",
286+
DiscussionCategory: "General",
287+
},
288+
wantParams: map[string]interface{}{
289+
"tag_name": "v1.2.3",
290+
"name": "The Big 1.2",
291+
"body": "* Fixed bugs",
292+
"draft": false,
293+
"prerelease": false,
294+
"discussion_category_name": "General",
295+
},
296+
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
297+
wantStderr: ``,
298+
},
252299
{
253300
name: "with target commitish",
254301
isTTY: true,
@@ -291,6 +338,29 @@ func Test_createRun(t *testing.T) {
291338
wantStdout: "https://github.com/OWNER/REPO/releases/tag/v1.2.3\n",
292339
wantStderr: ``,
293340
},
341+
{
342+
name: "discussion category for draft release",
343+
isTTY: true,
344+
opts: CreateOptions{
345+
TagName: "v1.2.3",
346+
Name: "",
347+
Body: "",
348+
BodyProvided: true,
349+
Draft: true,
350+
Target: "",
351+
DiscussionCategory: "general",
352+
},
353+
wantParams: map[string]interface{}{
354+
"tag_name": "v1.2.3",
355+
"name": "",
356+
"body": "",
357+
"draft": true,
358+
"prerelease": false,
359+
"discussion_category_name": "general",
360+
},
361+
wantErr: "X Discussions not supported with draft releases",
362+
wantStdout: "",
363+
},
294364
{
295365
name: "publish after uploading files",
296366
isTTY: true,

0 commit comments

Comments
 (0)
X Tutup