-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix/fix-oci-private-repos
- Loading branch information
Showing
159 changed files
with
1,231 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package pull_request | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"os" | ||
|
||
"code.gitea.io/sdk/gitea" | ||
) | ||
|
||
type GiteaService struct { | ||
client *gitea.Client | ||
owner string | ||
repo string | ||
} | ||
|
||
var _ PullRequestService = (*GiteaService)(nil) | ||
|
||
func NewGiteaService(ctx context.Context, token, url, owner, repo string, insecure bool) (PullRequestService, error) { | ||
if token == "" { | ||
token = os.Getenv("GITEA_TOKEN") | ||
} | ||
httpClient := &http.Client{} | ||
if insecure { | ||
cookieJar, _ := cookiejar.New(nil) | ||
|
||
httpClient = &http.Client{ | ||
Jar: cookieJar, | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}} | ||
} | ||
client, err := gitea.NewClient(url, gitea.SetToken(token), gitea.SetHTTPClient(httpClient)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &GiteaService{ | ||
client: client, | ||
owner: owner, | ||
repo: repo, | ||
}, nil | ||
} | ||
|
||
func (g *GiteaService) List(ctx context.Context) ([]*PullRequest, error) { | ||
opts := gitea.ListPullRequestsOptions{ | ||
State: gitea.StateOpen, | ||
} | ||
prs, _, err := g.client.ListRepoPullRequests(g.owner, g.repo, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
list := []*PullRequest{} | ||
for _, pr := range prs { | ||
list = append(list, &PullRequest{ | ||
Number: int(pr.Index), | ||
Branch: pr.Head.Ref, | ||
HeadSHA: pr.Head.Sha, | ||
}) | ||
} | ||
return list, 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,19 @@ | ||
package pull_request | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGiteaList(t *testing.T) { | ||
host, err := NewGiteaService(context.Background(), "", "https://gitea.com", "test-argocd", "pr-test", false) | ||
assert.Nil(t, err) | ||
prs, err := host.List(context.Background()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, len(prs), 1) | ||
assert.Equal(t, prs[0].Number, 1) | ||
assert.Equal(t, prs[0].Branch, "test") | ||
assert.Equal(t, prs[0].HeadSHA, "7bbaf62d92ddfafd9cc8b340c619abaec32bc09f") | ||
} |
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,136 @@ | ||
package scm_provider | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"os" | ||
|
||
"code.gitea.io/sdk/gitea" | ||
) | ||
|
||
type GiteaProvider struct { | ||
client *gitea.Client | ||
owner string | ||
allBranches bool | ||
} | ||
|
||
var _ SCMProviderService = &GiteaProvider{} | ||
|
||
func NewGiteaProvider(ctx context.Context, owner, token, url string, allBranches, insecure bool) (*GiteaProvider, error) { | ||
if token == "" { | ||
token = os.Getenv("GITEA_TOKEN") | ||
} | ||
httpClient := &http.Client{} | ||
if insecure { | ||
cookieJar, _ := cookiejar.New(nil) | ||
|
||
httpClient = &http.Client{ | ||
Jar: cookieJar, | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}} | ||
} | ||
client, err := gitea.NewClient(url, gitea.SetToken(token), gitea.SetHTTPClient(httpClient)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &GiteaProvider{ | ||
client: client, | ||
owner: owner, | ||
allBranches: allBranches, | ||
}, nil | ||
} | ||
|
||
func (g *GiteaProvider) GetBranches(ctx context.Context, repo *Repository) ([]*Repository, error) { | ||
if !g.allBranches { | ||
branch, _, err := g.client.GetRepoBranch(g.owner, repo.Repository, repo.Branch) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []*Repository{ | ||
{ | ||
Organization: repo.Organization, | ||
Repository: repo.Repository, | ||
Branch: repo.Branch, | ||
URL: repo.URL, | ||
SHA: branch.Commit.ID, | ||
Labels: repo.Labels, | ||
RepositoryId: repo.RepositoryId, | ||
}, | ||
}, nil | ||
} | ||
repos := []*Repository{} | ||
opts := gitea.ListRepoBranchesOptions{} | ||
branches, _, err := g.client.ListRepoBranches(g.owner, repo.Repository, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, branch := range branches { | ||
repos = append(repos, &Repository{ | ||
Organization: repo.Organization, | ||
Repository: repo.Repository, | ||
Branch: branch.Name, | ||
URL: repo.URL, | ||
SHA: branch.Commit.ID, | ||
Labels: repo.Labels, | ||
RepositoryId: repo.RepositoryId, | ||
}) | ||
} | ||
return repos, nil | ||
} | ||
|
||
func (g *GiteaProvider) ListRepos(ctx context.Context, cloneProtocol string) ([]*Repository, error) { | ||
repos := []*Repository{} | ||
repoOpts := gitea.ListOrgReposOptions{} | ||
giteaRepos, _, err := g.client.ListOrgRepos(g.owner, repoOpts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, repo := range giteaRepos { | ||
var url string | ||
switch cloneProtocol { | ||
// Default to SSH if unspecified (i.e. if ""). | ||
case "", "ssh": | ||
url = repo.SSHURL | ||
case "https": | ||
url = repo.HTMLURL | ||
default: | ||
return nil, fmt.Errorf("unknown clone protocol for GitHub %v", cloneProtocol) | ||
} | ||
labelOpts := gitea.ListLabelsOptions{} | ||
giteaLabels, _, err := g.client.ListRepoLabels(g.owner, repo.Name, labelOpts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
labels := []string{} | ||
for _, label := range giteaLabels { | ||
labels = append(labels, label.Name) | ||
} | ||
repos = append(repos, &Repository{ | ||
Organization: g.owner, | ||
Repository: repo.Name, | ||
Branch: repo.DefaultBranch, | ||
URL: url, | ||
Labels: labels, | ||
RepositoryId: int(repo.ID), | ||
}) | ||
} | ||
return repos, nil | ||
} | ||
|
||
func (g *GiteaProvider) RepoHasPath(ctx context.Context, repo *Repository, path string) (bool, error) { | ||
_, resp, err := g.client.GetContents(repo.Organization, repo.Repository, repo.Branch, path) | ||
if resp != nil && resp.StatusCode == 404 { | ||
return false, nil | ||
} | ||
if fmt.Sprint(err) == "expect file, got directory" { | ||
return true, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
return true, 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,98 @@ | ||
package scm_provider | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1" | ||
) | ||
|
||
func TestGiteaListRepos(t *testing.T) { | ||
cases := []struct { | ||
name, proto, url string | ||
hasError, allBranches, includeSubgroups bool | ||
branches []string | ||
filters []v1alpha1.SCMProviderGeneratorFilter | ||
}{ | ||
{ | ||
name: "blank protocol", | ||
allBranches: false, | ||
url: "git@gitea.com:gitea/go-sdk.git", | ||
branches: []string{"master"}, | ||
}, | ||
{ | ||
name: "ssh protocol", | ||
allBranches: false, | ||
proto: "ssh", | ||
url: "git@gitea.com:gitea/go-sdk.git", | ||
}, | ||
{ | ||
name: "https protocol", | ||
allBranches: false, | ||
proto: "https", | ||
url: "https://gitea.com/gitea/go-sdk", | ||
}, | ||
{ | ||
name: "other protocol", | ||
allBranches: false, | ||
proto: "other", | ||
hasError: true, | ||
}, | ||
{ | ||
name: "all branches", | ||
allBranches: true, | ||
url: "git@gitea.com:gitea/go-sdk.git", | ||
branches: []string{"master", "release/v0.11", "release/v0.12", "release/v0.13", "release/v0.14", "release/v0.15"}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
provider, _ := NewGiteaProvider(context.Background(), "gitea", "", "https://gitea.com/", c.allBranches, false) | ||
rawRepos, err := ListRepos(context.Background(), provider, c.filters, c.proto) | ||
if c.hasError { | ||
assert.NotNil(t, err) | ||
} else { | ||
checkRateLimit(t, err) | ||
assert.Nil(t, err) | ||
// Just check that this one project shows up. Not a great test but better thing nothing? | ||
repos := []*Repository{} | ||
branches := []string{} | ||
for _, r := range rawRepos { | ||
if r.Repository == "go-sdk" { | ||
repos = append(repos, r) | ||
branches = append(branches, r.Branch) | ||
} | ||
} | ||
assert.NotEmpty(t, repos) | ||
assert.Equal(t, c.url, repos[0].URL) | ||
for _, b := range c.branches { | ||
assert.Contains(t, branches, b) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGiteaHasPath(t *testing.T) { | ||
host, _ := NewGiteaProvider(context.Background(), "gitea", "", "https://gitea.com/", false, false) | ||
repo := &Repository{ | ||
Organization: "gitea", | ||
Repository: "go-sdk", | ||
Branch: "master", | ||
} | ||
ok, err := host.RepoHasPath(context.Background(), repo, "README.md") | ||
assert.Nil(t, err) | ||
assert.True(t, ok) | ||
|
||
// directory | ||
ok, err = host.RepoHasPath(context.Background(), repo, "gitea") | ||
assert.Nil(t, err) | ||
assert.True(t, ok) | ||
|
||
ok, err = host.RepoHasPath(context.Background(), repo, "notathing") | ||
assert.Nil(t, err) | ||
assert.False(t, ok) | ||
} |
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
Oops, something went wrong.