-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for gitlab, azure dev and bitbucket
- Loading branch information
1 parent
8d64e45
commit 9471226
Showing
9 changed files
with
1,898 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/core" | ||
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git" | ||
) | ||
|
||
type AzureProvider struct { | ||
Org string | ||
} | ||
|
||
func (a *AzureProvider) GetRepos() ([]*Repo, bool, error) { | ||
|
||
connection := azuredevops.NewPatConnection("https://dev.azure.com/"+a.Org, os.Getenv("AZURE_TOKEN")) | ||
ctx := context.Background() | ||
gitClient, err := git.NewClient(ctx, connection) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to create core client: %w", err) | ||
} | ||
coreClient, err := core.NewClient(ctx, connection) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to create core client: %w", err) | ||
} | ||
|
||
projects, err := getAzureProjects(ctx, coreClient) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to list projects: %w", err) | ||
} | ||
|
||
var repos []*Repo | ||
for _, project := range projects { | ||
r, err := gitClient.GetRepositories(ctx, git.GetRepositoriesArgs{ | ||
Project: &project, | ||
}) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to list repos: %w", err) | ||
} | ||
|
||
for _, repo := range *r { | ||
repos = append(repos, &Repo{ | ||
Name: *repo.Name, | ||
URL: *repo.RemoteUrl, | ||
CloneURL: *repo.RemoteUrl, | ||
SSHURL: *repo.SshUrl, | ||
}) | ||
} | ||
} | ||
|
||
return repos, false, nil | ||
} | ||
|
||
func getAzureProjects(ctx context.Context, client core.Client) ([]string, error) { | ||
resp, err := client.GetProjects(ctx, core.GetProjectsArgs{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list projects: %w", err) | ||
} | ||
|
||
var projects []string | ||
if resp != nil { | ||
for _, project := range (*resp).Value { | ||
projects = append(projects, *project.Name) | ||
} | ||
if resp.ContinuationToken != "" { | ||
ct, err := strconv.Atoi(resp.ContinuationToken) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse continuation token: %w", err) | ||
} | ||
resp, err = client.GetProjects(ctx, core.GetProjectsArgs{ | ||
ContinuationToken: &ct, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to list projects: %w", err) | ||
} | ||
} else { | ||
resp = nil | ||
} | ||
} | ||
|
||
return projects, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ktrysmt/go-bitbucket" | ||
) | ||
|
||
type BitbucketProvider struct { | ||
Org string | ||
} | ||
|
||
func (b *BitbucketProvider) GetRepos() ([]*Repo, bool, error) { | ||
client := bitbucket.NewBasicAuth(os.Getenv("BITBUCKET_USERNAME"), os.Getenv("BITBUCKET_PASSWORD")) | ||
repos, err := client.Repositories.ListForAccount(&bitbucket.RepositoriesOptions{ | ||
Owner: b.Org, | ||
}) | ||
if err != nil { | ||
return nil, false, fmt.Errorf("failed to list repositories: %w", err) | ||
} | ||
|
||
var result []*Repo | ||
for _, repo := range repos.Items { | ||
result = append(result, &Repo{ | ||
Name: repo.Name, | ||
URL: getBitbucketLink(repo.Links, "html"), | ||
CloneURL: getBitbucketLink(repo.Links, "clone", "https"), | ||
SSHURL: getBitbucketLink(repo.Links, "clone", "ssh"), | ||
}) | ||
} | ||
|
||
return result, false, nil | ||
} | ||
|
||
func getBitbucketLink(links map[string]interface{}, p ...string) string { | ||
for name, link := range links { | ||
if name != p[0] { | ||
continue | ||
} | ||
if l, ok := link.(map[string]interface{}); ok { | ||
return l["href"].(string) | ||
} | ||
if l, ok := link.([]interface{}); ok { | ||
for _, ll := range l { | ||
if llm, ok := ll.(map[string]interface{}); ok { | ||
if llm["name"].(string) != p[1] { | ||
continue | ||
} | ||
return llm["href"].(string) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return "" | ||
} |
Oops, something went wrong.