Skip to content

Commit

Permalink
Handle empty repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeuffer committed Nov 12, 2024
1 parent 9935cf9 commit 9cece5b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
6 changes: 5 additions & 1 deletion applicationset/services/scm_provider/scm-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ func (g *ScmManagerProvider) ListRepos(ctx context.Context, cloneProtocol string

defaultBranch, err := g.client.GetDefaultBranch(scmmRepo.Namespace, scmmRepo.Name)
if err != nil {
return nil, err
if errors.Is(err, scmm.ErrEmptyRepository) || errors.Is(err, scmm.ErrNoDefaultBranchFound) {
continue
} else {
return nil, err
}
}

repos = append(repos, &Repository{
Expand Down
61 changes: 47 additions & 14 deletions applicationset/services/scm_provider/scm-manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
)

func scmManagerMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
func scmManagerMockHandler(t *testing.T, emptyRepo bool) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.RequestURI {
Expand Down Expand Up @@ -43,19 +43,39 @@ func scmManagerMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request
t.Fail()
}
case "/api/v2/repositories/test-argocd/pr-test/branches/":
if emptyRepo {
_, err := io.WriteString(w, `{
"_embedded": {
"branches": []
}
}`)
if err != nil {
t.Fail()
}
} else {
_, err := io.WriteString(w, `{
"_embedded": {
"branches": [{
"name": "main",
"defaultBranch": true,
"revision": "72687815ccba81ef014a96201cc2e846a68789d8",
"stale": false,
"lastCommitDate": "2022-04-05T14:29:51-04:00",
"lastCommitter": {
"name": "Eduard Heimbuch",
"mail": "[email protected]"
}
}]
}
}`)
if err != nil {
t.Fail()
}
}
case "/api/v2/repositories/test-argocd/empty-test/branches/":
_, err := io.WriteString(w, `{
"_embedded": {
"branches": [{
"name": "main",
"defaultBranch": true,
"revision": "72687815ccba81ef014a96201cc2e846a68789d8",
"stale": false,
"lastCommitDate": "2022-04-05T14:29:51-04:00",
"lastCommitter": {
"name": "Eduard Heimbuch",
"mail": "[email protected]"
}
}]
"branches": []
}
}`)
if err != nil {
Expand Down Expand Up @@ -135,7 +155,7 @@ func TestScmManagerListRepos(t *testing.T) {
},
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scmManagerMockHandler(t)(w, r)
scmManagerMockHandler(t, false)(w, r)
}))
defer ts.Close()
for _, c := range cases {
Expand Down Expand Up @@ -164,9 +184,22 @@ func TestScmManagerListRepos(t *testing.T) {
}
}

func TestScmManagerListEmptyRepos(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scmManagerMockHandler(t, true)(w, r)
}))
defer ts.Close()
t.Run("empty repository", func(t *testing.T) {
provider, _ := NewScmManagerProvider(context.Background(), "", ts.URL, false, false, "", nil)
rawRepos, err := ListRepos(context.Background(), provider, make([]v1alpha1.SCMProviderGeneratorFilter, 0), "https")
require.NoError(t, err)
assert.Empty(t, rawRepos)
})
}

func TestScmManagerHasPath(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
scmManagerMockHandler(t)(w, r)
scmManagerMockHandler(t, false)(w, r)
}))
defer ts.Close()
host, _ := NewScmManagerProvider(context.Background(), "", ts.URL, false, false, "", nil)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ require (
github.com/r3labs/diff v1.1.0
github.com/redis/go-redis/v9 v9.6.1
github.com/robfig/cron/v3 v3.0.1
github.com/scm-manager/goscm v0.0.6
github.com/scm-manager/goscm v0.0.7
github.com/sirupsen/logrus v1.9.3
github.com/skratchdot/open-golang v0.0.0-20160302144031-75fb7ed4208c
github.com/soheilhy/cmux v0.1.5
Expand Down Expand Up @@ -338,3 +338,5 @@ replace (
k8s.io/sample-cli-plugin => k8s.io/sample-cli-plugin v0.29.6
k8s.io/sample-controller => k8s.io/sample-controller v0.29.6
)

replace github.com/scm-manager/goscm => /home/rene/development/cloudogu/goscm
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,6 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/scm-manager/goscm v0.0.6 h1:CD3ckqOP2U9k8U1jCIvT01kWBkkGRGn2y9jkCQHVsoU=
github.com/scm-manager/goscm v0.0.6/go.mod h1:1pYGtYGb1pHUh5eQQ3bwUIdMh9ahcFrZh2ti4wV7rCc=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
Expand Down

0 comments on commit 9cece5b

Please sign in to comment.