From 2208766b7d65bf984ee7616ff87bbce0ac9eaa05 Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Fri, 11 Oct 2024 20:58:49 -0700 Subject: [PATCH] Address PR comments --- clients/azuredevopsrepo/branches.go | 6 +- clients/azuredevopsrepo/client.go | 113 +++++++++++++--------------- clients/azuredevopsrepo/commits.go | 26 +++++-- cmd/root.go | 11 ++- 4 files changed, 85 insertions(+), 71 deletions(-) diff --git a/clients/azuredevopsrepo/branches.go b/clients/azuredevopsrepo/branches.go index a98be737013..9907a7da143 100644 --- a/clients/azuredevopsrepo/branches.go +++ b/clients/azuredevopsrepo/branches.go @@ -25,6 +25,7 @@ import ( type branchesHandler struct { gitClient git.Client + ctx context.Context once *sync.Once errSetup error repourl *Repo @@ -32,7 +33,8 @@ type branchesHandler struct { 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) @@ -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, }) diff --git a/clients/azuredevopsrepo/client.go b/clients/azuredevopsrepo/client.go index 5b59fc9be39..40d907836d2 100644 --- a/clients/azuredevopsrepo/client.go +++ b/clients/azuredevopsrepo/client.go @@ -19,7 +19,6 @@ import ( "errors" "fmt" "io" - "log" "os" "strings" "time" @@ -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, }) @@ -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, @@ -76,19 +75,19 @@ 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) } @@ -96,91 +95,91 @@ func (client *Client) IsArchived() (bool, error) { 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") } -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 } @@ -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) -} diff --git a/clients/azuredevopsrepo/commits.go b/clients/azuredevopsrepo/commits.go index 1a794b9b663..b7e3f398bb0 100644 --- a/clients/azuredevopsrepo/commits.go +++ b/clients/azuredevopsrepo/commits.go @@ -25,6 +25,7 @@ import ( type commitsHandler struct { gitClient git.Client + ctx context.Context once *sync.Once errSetup error repourl *Repo @@ -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) @@ -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 @@ -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 diff --git a/cmd/root.go b/cmd/root.go index 25ff2316494..bc50e7b8d4b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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)) } } }