Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
Signed-off-by: Jamie Magee <[email protected]>
  • Loading branch information
JamieMagee committed Oct 18, 2024
1 parent a9f1986 commit ebd485a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 31 deletions.
2 changes: 1 addition & 1 deletion clients/azuredevopsrepo/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"

"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git"

"github.com/ossf/scorecard/v5/clients"
)

Expand Down Expand Up @@ -62,7 +63,6 @@ func (handler *branchesHandler) setup() error {
handler.errSetup = nil
})
return handler.errSetup

}

func (handler *branchesHandler) getDefaultBranch() (*clients.BranchRef, error) {
Expand Down
6 changes: 4 additions & 2 deletions clients/azuredevopsrepo/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ import (
)

func TestGetDefaultBranch(t *testing.T) {
t.Parallel()
tests := []struct {
name string
setupMock func() fnQueryBranch
expectedError bool
name string
expectedName string
expectedError bool
}{
{
name: "successful branch retrieval",
Expand All @@ -53,6 +54,7 @@ func TestGetDefaultBranch(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
handler := &branchesHandler{
queryBranch: tt.setupMock(),
once: new(sync.Once),
Expand Down
14 changes: 8 additions & 6 deletions clients/azuredevopsrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ import (

"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git"

"github.com/ossf/scorecard/v5/clients"
)

var (
_ clients.RepoClient = &Client{}
errInputRepoType = errors.New("input repo should be of type azuredevopsrepo.Repo")
_ clients.RepoClient = &Client{}
errInputRepoType = errors.New("input repo should be of type azuredevopsrepo.Repo")
errDefaultBranchNotFound = errors.New("default branch not found")
)

type Client struct {
repourl *Repo
azdoClient git.Client
once sync.Once
ctx context.Context
repourl *Repo
branches *branchesHandler
commits *commitsHandler
ctx context.Context
commitDepth int
once sync.Once
}

func (c *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth int) error {
Expand Down Expand Up @@ -135,7 +137,7 @@ func (c *Client) GetDefaultBranchName() (string, error) {
return c.repourl.defaultBranch, nil
}

return "", fmt.Errorf("%s", "default branch name is empty")
return "", fmt.Errorf("%w", errDefaultBranchNotFound)
}

func (c *Client) GetDefaultBranch() (*clients.BranchRef, error) {
Expand Down
6 changes: 4 additions & 2 deletions clients/azuredevopsrepo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (

type mockGitClient struct {
git.Client
isDisabled bool
err error
isDisabled bool
}

func (m *mockGitClient) GetRepository(ctx context.Context, args git.GetRepositoryArgs) (*git.GitRepository, error) {
Expand All @@ -36,10 +36,11 @@ func (m *mockGitClient) GetRepository(ctx context.Context, args git.GetRepositor
}

func TestIsArchived(t *testing.T) {
t.Parallel()
tests := []struct {
err error
name string
isDisabled bool
err error
want bool
wantErr bool
}{
Expand All @@ -65,6 +66,7 @@ func TestIsArchived(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
client := &Client{
azdoClient: &mockGitClient{
isDisabled: tt.isDisabled,
Expand Down
22 changes: 14 additions & 8 deletions clients/azuredevopsrepo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,28 @@ package azuredevopsrepo

import (
"context"
"errors"
"fmt"
"sync"

"github.com/microsoft/azure-devops-go-api/azuredevops/v7/git"

"github.com/ossf/scorecard/v5/clients"
)

var errMultiplePullRequests = errors.New("expected 1 pull request for commit, got multiple")

type commitsHandler struct {
gitClient git.Client
ctx context.Context
once *sync.Once
errSetup error
once *sync.Once
repourl *Repo
commitsRaw *[]git.GitCommitRef
pullRequestsRaw *git.GitPullRequestQuery
commitDepth int
getCommits fnGetCommits
getPullRequestQuery fnGetPullRequestQuery
commitDepth int
}

func (handler *commitsHandler) init(ctx context.Context, repourl *Repo, commitDepth int) {
Expand Down Expand Up @@ -81,8 +85,8 @@ func (handler *commitsHandler) setup() error {
}

commitIds := make([]string, len(*commits))
for i, commit := range *commits {
commitIds[i] = *commit.CommitId
for i := range *commits {
commitIds[i] = *(*commits)[i].CommitId
}

pullRequestQuery := git.GetPullRequestQueryArgs{
Expand Down Expand Up @@ -117,7 +121,8 @@ func (handler *commitsHandler) listCommits() ([]clients.Commit, error) {
}

commits := make([]clients.Commit, len(*handler.commitsRaw))
for i, commit := range *handler.commitsRaw {
for i := range *handler.commitsRaw {
commit := &(*handler.commitsRaw)[i]
commits[i] = clients.Commit{
SHA: *commit.CommitId,
Message: *commit.Comment,
Expand All @@ -134,13 +139,14 @@ func (handler *commitsHandler) listCommits() ([]clients.Commit, error) {
return nil, fmt.Errorf("error during commitsHandler.listPullRequests: %w", err)
}

for i, commit := range commits {
for i := range commits {
commit := &commits[i]
associatedPullRequest, ok := pullRequests[commit.SHA]
if !ok {
continue
}

commits[i].AssociatedMergeRequest = associatedPullRequest
commit.AssociatedMergeRequest = associatedPullRequest
}

return commits, nil
Expand All @@ -159,7 +165,7 @@ func (handler *commitsHandler) listPullRequests() (map[string]clients.PullReques
}

if len(azdoPullRequests) > 1 {
return nil, fmt.Errorf("expected 1 pull request for commit %s, got %d", commit, len(azdoPullRequests))
return nil, errMultiplePullRequests
}

azdoPullRequest := azdoPullRequests[0]
Expand Down
34 changes: 22 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,28 @@ func printCheckResults(enabledChecks checker.CheckNameToFnMap) {
func makeRepo(uri string) (clients.Repo, error) {
var repo clients.Repo
var errGitHub, errGitLab, errAzureDevOps error
if repo, errGitHub = githubrepo.MakeGithubRepo(uri); errGitHub != nil {
if repo, errGitLab = gitlabrepo.MakeGitlabRepo(uri); errGitLab != nil {
_, experimental := os.LookupEnv("SCORECARD_EXPERIMENTAL")
if experimental {
repo, errAzureDevOps = azuredevopsrepo.MakeAzureDevOpsRepo(uri)
if errAzureDevOps != nil {
return nil, fmt.Errorf("unable to parse as github, gitlab, or azuredevops: %w", errors.Join(errGitHub, errGitLab, errAzureDevOps))
}
} else {
return nil, fmt.Errorf("unable to parse as github or gitlab: %w", errors.Join(errGitHub, errGitLab))
}
var compositeErr error

repo, errGitHub = githubrepo.MakeGithubRepo(uri)
if errGitHub == nil {
return repo, nil
}
compositeErr = errors.Join(compositeErr, errGitHub)

repo, errGitLab = gitlabrepo.MakeGitlabRepo(uri)
if errGitLab == nil {
return repo, nil
}
compositeErr = errors.Join(compositeErr, errGitLab)

_, experimental := os.LookupEnv("SCORECARD_EXPERIMENTAL")
if experimental {
repo, errAzureDevOps = azuredevopsrepo.MakeAzureDevOpsRepo(uri)
if errAzureDevOps == nil {
return repo, nil
}
compositeErr = errors.Join(compositeErr, errAzureDevOps)
}
return repo, nil

return nil, fmt.Errorf("unable to parse as github, gitlab, or azuredevops: %w", compositeErr)
}

0 comments on commit ebd485a

Please sign in to comment.