Skip to content

Commit 64458cd

Browse files
committed
add 'edit' command
1 parent d5120ec commit 64458cd

2 files changed

Lines changed: 89 additions & 3 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ $ github-release release \
7171
--description "Not a movie, contrary to popular opinion. Still, my first release!" \
7272
--pre-release
7373

74+
# you've made a mistake, but you can edit the release without
75+
# having to delete it first (this also means you can edit without having
76+
# to upload your files again)
77+
$ github-release edit \
78+
--user aktau \
79+
--repo gofinance \
80+
--tag v0.1.0 \
81+
--name "Highlander II: The Quickening" \
82+
--description "This is the actual description!"
83+
7484
# upload a file, for example the OSX/AMD64 binary of my gofinance app
7585
$ github-release upload \
7686
--user aktau \

github-release.go

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ type Options struct {
3636
Draft bool `goptions:"--draft, description='The release is a draft'"`
3737
Prerelease bool `goptions:"-p, --pre-release, description='The release is a pre-release'"`
3838
} `goptions:"release"`
39+
Edit struct {
40+
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
41+
User string `goptions:"-u, --user, description='Github user (required if $GITHUB_USER not set)'"`
42+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
43+
Tag string `goptions:"-t, --tag, obligatory, description='Git tag to edit the release of'"`
44+
Name string `goptions:"-n, --name, description='New name of the release (defaults to tag)'"`
45+
Desc string `goptions:"-d, --description, description='New description of the release (defaults to tag)'"`
46+
Draft bool `goptions:"--draft, description='The release is a draft'"`
47+
Prerelease bool `goptions:"-p, --pre-release, description='The release is a pre-release'"`
48+
} `goptions:"edit"`
3949
Delete struct {
4050
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
4151
User string `goptions:"-u, --user, description='Github user (required if $GITHUB_USER not set)'"`
@@ -54,6 +64,7 @@ type Command func(Options) error
5464
var commands = map[goptions.Verbs]Command{
5565
"upload": uploadcmd,
5666
"release": releasecmd,
67+
"edit": editcmd,
5768
"delete": deletecmd,
5869
"info": infocmd,
5970
}
@@ -63,9 +74,9 @@ var (
6374
)
6475

6576
var (
66-
EnvToken string
67-
EnvUser string
68-
EnvRepo string
77+
EnvToken string
78+
EnvUser string
79+
EnvRepo string
6980
EnvApiEndpoint string
7081
)
7182

@@ -296,6 +307,71 @@ func releasecmd(opt Options) error {
296307
return nil
297308
}
298309

310+
func editcmd(opt Options) error {
311+
cmdopt := opt.Edit
312+
user := nvls(cmdopt.User, EnvUser)
313+
repo := nvls(cmdopt.Repo, EnvRepo)
314+
token := nvls(cmdopt.Token, EnvToken)
315+
tag := cmdopt.Tag
316+
name := nvls(cmdopt.Name, tag)
317+
desc := nvls(cmdopt.Desc, tag)
318+
draft := cmdopt.Draft
319+
prerelease := cmdopt.Prerelease
320+
321+
vprintln("editing...")
322+
323+
if err := ValidateCredentials(user, repo, token, tag); err != nil {
324+
return err
325+
}
326+
327+
id, err := IdOfTag(user, repo, tag)
328+
if err != nil {
329+
return err
330+
}
331+
332+
vprintf("release %v has id %v\n", tag, id)
333+
334+
/* the release create struct works for editing releases as well */
335+
params := ReleaseCreate{
336+
TagName: tag,
337+
Name: name,
338+
Body: desc,
339+
Draft: draft,
340+
Prerelease: prerelease,
341+
}
342+
343+
/* encode the parameters as JSON, as required by the github API */
344+
payload, err := json.Marshal(params)
345+
if err != nil {
346+
return fmt.Errorf("can't encode release creation params, %v", err)
347+
}
348+
349+
uri := fmt.Sprintf("/repos/%s/%s/releases/%d", user, repo, id)
350+
resp, err := DoAuthRequest("PATCH", ApiURL()+uri, "application/json",
351+
token, bytes.NewReader(payload))
352+
if err != nil {
353+
return fmt.Errorf("while submitting %v, %v", string(payload), err)
354+
}
355+
defer resp.Body.Close()
356+
357+
vprintln("RESPONSE:", resp)
358+
if resp.StatusCode != http.StatusOK {
359+
if resp.StatusCode == 422 {
360+
return fmt.Errorf("github returned %v (this is probably because the release already exists)",
361+
resp.Status)
362+
}
363+
return fmt.Errorf("github returned unexpected status code %v", resp.Status)
364+
}
365+
366+
body, err := ioutil.ReadAll(resp.Body)
367+
if err != nil {
368+
return fmt.Errorf("error while reading response, %v", err)
369+
}
370+
vprintln("BODY:", string(body))
371+
372+
return nil
373+
}
374+
299375
func deletecmd(opt Options) error {
300376
user, repo, token, tag := nvls(opt.Delete.User, EnvUser),
301377
nvls(opt.Delete.Repo, EnvRepo),

0 commit comments

Comments
 (0)