Skip to content

Commit

Permalink
Add support for commit-based lookup to GitHub APIs (#1612)
Browse files Browse the repository at this point in the history
Co-authored-by: Azeem Shaikh <[email protected]>
  • Loading branch information
azeemshaikh38 and azeemsgoogle authored Feb 7, 2022
1 parent 68bf172 commit eac2aec
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 71 deletions.
6 changes: 4 additions & 2 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Client struct {

// InitRepo sets up the GitHub repo in local storage for improving performance and GitHub token usage efficiency.
func (client *Client) InitRepo(inputRepo clients.Repo) error {
commitSHA := "HEAD"
ghRepo, ok := inputRepo.(*repoURL)
if !ok {
return fmt.Errorf("%w: %v", errInputRepoType, inputRepo)
Expand All @@ -67,12 +68,13 @@ func (client *Client) InitRepo(inputRepo clients.Repo) error {
client.repoName = repo.GetName()

// Init tarballHandler.
if err := client.tarball.init(client.ctx, client.repo); err != nil {
if err := client.tarball.init(client.ctx, client.repo, commitSHA); err != nil {
return fmt.Errorf("error during tarballHandler.init: %w", err)
}

// Setup GraphQL.
client.graphClient.init(client.ctx, client.owner, client.repoName)
client.graphClient.init(client.ctx, client.owner, client.repoName,
client.repo.GetDefaultBranch(), commitSHA)

// Setup contributorsHandler.
client.contributors.init(client.ctx, client.owner, client.repoName)
Expand Down
140 changes: 75 additions & 65 deletions clients/githubrepo/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package githubrepo
import (
"context"
"fmt"
"strings"
"sync"
"time"

Expand All @@ -38,65 +39,63 @@ const (
// nolint: govet
type graphqlData struct {
Repository struct {
IsArchived githubv4.Boolean
DefaultBranchRef struct {
Target struct {
Commit struct {
History struct {
Nodes []struct {
CommittedDate githubv4.DateTime
Message githubv4.String
Oid githubv4.GitObjectID
Author struct {
User struct {
Login githubv4.String
}
IsArchived githubv4.Boolean
Object struct {
Commit struct {
History struct {
Nodes []struct {
CommittedDate githubv4.DateTime
Message githubv4.String
Oid githubv4.GitObjectID
Author struct {
User struct {
Login githubv4.String
}
Committer struct {
Name *string
User struct {
Login *string
}
}
Committer struct {
Name *string
User struct {
Login *string
}
AssociatedPullRequests struct {
Nodes []struct {
Repository struct {
Name githubv4.String
Owner struct {
Login githubv4.String
}
}
Author struct {
}
AssociatedPullRequests struct {
Nodes []struct {
Repository struct {
Name githubv4.String
Owner struct {
Login githubv4.String
}
Number githubv4.Int
HeadRefOid githubv4.String
MergedAt githubv4.DateTime
MergeCommit struct {
// NOTE: only used for sanity check.
// Use original commit oid instead.
Oid githubv4.GitObjectID
}
Author struct {
Login githubv4.String
}
Number githubv4.Int
HeadRefOid githubv4.String
MergedAt githubv4.DateTime
MergeCommit struct {
// NOTE: only used for sanity check.
// Use original commit oid instead.
Oid githubv4.GitObjectID
}
Labels struct {
Nodes []struct {
Name githubv4.String
}
Labels struct {
Nodes []struct {
Name githubv4.String
}
} `graphql:"labels(last: $labelsToAnalyze)"`
Reviews struct {
Nodes []struct {
State githubv4.String
Author struct {
Login githubv4.String
}
} `graphql:"labels(last: $labelsToAnalyze)"`
Reviews struct {
Nodes []struct {
State githubv4.String
Author struct {
Login githubv4.String
}
} `graphql:"reviews(last: $reviewsToAnalyze)"`
}
} `graphql:"associatedPullRequests(first: $pullRequestsToAnalyze)"`
}
} `graphql:"history(first: $commitsToAnalyze)"`
} `graphql:"... on Commit"`
}
}
}
} `graphql:"reviews(last: $reviewsToAnalyze)"`
}
} `graphql:"associatedPullRequests(first: $pullRequestsToAnalyze)"`
}
} `graphql:"history(first: $commitsToAnalyze)"`
} `graphql:"... on Commit"`
} `graphql:"object(expression: $commitExpression)"`
Issues struct {
Nodes []struct {
// nolint: revive,stylecheck // naming according to githubv4 convention.
Expand All @@ -115,29 +114,39 @@ type graphqlData struct {
}

type graphqlHandler struct {
client *githubv4.Client
data *graphqlData
once *sync.Once
ctx context.Context
errSetup error
owner string
repo string
commits []clients.Commit
issues []clients.Issue
archived bool
client *githubv4.Client
data *graphqlData
once *sync.Once
ctx context.Context
errSetup error
owner string
repo string
defaultBranch string
commitSHA string
commits []clients.Commit
issues []clients.Issue
archived bool
}

func (handler *graphqlHandler) init(ctx context.Context, owner, repo string) {
func (handler *graphqlHandler) init(ctx context.Context, owner, repo, defaultBranch, commitSHA string) {
handler.ctx = ctx
handler.owner = owner
handler.repo = repo
handler.defaultBranch = defaultBranch
handler.commitSHA = commitSHA
handler.data = new(graphqlData)
handler.errSetup = nil
handler.once = new(sync.Once)
}

func (handler *graphqlHandler) setup() error {
handler.once.Do(func() {
commitExpression := handler.commitSHA
if strings.EqualFold(handler.commitSHA, "HEAD") {
// TODO(#575): Confirm that this works as expected.
commitExpression = fmt.Sprintf("heads/%s", handler.defaultBranch)
}

vars := map[string]interface{}{
"owner": githubv4.String(handler.owner),
"name": githubv4.String(handler.repo),
Expand All @@ -147,6 +156,7 @@ func (handler *graphqlHandler) setup() error {
"reviewsToAnalyze": githubv4.Int(reviewsToAnalyze),
"labelsToAnalyze": githubv4.Int(labelsToAnalyze),
"commitsToAnalyze": githubv4.Int(commitsToAnalyze),
"commitExpression": githubv4.String(commitExpression),
}
if err := handler.client.Query(handler.ctx, handler.data, vars); err != nil {
handler.errSetup = sce.WithMessage(sce.ErrScorecardInternal, fmt.Sprintf("githubv4.Query: %v", err))
Expand Down Expand Up @@ -186,7 +196,7 @@ func (handler *graphqlHandler) isArchived() (bool, error) {
// nolint: unparam
func commitsFrom(data *graphqlData, repoOwner, repoName string) ([]clients.Commit, error) {
ret := make([]clients.Commit, 0)
for _, commit := range data.Repository.DefaultBranchRef.Target.Commit.History.Nodes {
for _, commit := range data.Repository.Object.Commit.History.Nodes {
var committer string
if commit.Committer.User.Login != nil {
committer = *commit.Committer.User.Login
Expand Down
12 changes: 8 additions & 4 deletions clients/githubrepo/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ type tarballHandler struct {
files []string
}

func (handler *tarballHandler) init(ctx context.Context, repo *github.Repository) error {
func (handler *tarballHandler) init(ctx context.Context, repo *github.Repository, commitSHA string) error {
// Cleanup any previous state.
if err := handler.cleanup(); err != nil {
return sce.WithMessage(sce.ErrScorecardInternal, err.Error())
}

// Setup temp dir/files and download repo tarball.
if err := handler.getTarball(ctx, repo); errors.Is(err, errTarballNotFound) {
if err := handler.getTarball(ctx, repo, commitSHA); errors.Is(err, errTarballNotFound) {
log.Printf("unable to get tarball %v. Skipping...", err)
return nil
} else if err != nil {
Expand All @@ -93,10 +93,14 @@ func (handler *tarballHandler) init(ctx context.Context, repo *github.Repository
return nil
}

func (handler *tarballHandler) getTarball(ctx context.Context, repo *github.Repository) error {
func (handler *tarballHandler) getTarball(ctx context.Context, repo *github.Repository, commitSHA string) error {
url := repo.GetArchiveURL()
url = strings.Replace(url, "{archive_format}", "tarball/", 1)
url = strings.Replace(url, "{/ref}", "", 1)
if strings.EqualFold(commitSHA, "HEAD") {
url = strings.Replace(url, "{/ref}", "", 1)
} else {
url = strings.Replace(url, "{/ref}", commitSHA, 1)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return fmt.Errorf("http.NewRequestWithContext: %w", err)
Expand Down

0 comments on commit eac2aec

Please sign in to comment.