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

fix: Add skip to test if branch do not exists, instead of failing #9204

Closed
Closed
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
10 changes: 8 additions & 2 deletions applicationset/services/scm_provider/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func NewGiteaProvider(ctx context.Context, owner, token, url string, allBranches

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)
branch, status, err := g.client.GetRepoBranch(g.owner, repo.Repository, repo.Branch)
if status.StatusCode == 404 {
return nil, BranchDoNotExist
}
if err != nil {
return nil, err
}
Expand All @@ -64,7 +67,10 @@ func (g *GiteaProvider) GetBranches(ctx context.Context, repo *Repository) ([]*R
}
repos := []*Repository{}
opts := gitea.ListRepoBranchesOptions{}
branches, _, err := g.client.ListRepoBranches(g.owner, repo.Repository, opts)
branches, status, err := g.client.ListRepoBranches(g.owner, repo.Repository, opts)
if status.StatusCode == 404 {
return nil, BranchDoNotExist
}
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions applicationset/services/scm_provider/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package scm_provider

import (
"context"
"errors"
"regexp"
)

var BranchDoNotExist = errors.New("Branch Not Found")

// An abstract repository from an API provider.
type Repository struct {
Organization string
Expand Down
8 changes: 8 additions & 0 deletions applicationset/services/scm_provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scm_provider

import (
"context"
"errors"
"fmt"
"regexp"

Expand Down Expand Up @@ -126,6 +127,9 @@ func getBranches(ctx context.Context, provider SCMProviderService, repos []*Repo
for _, repo := range repos {
reposFilled, err := provider.GetBranches(ctx, repo)
if err != nil {
if checkBranchDoNotExist(err) {
continue
}
Comment on lines +130 to +132
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the test, I don't think this is the correct fix. We're hitting an error because the gitea API is returning main as the default branch name for this repo. But that repo doesn't have a main branch. It looks like a bug in the gitea API.

In the case of a 404 we could fall back to listing all branches and then picking the 1st one if there's only one. I'll put up another PR based on this one.

return nil, err
}
reposWithBranches = append(reposWithBranches, reposFilled...)
Expand Down Expand Up @@ -165,3 +169,7 @@ func getApplicableFilters(filters []*Filter) map[FilterType][]*Filter {
}
return filterMap
}

func checkBranchDoNotExist(err error) bool {
leoluz marked this conversation as resolved.
Show resolved Hide resolved
return errors.Is(err, BranchDoNotExist)
}
38 changes: 38 additions & 0 deletions applicationset/services/scm_provider/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1"
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/applicationset/v1alpha1"
)

Expand Down Expand Up @@ -290,3 +291,40 @@ func TestApplicableFilterMap(t *testing.T) {
assert.Len(t, filterMap[FilterTypeRepo], 2)
assert.Len(t, filterMap[FilterTypeBranch], 3)
}

func TestRepoBranchDoNotExist(t *testing.T) {
// Branch doesn't exists
valid := struct {
name, proto, url string
hasError, allBranches, includeSubgroups bool
branches []string
filters []v1alpha1.SCMProviderGeneratorFilter
}{
name: "valid repo",
allBranches: false,
url: "git@gitea.com:gitea/go-sdk.git",
branches: []string{"specificthis"},
}
t.Run(valid.name, func(t *testing.T) {
provider, _ := NewGiteaProvider(context.Background(), "gitea", "", "https://gitea.com/", valid.allBranches, false)
_, err := ListRepos(context.Background(), provider, valid.filters, valid.proto)
assert.Nilf(t, err, "Expected No errors")
})

invalid := struct {
name, proto, url string
hasError, allBranches, includeSubgroups bool
branches []string
filters []v1alpha1.SCMProviderGeneratorFilter
}{
name: "invalid_repo",
allBranches: false,
proto: "https",
url: "https://gitea.com/gitea2/go-sdk",
}
t.Run(invalid.name, func(t *testing.T) {
provider, _ := NewGiteaProvider(context.Background(), "gitea2", "", "https://gitea.com/", invalid.allBranches, false)
_, err := ListRepos(context.Background(), provider, invalid.filters, invalid.proto)
assert.NotNilf(t, err, "Expected Error")
})
}