Skip to content

Commit

Permalink
Add and use RepoClient API for ListStatuses (#949)
Browse files Browse the repository at this point in the history
Co-authored-by: Azeem Shaikh <azeems@google.com>
  • Loading branch information
azeemshaikh38 and azeemsgoogle authored Sep 1, 2021
1 parent eb2b3b2 commit eceb577
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 8 deletions.
13 changes: 5 additions & 8 deletions checks/ci_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"fmt"
"strings"

"github.com/google/go-github/v38/github"

"github.com/ossf/scorecard/v2/checker"
"github.com/ossf/scorecard/v2/clients"
sce "github.com/ossf/scorecard/v2/errors"
Expand Down Expand Up @@ -106,23 +104,22 @@ func CITests(c *checker.CheckRequest) checker.CheckResult {

// PR has a status marked 'success' and a CI-related context.
func prHasSuccessStatus(pr *clients.PullRequest, c *checker.CheckRequest) (bool, error) {
statuses, _, err := c.Client.Repositories.ListStatuses(c.Ctx, c.Owner, c.Repo, pr.HeadSHA,
&github.ListOptions{})
statuses, err := c.RepoClient.ListStatuses(pr.HeadSHA)
if err != nil {
//nolint
return false, sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("Client.Repositories.ListStatuses: %v", err))
}

for _, status := range statuses {
if status.GetState() != success {
if status.State != success {
continue
}
if isTest(status.GetContext()) {
if isTest(status.Context) {
c.Dlogger.Debug3(&checker.LogMessage{
Path: status.GetURL(),
Path: status.URL,
Type: checker.FileTypeURL,
Text: fmt.Sprintf("CI test found: pr: %d, context: %s", pr.Number,
status.GetContext()),
status.Context),
})
return true, nil
}
Expand Down
12 changes: 12 additions & 0 deletions clients/githubrepo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Client struct {
releases *releasesHandler
workflows *workflowsHandler
checkruns *checkrunsHandler
statuses *statusesHandler
search *searchHandler
ctx context.Context
tarball tarballHandler
Expand Down Expand Up @@ -78,6 +79,9 @@ func (client *Client) InitRepo(owner, repoName string) error {
// Setup checkrunsHandler.
client.checkruns.init(client.ctx, client.owner, client.repoName)

// Setup statusesHandler.
client.statuses.init(client.ctx, client.owner, client.repoName)

// Setup searchHandler.
client.search.init(client.ctx, client.owner, client.repoName)

Expand Down Expand Up @@ -144,6 +148,11 @@ func (client *Client) ListCheckRunsForRef(ref string) ([]clients.CheckRun, error
return client.checkruns.listCheckRunsForRef(ref)
}

// ListStatuses implements RepoClient.ListStatuses.
func (client *Client) ListStatuses(ref string) ([]clients.Status, error) {
return client.statuses.listStatuses(ref)
}

// Search implements RepoClient.Search.
func (client *Client) Search(request clients.SearchRequest) (clients.SearchResponse, error) {
return client.search.search(request)
Expand Down Expand Up @@ -179,6 +188,9 @@ func CreateGithubRepoClient(ctx context.Context,
checkruns: &checkrunsHandler{
client: client,
},
statuses: &statusesHandler{
client: client,
},
search: &searchHandler{
ghClient: client,
},
Expand Down
61 changes: 61 additions & 0 deletions clients/githubrepo/statuses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package githubrepo

import (
"context"
"fmt"

"github.com/google/go-github/v38/github"

"github.com/ossf/scorecard/v2/clients"
sce "github.com/ossf/scorecard/v2/errors"
)

type statusesHandler struct {
client *github.Client
ctx context.Context
owner string
repo string
}

func (handler *statusesHandler) init(ctx context.Context, owner, repo string) {
handler.ctx = ctx
handler.owner = owner
handler.repo = repo
}

func (handler *statusesHandler) listStatuses(ref string) ([]clients.Status, error) {
statuses, _, err := handler.client.Repositories.ListStatuses(handler.ctx, handler.owner, handler.repo, ref,
&github.ListOptions{})
if err != nil {
// nolint: wrapcheck
return nil, sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("ListStatuses: %v", err))
}
return statusesFrom(statuses), nil
}

func statusesFrom(data []*github.RepoStatus) []clients.Status {
// nolint: prealloc // https://github.com/golang/go/wiki/CodeReviewComments#declaring-empty-slices
var statuses []clients.Status
for _, status := range data {
statuses = append(statuses, clients.Status{
State: status.GetState(),
Context: status.GetContext(),
URL: status.GetURL(),
})
}
return statuses
}
15 changes: 15 additions & 0 deletions clients/mockrepo/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions clients/repo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type RepoClient interface {
ListContributors() ([]Contributor, error)
ListSuccessfulWorkflowRuns(filename string) ([]WorkflowRun, error)
ListCheckRunsForRef(ref string) ([]CheckRun, error)
ListStatuses(ref string) ([]Status, error)
Search(request SearchRequest) (SearchResponse, error)
Close() error
}
22 changes: 22 additions & 0 deletions clients/statuses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 Security Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package clients

// Status for a Git object/ref.
type Status struct {
State string
Context string
URL string
}

0 comments on commit eceb577

Please sign in to comment.