Skip to content

Commit d1f0be3

Browse files
authored
Add/bboauth (gabrie30#173)
* Add bitbucket oauth
1 parent 70a8915 commit d1f0be3

File tree

8 files changed

+50
-9
lines changed

8 files changed

+50
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
## [1.7.6] - unreleased
77
### Added
88
- goreleaser
9+
- GHORG_BITBUCKET_OAUTH_TOKEN to support oauth tokens for bitbucket
910
### Changed
1011
### Deprecated
1112
### Removed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,17 @@ $ ghorg clone fdroid --scm=gitlab --token=XXXX --preserve-dir
155155

156156
### bitbucket setup
157157

158-
1. To configure with bitbucket you will need to create a new [app password](https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html) and update your `$HOME/.config/ghorg/conf.yaml` [here](https://github.com/gabrie30/ghorg/blob/master/sample-conf.yaml#L37-L47) or use the (--token, -t) and (--bitbucket-username) flags.
158+
#### app passwords
159+
160+
1. To configure with bitbucket you will need to create a new [app password](https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html) and update your `$HOME/.config/ghorg/conf.yaml` or use the (--token, -t) and (--bitbucket-username) flags.
159161
1. Update [SCM type](https://github.com/gabrie30/ghorg/blob/master/sample-conf.yaml#L54-L57) to `bitbucket` in your `ghorg/conf.yaml` or via cli flags
160162

163+
#### PAT/OAuth token
164+
165+
1. Create a [PAT](https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html)
166+
1. Set the token with `GHORG_BITBUCKET_OAUTH_TOKEN` in your `$HOME/.config/ghorg/conf.yaml` or using the `--token` flag. Make sure you do not have `--bitbucket-username` set.
167+
1. Update SCM TYPE to `bitbucket` in your `ghorg/conf.yaml` or via cli flags
168+
161169
### osx default github/gitlab token used
162170

163171
> NOTE: cloning via https rather than ssh is the ghorg default, this is because a token must be present to retrieve the list of repos. However, if you run into trouble cloning via https and generally clone via ssh, try switching `--protocol ssh`

cmd/clone.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ func cloneFunc(cmd *cobra.Command, argz []string) {
140140
} else if os.Getenv("GHORG_SCM_TYPE") == "gitlab" {
141141
os.Setenv("GHORG_GITLAB_TOKEN", cmd.Flag("token").Value.String())
142142
} else if os.Getenv("GHORG_SCM_TYPE") == "bitbucket" {
143-
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", cmd.Flag("token").Value.String())
143+
if cmd.Flags().Changed("bitbucket-username") {
144+
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", cmd.Flag("token").Value.String())
145+
} else {
146+
os.Setenv("GHORG_BITBUCKET_OAUTH_TOKEN", cmd.Flag("token").Value.String())
147+
}
148+
144149
} else if os.Getenv("GHORG_SCM_TYPE") == "gitea" {
145150
os.Setenv("GHORG_GITEA_TOKEN", cmd.Flag("token").Value.String())
146151
}

cmd/root.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func InitConfig() {
169169
getOrSetDefaults("GHORG_GITLAB_TOKEN")
170170
getOrSetDefaults("GHORG_BITBUCKET_USERNAME")
171171
getOrSetDefaults("GHORG_BITBUCKET_APP_PASSWORD")
172+
getOrSetDefaults("GHORG_BITBUCKET_OAUTH_TOKEN")
172173
getOrSetDefaults("GHORG_SCM_BASE_URL")
173174
getOrSetDefaults("GHORG_PRESERVE_DIRECTORY_STRUCTURE")
174175
getOrSetDefaults("GHORG_OUTPUT_DIR")
@@ -196,7 +197,7 @@ func init() {
196197
cloneCmd.Flags().StringVar(&protocol, "protocol", "", "GHORG_CLONE_PROTOCOL - protocol to clone with, ssh or https, (default https)")
197198
cloneCmd.Flags().StringVarP(&path, "path", "p", "", "GHORG_ABSOLUTE_PATH_TO_CLONE_TO - absolute path the ghorg_* directory will be created. Must end with / (default $HOME/Desktop/ghorg)")
198199
cloneCmd.Flags().StringVarP(&branch, "branch", "b", "", "GHORG_BRANCH - branch left checked out for each repo cloned (default master)")
199-
cloneCmd.Flags().StringVarP(&token, "token", "t", "", "GHORG_GITHUB_TOKEN/GHORG_GITLAB_TOKEN/GHORG_GITEA_TOKEN/GHORG_BITBUCKET_APP_PASSWORD - scm token to clone with")
200+
cloneCmd.Flags().StringVarP(&token, "token", "t", "", "GHORG_GITHUB_TOKEN/GHORG_GITLAB_TOKEN/GHORG_GITEA_TOKEN/GHORG_BITBUCKET_APP_PASSWORD/GHORG_BITBUCKET_OAUTH_TOKEN - scm token to clone with")
200201
cloneCmd.Flags().StringVarP(&bitbucketUsername, "bitbucket-username", "", "", "GHORG_BITBUCKET_USERNAME - bitbucket only: username associated with the app password")
201202
cloneCmd.Flags().StringVarP(&scmType, "scm", "s", "", "GHORG_SCM_TYPE - type of scm used, github, gitlab, gitea or bitbucket (default github)")
202203
cloneCmd.Flags().StringVarP(&cloneType, "clone-type", "c", "", "GHORG_CLONE_TYPE - clone target type, user or org (default org)")

configs/configs.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func getOrSetGitLabToken() {
193193

194194
func getOrSetBitBucketToken() {
195195
var token string
196-
if isZero(os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")) || len(os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")) != 20 {
196+
if isZero(os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")) && isZero(os.Getenv("GHORG_BITBUCKET_OAUTH_TOKEN")) {
197197
if runtime.GOOS == "windows" {
198198
return
199199
}
@@ -205,7 +205,11 @@ func getOrSetBitBucketToken() {
205205

206206
token = strings.TrimSuffix(string(out), "\n")
207207

208-
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", token)
208+
if !isZero(os.Getenv("GHORG_BITBUCKET_USERNAME")) {
209+
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", token)
210+
} else {
211+
os.Setenv("GHORG_BITBUCKET_OAUTH_TOKEN", token)
212+
}
209213
}
210214
}
211215

@@ -231,10 +235,18 @@ func VerifyTokenSet() error {
231235

232236
if scmProvider == "bitbucket" {
233237
tokenLength = 20
234-
token = os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")
235-
if os.Getenv("GHORG_BITBUCKET_USERNAME") == "" {
238+
if os.Getenv("GHORG_BITBUCKET_USERNAME") == "" && len(os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")) == 20 {
236239
return ErrNoBitbucketUsername
237240
}
241+
242+
if isZero(os.Getenv("GHORG_BITBUCKET_USERNAME")) {
243+
// todo not sure how long this is so, so just make it pass for now
244+
tokenLength = 0
245+
token = ""
246+
} else {
247+
token = os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")
248+
}
249+
238250
}
239251

240252
if len(token) != tokenLength {

configs/configs_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestVerifyTokenSet(t *testing.T) {
3434
t.Run("When cloning bitbucket with no username", func(tt *testing.T) {
3535
os.Setenv("GHORG_SCM_TYPE", "bitbucket")
3636
os.Setenv("GHORG_BITBUCKET_USERNAME", "")
37+
os.Setenv("GHORG_BITBUCKET_APP_PASSWORD", "12345678912345678901")
3738
err := configs.VerifyTokenSet()
3839
if err != configs.ErrNoBitbucketUsername {
3940
tt.Errorf("Expected ErrNoBitbucketUsername, got: %v", err)

sample-conf.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ GHORG_GITEA_TOKEN:
4040
# |B|I|T|B|U|C|K|E|T| |S|P|E|C|I|F|I|C|
4141
# +-+-+-+-+-+-+-+-+-+ +-+-+-+-+-+-+-+-+
4242

43-
# Add your Bitbucket app password
43+
# When using this OAuth/PAT token, do not set the bitbucket username flag. Do not set both this value and the GHORG_BITBUCKET_APP_PASSWORD
44+
# https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html
45+
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
46+
GHORG_BITBUCKET_OAUTH_TOKEN:
47+
48+
# Add your Bitbucket app password. Do not set both this value and the GHORG_BITBUCKET_OAUTH_TOKEN
4449
# https://confluence.atlassian.com/bitbucket/app-passwords-828781300.html
4550
# flag (--token, -t) eg: --token=bGVhdmUgYSBjb21tZW50IG9uIGlzc3VlIDY2
4651
GHORG_BITBUCKET_APP_PASSWORD:
4752

53+
# Used with GHORG_BITBUCKET_APP_PASSWORD. Should not be set when using GHORG_BITBUCKET_OAUTH_TOKEN
4854
# flag (--bitbucket-username) eg: --bitbucket-username=user123
4955
GHORG_BITBUCKET_USERNAME:
5056

scm/bitbucket.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ func (c Bitbucket) GetUserRepos(targetUser string) ([]Repo, error) {
4848
func (_ Bitbucket) NewClient() (Client, error) {
4949
user := os.Getenv("GHORG_BITBUCKET_USERNAME")
5050
password := os.Getenv("GHORG_BITBUCKET_APP_PASSWORD")
51-
c := bitbucket.NewBasicAuth(user, password)
51+
oAuth := os.Getenv("GHORG_BITBUCKET_OAUTH")
52+
var c *bitbucket.Client
53+
54+
if oAuth != "" {
55+
c = bitbucket.NewOAuthbearerToken(oAuth)
56+
} else {
57+
c = bitbucket.NewBasicAuth(user, password)
58+
}
5259
return Bitbucket{c}, nil
5360
}
5461

0 commit comments

Comments
 (0)