Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee committed Oct 12, 2024
1 parent c8937a4 commit 2208766
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 71 deletions.
6 changes: 4 additions & 2 deletions clients/azuredevopsrepo/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ import (

type branchesHandler struct {
gitClient git.Client
ctx context.Context
once *sync.Once
errSetup error
repourl *Repo
defaultBranchRef *clients.BranchRef
queryBranch fnQueryBranch
}

func (handler *branchesHandler) init(repourl *Repo) {
func (handler *branchesHandler) init(ctx context.Context, repourl *Repo) {
handler.ctx = ctx
handler.repourl = repourl
handler.errSetup = nil
handler.once = new(sync.Once)
Expand All @@ -45,7 +47,7 @@ type (

func (handler *branchesHandler) setup() error {
handler.once.Do(func() {
branch, err := handler.queryBranch(context.Background(), git.GetBranchArgs{
branch, err := handler.queryBranch(handler.ctx, git.GetBranchArgs{
RepositoryId: &handler.repourl.ID,
Name: &handler.repourl.defaultBranch,
})
Expand Down
113 changes: 54 additions & 59 deletions clients/azuredevopsrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"time"
Expand All @@ -43,13 +42,13 @@ type Client struct {
commitDepth int
}

func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth int) error {
func (c *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitDepth int) error {
azdoRepo, ok := inputRepo.(*Repo)
if !ok {
return fmt.Errorf("%w: %v", errInputRepoType, inputRepo)
}

repo, err := client.azdoClient.GetRepository(context.Background(), git.GetRepositoryArgs{
repo, err := c.azdoClient.GetRepository(c.ctx, git.GetRepositoryArgs{
Project: &azdoRepo.project,
RepositoryId: &azdoRepo.name,
})
Expand All @@ -58,14 +57,14 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
}

if commitDepth <= 0 {
client.commitDepth = 30 // default
c.commitDepth = 30 // default
} else {
client.commitDepth = commitDepth
c.commitDepth = commitDepth
}

branch, _ := strings.CutPrefix(*repo.DefaultBranch, "refs/heads/")
branch := strings.TrimPrefix(*repo.DefaultBranch, "refs/heads/")

client.repourl = &Repo{
c.repourl = &Repo{
scheme: azdoRepo.scheme,
host: azdoRepo.host,
organization: azdoRepo.organization,
Expand All @@ -76,111 +75,111 @@ func (client *Client) InitRepo(inputRepo clients.Repo, commitSHA string, commitD
commitSHA: commitSHA,
}

client.branches.init(client.repourl)
c.branches.init(c.ctx, c.repourl)

client.commits.init(client.repourl, client.commitDepth)
c.commits.init(c.ctx, c.repourl, c.commitDepth)

return nil
}

func (client *Client) URI() string {
return fmt.Sprintf("%s/%s/_git/%s", client.repourl.host, client.repourl.organization, client.repourl.project)
func (c *Client) URI() string {
return fmt.Sprintf("%s/%s", c.repourl.host, c.repourl.Path())
}

func (client *Client) IsArchived() (bool, error) {
repo, err := client.azdoClient.GetRepository(context.Background(), git.GetRepositoryArgs{RepositoryId: &client.repourl.ID})
func (c *Client) IsArchived() (bool, error) {
repo, err := c.azdoClient.GetRepository(c.ctx, git.GetRepositoryArgs{RepositoryId: &c.repourl.ID})
if err != nil {
return false, fmt.Errorf("could not get repository with error: %w", err)
}

return *repo.IsDisabled, nil
}

func (client *Client) ListFiles(predicate func(string) (bool, error)) ([]string, error) {
return []string{}, nil
func (c *Client) ListFiles(predicate func(string) (bool, error)) ([]string, error) {
return []string{}, clients.ErrUnsupportedFeature
}

func (client *Client) LocalPath() (string, error) {
return "", nil
func (c *Client) LocalPath() (string, error) {
return "", clients.ErrUnsupportedFeature
}

func (client *Client) GetFileReader(filename string) (io.ReadCloser, error) {
return nil, nil
func (c *Client) GetFileReader(filename string) (io.ReadCloser, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) GetBranch(branch string) (*clients.BranchRef, error) {
return nil, nil
func (c *Client) GetBranch(branch string) (*clients.BranchRef, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) GetCreatedAt() (time.Time, error) {
return time.Time{}, nil
func (c *Client) GetCreatedAt() (time.Time, error) {
return time.Time{}, clients.ErrUnsupportedFeature
}

func (client *Client) GetDefaultBranchName() (string, error) {
if len(client.repourl.defaultBranch) > 0 {
return client.repourl.defaultBranch, nil
func (c *Client) GetDefaultBranchName() (string, error) {
if len(c.repourl.defaultBranch) > 0 {
return c.repourl.defaultBranch, nil
}

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

Check failure on line 123 in clients/azuredevopsrepo/client.go

View workflow job for this annotation

GitHub Actions / check-linter

do not define dynamic errors, use wrapped static errors instead: "fmt.Errorf(\"%s\", \"default branch name is empty\")" (err113)
}

func (client *Client) GetDefaultBranch() (*clients.BranchRef, error) {
return client.branches.getDefaultBranch()
func (c *Client) GetDefaultBranch() (*clients.BranchRef, error) {
return c.branches.getDefaultBranch()
}

func (client *Client) GetOrgRepoClient(context.Context) (clients.RepoClient, error) {
return nil, fmt.Errorf("GetOrgRepoClient (GitLab): %w", clients.ErrUnsupportedFeature)
func (c *Client) GetOrgRepoClient(context.Context) (clients.RepoClient, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListCommits() ([]clients.Commit, error) {
return client.commits.listCommits()
func (c *Client) ListCommits() ([]clients.Commit, error) {
return c.commits.listCommits()
}

func (client *Client) ListIssues() ([]clients.Issue, error) {
return nil, nil
func (c *Client) ListIssues() ([]clients.Issue, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListLicenses() ([]clients.License, error) {
return nil, nil
func (c *Client) ListLicenses() ([]clients.License, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListReleases() ([]clients.Release, error) {
return nil, nil
func (c *Client) ListReleases() ([]clients.Release, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListContributors() ([]clients.User, error) {
return nil, nil
func (c *Client) ListContributors() ([]clients.User, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListSuccessfulWorkflowRuns(filename string) ([]clients.WorkflowRun, error) {
return nil, nil
func (c *Client) ListSuccessfulWorkflowRuns(filename string) ([]clients.WorkflowRun, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
return nil, nil
func (c *Client) ListCheckRunsForRef(ref string) ([]clients.CheckRun, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListStatuses(ref string) ([]clients.Status, error) {
return nil, nil
func (c *Client) ListStatuses(ref string) ([]clients.Status, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListWebhooks() ([]clients.Webhook, error) {
return nil, nil
func (c *Client) ListWebhooks() ([]clients.Webhook, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) ListProgrammingLanguages() ([]clients.Language, error) {
return nil, nil
func (c *Client) ListProgrammingLanguages() ([]clients.Language, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) Search(request clients.SearchRequest) (clients.SearchResponse, error) {
return clients.SearchResponse{}, nil
func (c *Client) Search(request clients.SearchRequest) (clients.SearchResponse, error) {
return clients.SearchResponse{}, clients.ErrUnsupportedFeature
}

func (client *Client) SearchCommits(request clients.SearchCommitsOptions) ([]clients.Commit, error) {
return nil, nil
func (c *Client) SearchCommits(request clients.SearchCommitsOptions) ([]clients.Commit, error) {
return nil, clients.ErrUnsupportedFeature
}

func (client *Client) Close() error {
func (c *Client) Close() error {
return nil
}

Expand Down Expand Up @@ -210,7 +209,3 @@ func CreateAzureDevOpsClientWithToken(ctx context.Context, token string, repo cl
},
}, nil
}

func CreateOssFuzzRepoClient(ctx context.Context, logger *log.Logger) (clients.RepoClient, error) {
return nil, fmt.Errorf("%w, oss fuzz currently only supported for github repos", clients.ErrUnsupportedFeature)
}
26 changes: 19 additions & 7 deletions clients/azuredevopsrepo/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

type commitsHandler struct {

Check failure on line 26 in clients/azuredevopsrepo/commits.go

View workflow job for this annotation

GitHub Actions / check-linter

fieldalignment: struct with 104 pointer bytes could be 96 (govet)
gitClient git.Client
ctx context.Context
once *sync.Once
errSetup error
repourl *Repo
Expand All @@ -35,7 +36,8 @@ type commitsHandler struct {
getPullRequestQuery fnGetPullRequestQuery
}

func (handler *commitsHandler) init(repourl *Repo, commitDepth int) {
func (handler *commitsHandler) init(ctx context.Context, repourl *Repo, commitDepth int) {
handler.ctx = ctx
handler.repourl = repourl
handler.errSetup = nil
handler.once = new(sync.Once)
Expand All @@ -51,18 +53,28 @@ type (

func (handler *commitsHandler) setup() error {
handler.once.Do(func() {
var itemVersion git.GitVersionDescriptor
if handler.repourl.commitSHA == "HEAD" {
itemVersion = git.GitVersionDescriptor{
VersionType: &git.GitVersionTypeValues.Branch,
Version: &handler.repourl.defaultBranch,
}
} else {
itemVersion = git.GitVersionDescriptor{
VersionType: &git.GitVersionTypeValues.Commit,
Version: &handler.repourl.commitSHA,
}
}

opt := git.GetCommitsArgs{
RepositoryId: &handler.repourl.ID,
Top: &handler.commitDepth,
SearchCriteria: &git.GitQueryCommitsCriteria{
ItemVersion: &git.GitVersionDescriptor{
VersionType: &git.GitVersionTypeValues.Branch,
Version: &handler.repourl.defaultBranch,
},
ItemVersion: &itemVersion,
},
}

commits, err := handler.getCommits(context.Background(), opt)
commits, err := handler.getCommits(handler.ctx, opt)
if err != nil {
handler.errSetup = fmt.Errorf("request for commits failed with %w", err)
return
Expand All @@ -84,7 +96,7 @@ func (handler *commitsHandler) setup() error {
},
},
}
pullRequests, err := handler.getPullRequestQuery(context.Background(), pullRequestQuery)
pullRequests, err := handler.getPullRequestQuery(handler.ctx, pullRequestQuery)
if err != nil {
handler.errSetup = fmt.Errorf("request for pull requests failed with %w", err)
return
Expand Down
11 changes: 8 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,14 @@ func makeRepo(uri string) (clients.Repo, error) {
var errGitHub, errGitLab, errAzureDevOps error
if repo, errGitHub = githubrepo.MakeGithubRepo(uri); errGitHub != nil {
if repo, errGitLab = gitlabrepo.MakeGitlabRepo(uri); errGitLab != nil {
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))
_, 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))
}
}
}
Expand Down

0 comments on commit 2208766

Please sign in to comment.