Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed token and user, get repositories from workspaces too #257

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
repos := []types.Repo{}
for _, repo := range conf.Source.BitBucket {
ran = true
client := bitbucket.NewBasicAuth(repo.Username, repo.Password)
repo.Token = repo.GetToken()
if repo.Token != "" && repo.Password == "" {
repo.Password = repo.Token
}
if repo.User == "" {
repo.User = repo.Username
}

if repo.Username == "" && repo.User != "" {
repo.Username = repo.User
}

include := types.GetMap(repo.Include)
exclude := types.GetMap(repo.Exclude)
includeorgs := types.GetMap(repo.IncludeOrgs)
excludeorgs := types.GetMap(repo.ExcludeOrgs)

client := bitbucket.NewBasicAuth(repo.Username, repo.Password)

if repo.URL == "" {
repo.URL = bitbucket.DEFAULT_BITBUCKET_API_BASE_URL
sub = logger.CreateSubLogger("stage", "bitbucket", "url", repo.URL)
Expand Down Expand Up @@ -55,15 +69,47 @@ func Get(conf *types.Conf) ([]types.Repo, bool) {
continue
}

include := types.GetMap(repo.Include)
exclude := types.GetMap(repo.Exclude)
if repo.Token != "" {
workspaces, err := client.Workspaces.List()
if err != nil {
sub.Error().
Msg(err.Error())
} else {
for _, workspace := range workspaces.Workspaces {
if workspace.Slug != repo.User {
if len(includeorgs) > 0 {
if !includeorgs[workspace.Slug] {
continue
}
}
if len(excludeorgs) > 0 {
if excludeorgs[workspace.Slug] {
continue
}
}
workspacerepos, err := client.Repositories.ListForAccount(&bitbucket.RepositoriesOptions{Owner: workspace.Slug})
if err != nil {
sub.Error().
Msg(err.Error())
} else {
repositories.Items = append(repositories.Items, workspacerepos.Items...)
}

}
}
}
}

for _, r := range repositories.Items {
sub.Debug().Msg(r.Links["clone"].([]interface{})[0].(map[string]interface{})["href"].(string))
user := repo.User
if r.Owner != nil {
if _, ok := r.Owner["nickname"]; ok {
user = r.Owner["nickname"].(string)
if _, ok := r.Owner["username"]; ok {
user = r.Owner["username"].(string)
} else {
if _, ok := r.Owner["nickname"]; ok {
user = r.Owner["nickname"].(string)
}
}
}

Expand Down
10 changes: 9 additions & 1 deletion conf.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ source:
excludeforks: true # exclude forked repositories
bitbucket:
- user: some-user # the user you want to clone the repositories from.
- token: some-token
# token_file: token.txt # alternatively, specify token in a file
url: http(s)://url-to-bitbucket # if empty, it uses https://bitbucket.org
username: your-user # user is used to clone the repo with
password: your-password
Expand All @@ -148,8 +150,14 @@ source:
exclude: # this excludes the repos foo and bar
- foo
- bar
include:
include: # this includes the repo "foobar"
- foobar
excludeorgs: # this excludes repos from the workpaces "foo" and "bar"
- foo
- bar
includeorgs: # this includes repos from the workspaces "foo1" and "bar1"
- foo1
- bar1
filter:
lastactivity: 1y # only clone repos which had activity during the last year
onedev:
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ require (
github.com/skeema/knownhosts v1.3.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)
Loading
Loading