Skip to content

Commit 1723636

Browse files
committed
all: use basic auth instead of token auth in querystring
Port the Client implementation to use github.com/kevinburke/rest, which gives us inspection of the HTTP request/response, user agents and basic auth out of the box, without needing to set those every time. Most of the rest of the code remains the same. Move the VERSION constant into the github subpackage (from the main package) so we can use it there. Fixes github-release#95. Fixes github-release#97.
1 parent 3f0a5a4 commit 1723636

10 files changed

Lines changed: 178 additions & 69 deletions

File tree

LICENSE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Nicolas Hillegeer
3+
Copyright (c) 2014-2017 Nicolas Hillegeer
4+
Copyright (c) 2020 Meter, Inc.
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy of
67
this software and associated documentation files (the "Software"), to deal in

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
github-release
22
==============
33

4+
**Note:** This has been forked from aktau/github-release, since that project is
5+
no longer maintained. Most of the code is still the same; the API's are still
6+
the same. However, we use Basic Auth to authenticate to the Github API now,
7+
instead of passing tokens in query parameters.
8+
49
A small commandline app written in Go that allows you to easily create
510
and delete releases of your projects on Github. In addition it allows
611
you to attach files to those releases.
@@ -119,6 +124,7 @@ Used libraries
119124
| [github.com/dustin/go-humanize](https://github.com/dustin/go-humanize) | humanize file sizes | MIT |
120125
| [github.com/tomnomnom/linkheader](https://github.com/tomnomnom/linkheader) | GH API pagination | MIT |
121126
| [github.com/voxelbrain/goptions](https://github.com/voxelbrain/goptions) | option parsing | BSD |
127+
| [github.com/kevinburke/rest](https://github.com/kevinburke/rest) | HTTP client | MIT |
122128

123129
Todo
124130
====
@@ -128,4 +134,5 @@ Todo
128134
Copyright
129135
=========
130136

131-
Copyright (c) 2014, Nicolas Hillegeer. All rights reserved.
137+
Copyright (c) 2014-2017, Nicolas Hillegeer. All rights reserved.
138+
Copyright (c) 2020, Meter, Inc. All rights reserved.

cmd.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
func infocmd(opt Options) error {
1818
user := nvls(opt.Info.User, EnvUser)
19+
authUser := nvls(opt.Info.AuthUser, EnvAuthUser)
1920
repo := nvls(opt.Info.Repo, EnvRepo)
2021
token := nvls(opt.Info.Token, EnvToken)
2122
tag := opt.Info.Tag
@@ -25,7 +26,7 @@ func infocmd(opt Options) error {
2526
}
2627

2728
// Find regular git tags.
28-
foundTags, err := Tags(user, repo, token)
29+
foundTags, err := Tags(user, repo, authUser, token)
2930
if err != nil {
3031
return fmt.Errorf("could not fetch tags, %v", err)
3132
}
@@ -52,14 +53,14 @@ func infocmd(opt Options) error {
5253
if tag == "" {
5354
// Get all releases.
5455
vprintf("%v/%v: getting information for all releases\n", user, repo)
55-
releases, err = Releases(user, repo, token)
56+
releases, err = Releases(user, repo, authUser, token)
5657
if err != nil {
5758
return err
5859
}
5960
} else {
6061
// Get only one release.
6162
vprintf("%v/%v/%v: getting information for the release\n", user, repo, tag)
62-
release, err := ReleaseOfTag(user, repo, tag, token)
63+
release, err := ReleaseOfTag(user, repo, tag, authUser, token)
6364
if err != nil {
6465
return err
6566
}
@@ -99,6 +100,7 @@ func renderInfoJSON(tags []Tag, releases []Release) error {
99100

100101
func uploadcmd(opt Options) error {
101102
user := nvls(opt.Upload.User, EnvUser)
103+
authUser := nvls(opt.Upload.AuthUser, EnvAuthUser)
102104
repo := nvls(opt.Upload.Repo, EnvRepo)
103105
token := nvls(opt.Upload.Token, EnvToken)
104106
tag := opt.Upload.Tag
@@ -118,7 +120,7 @@ func uploadcmd(opt Options) error {
118120
}
119121

120122
// Find the release corresponding to the entered tag, if any.
121-
rel, err := ReleaseOfTag(user, repo, tag, token)
123+
rel, err := ReleaseOfTag(user, repo, tag, authUser, token)
122124
if err != nil {
123125
return err
124126
}
@@ -130,7 +132,9 @@ func uploadcmd(opt Options) error {
130132
// uploads (which regrettably happen often using the Github API). See
131133
// issue #26.
132134
var assets []Asset
133-
err = github.Client{Token: token, BaseURL: EnvApiEndpoint}.Get(fmt.Sprintf(ASSET_RELEASE_LIST_URI, user, repo, rel.Id), &assets)
135+
client := github.NewClient(authUser, token, nil)
136+
client.SetBaseURL(EnvApiEndpoint)
137+
err = client.Get(fmt.Sprintf(ASSET_RELEASE_LIST_URI, user, repo, rel.Id), &assets)
134138
if err != nil {
135139
return err
136140
}
@@ -204,6 +208,7 @@ func uploadcmd(opt Options) error {
204208

205209
func downloadcmd(opt Options) error {
206210
user := nvls(opt.Download.User, EnvUser)
211+
authUser := nvls(opt.Download.AuthUser, EnvAuthUser)
207212
repo := nvls(opt.Download.Repo, EnvRepo)
208213
token := nvls(opt.Download.Token, EnvToken)
209214
tag := opt.Download.Tag
@@ -220,9 +225,9 @@ func downloadcmd(opt Options) error {
220225
var rel *Release
221226
var err error
222227
if latest {
223-
rel, err = LatestRelease(user, repo, token)
228+
rel, err = LatestRelease(user, repo, authUser, token)
224229
} else {
225-
rel, err = ReleaseOfTag(user, repo, tag, token)
230+
rel, err = ReleaseOfTag(user, repo, tag, authUser, token)
226231
}
227232
if err != nil {
228233
return err
@@ -379,6 +384,7 @@ func releasecmd(opt Options) error {
379384
func editcmd(opt Options) error {
380385
cmdopt := opt.Edit
381386
user := nvls(cmdopt.User, EnvUser)
387+
authUser := nvls(cmdopt.AuthUser, EnvAuthUser)
382388
repo := nvls(cmdopt.Repo, EnvRepo)
383389
token := nvls(cmdopt.Token, EnvToken)
384390
tag := cmdopt.Tag
@@ -393,7 +399,7 @@ func editcmd(opt Options) error {
393399
return err
394400
}
395401

396-
id, err := IdOfTag(user, repo, tag, token)
402+
id, err := IdOfTag(user, repo, tag, authUser, token)
397403
if err != nil {
398404
return err
399405
}
@@ -456,9 +462,10 @@ func deletecmd(opt Options) error {
456462
nvls(opt.Delete.Repo, EnvRepo),
457463
nvls(opt.Delete.Token, EnvToken),
458464
opt.Delete.Tag
465+
authUser := nvls(opt.Delete.AuthUser, EnvAuthUser)
459466
vprintln("deleting...")
460467

461-
id, err := IdOfTag(user, repo, tag, token)
468+
id, err := IdOfTag(user, repo, tag, authUser, token)
462469
if err != nil {
463470
return err
464471
}

github-release.go

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,24 @@ type Options struct {
1616

1717
goptions.Verbs
1818
Download struct {
19-
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
20-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
21-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
22-
Latest bool `goptions:"-l, --latest, description='Download latest release (required if tag is not specified)',mutexgroup='input'"`
23-
Tag string `goptions:"-t, --tag, description='Git tag to download from (required if latest is not specified)', mutexgroup='input',obligatory"`
24-
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
19+
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
20+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
21+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
22+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
23+
Latest bool `goptions:"-l, --latest, description='Download latest release (required if tag is not specified)',mutexgroup='input'"`
24+
Tag string `goptions:"-t, --tag, description='Git tag to download from (required if latest is not specified)', mutexgroup='input',obligatory"`
25+
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
2526
} `goptions:"download"`
2627
Upload struct {
27-
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
28-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
29-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
30-
Tag string `goptions:"-t, --tag, description='Git tag to upload to', obligatory"`
31-
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
32-
Label string `goptions:"-l, --label, description='Label (description) of the file'"`
33-
File *os.File `goptions:"-f, --file, description='File to upload (use - for stdin)', rdonly, obligatory"`
34-
Replace bool `goptions:"-R, --replace, description='Replace asset with same name if it already exists (WARNING: not atomic, failure to upload will remove the original asset too)'"`
28+
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
29+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
30+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
31+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
32+
Tag string `goptions:"-t, --tag, description='Git tag to upload to', obligatory"`
33+
Name string `goptions:"-n, --name, description='Name of the file', obligatory"`
34+
Label string `goptions:"-l, --label, description='Label (description) of the file'"`
35+
File *os.File `goptions:"-f, --file, description='File to upload (use - for stdin)', rdonly, obligatory"`
36+
Replace bool `goptions:"-R, --replace, description='Replace asset with same name if it already exists (WARNING: not atomic, failure to upload will remove the original asset too)'"`
3537
} `goptions:"upload"`
3638
Release struct {
3739
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
@@ -47,6 +49,7 @@ type Options struct {
4749
Edit struct {
4850
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
4951
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
52+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
5053
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
5154
Tag string `goptions:"-t, --tag, obligatory, description='Git tag to edit the release of'"`
5255
Name string `goptions:"-n, --name, description='New name of the release (defaults to tag)'"`
@@ -55,17 +58,19 @@ type Options struct {
5558
Prerelease bool `goptions:"-p, --pre-release, description='The release is a pre-release'"`
5659
} `goptions:"edit"`
5760
Delete struct {
58-
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
59-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
60-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
61-
Tag string `goptions:"-t, --tag, obligatory, description='Git tag of release to delete'"`
61+
Token string `goptions:"-s, --security-token, description='Github token (required if $GITHUB_TOKEN not set)'"`
62+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
63+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
64+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
65+
Tag string `goptions:"-t, --tag, obligatory, description='Git tag of release to delete'"`
6266
} `goptions:"delete"`
6367
Info struct {
64-
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
65-
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
66-
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
67-
Tag string `goptions:"-t, --tag, description='Git tag to query (optional)'"`
68-
JSON bool `goptions:"-j, --json, description='Emit info as JSON instead of text'"`
68+
Token string `goptions:"-s, --security-token, description='Github token ($GITHUB_TOKEN if set). required if repo is private.'"`
69+
User string `goptions:"-u, --user, description='Github repo user or organisation (required if $GITHUB_USER not set)'"`
70+
AuthUser string `goptions:"-a, --auth-user, description='Username for authenticating to the API (falls back to $GITHUB_AUTH_USER or $GITHUB_USER)'"`
71+
Repo string `goptions:"-r, --repo, description='Github repo (required if $GITHUB_REPO not set)'"`
72+
Tag string `goptions:"-t, --tag, description='Git tag to query (optional)'"`
73+
JSON bool `goptions:"-j, --json, description='Emit info as JSON instead of text'"`
6974
} `goptions:"info"`
7075
}
7176

@@ -85,6 +90,9 @@ var (
8590
)
8691

8792
var (
93+
// The user whose token is being used to authenticate to the API. If unset,
94+
// EnvUser is used.
95+
EnvAuthUser string
8896
EnvToken string
8997
EnvUser string
9098
EnvRepo string
@@ -94,8 +102,13 @@ var (
94102
func init() {
95103
EnvToken = os.Getenv("GITHUB_TOKEN")
96104
EnvUser = os.Getenv("GITHUB_USER")
105+
EnvAuthUser = os.Getenv("GITHUB_AUTH_USER")
97106
EnvRepo = os.Getenv("GITHUB_REPO")
98107
EnvApiEndpoint = os.Getenv("GITHUB_API")
108+
109+
if EnvAuthUser == "" {
110+
EnvAuthUser = EnvUser
111+
}
99112
}
100113

101114
func main() {
@@ -104,7 +117,7 @@ func main() {
104117
goptions.ParseAndFail(&options)
105118

106119
if options.Version {
107-
fmt.Printf("github-release v%s\n", VERSION)
120+
fmt.Printf("github-release v%s\n", github.VERSION)
108121
return
109122
}
110123

0 commit comments

Comments
 (0)