Skip to content

Commit

Permalink
install: Use package-internal GitHub interface (1/n)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Augustus <foo@auggie.dev>
  • Loading branch information
justaugustus committed May 24, 2022
1 parent ba36d71 commit 442d418
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 26 deletions.
96 changes: 96 additions & 0 deletions install/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,33 @@ type GitHub struct {
// Client is an interface modeling supported GitHub operations
type Client interface {
// TODO(install): Populate interface
CreateFile(
context.Context, string, string, string, *github.RepositoryContentFileOptions,
) (*github.RepositoryContentResponse, *github.Response, error)

CreatePullRequest(
context.Context, string, string, string, string, string, string,
) (*github.PullRequest, error)

CreateGitRef(
context.Context, string, string, *github.Reference,
) (*github.Reference, *github.Response, error)

GetBranch(
context.Context, string, string, string, bool,
) (*github.Branch, *github.Response, error)

GetContents(
context.Context, string, string, string, *github.RepositoryContentGetOptions,
) (*github.RepositoryContent, []*github.RepositoryContent, *github.Response, error)

GetRepositoriesByOrg(
context.Context, string,
) ([]*github.Repository, *github.Response, error)

GetRepository(
context.Context, string, string,
) (*github.Repository, *github.Response, error)
}

// Options is a set of options to configure the behavior of the GitHub package
Expand Down Expand Up @@ -181,3 +205,75 @@ func (g *githubClient) GetRepositoriesByOrg(

return repos, resp, nil
}

func (g *githubClient) GetRepository(
ctx context.Context, owner, repo string,
) (*github.Repository, *github.Response, error) {
pr, resp, err := g.Repositories.Get(ctx, owner, repo)
if err != nil {
return pr, resp, fmt.Errorf("getting repository: %w", err)
}

return pr, resp, nil
}

func (g *githubClient) GetBranch(
ctx context.Context, owner, repo, branch string, followRedirect bool) (*github.Branch, *github.Response, error) {
// TODO: Populate
return nil, nil, nil
}

func (g *githubClient) GetContents(
ctx context.Context, owner, repo, path string, opts *github.RepositoryContentGetOptions) (*github.RepositoryContent, []*github.RepositoryContent, *github.Response, error) {
// TODO: Populate
return g.Repositories.GetContents(
ctx,
owner,
repo,
path,
opts,
)
}

func (g *githubClient) CreateGitRef(
ctx context.Context, owner, repo string, ref *github.Reference) (*github.Reference, *github.Response, error) {
// TODO: Populate
return g.Git.CreateRef(
ctx,
owner,
repo,
ref,
)
}

func (g *githubClient) CreateFile(
ctx context.Context, owner, repo, path string, opts *github.RepositoryContentFileOptions) (*github.RepositoryContentResponse, *github.Response, error) {
// TODO: Populate
return g.Repositories.CreateFile(
ctx,
owner,
repo,
path,
opts,
)
}

func (g *githubClient) CreatePullRequest(
ctx context.Context, owner, repo, baseBranchName, headBranchName, title, body string,
) (*github.PullRequest, error) {
newPullRequest := &github.NewPullRequest{
Title: &title,
Head: &headBranchName,
Base: &baseBranchName,
Body: &body,
MaintainerCanModify: github.Bool(true),
}

pr, _, err := g.PullRequests.Create(ctx, owner, repo, newPullRequest)
if err != nil {
return pr, fmt.Errorf("creating pull request: %w", err)
}

logrus.Infof("Successfully created PR #%d", pr.GetNumber())
return pr, nil
}
47 changes: 21 additions & 26 deletions install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"io/ioutil"

"github.com/google/go-github/v42/github"
"golang.org/x/oauth2"

scagh "github.com/ossf/scorecard-action/install/github"
)

const (
Expand All @@ -37,20 +38,16 @@ var RepoList = []string{}
// Run adds the OpenSSF Scorecard workflow to all repositories under the given
// organization.
// TODO(install): Improve description.
// TODO(install): Accept a context instead of setting one.
func Run() {
// Get github user client.
ctx := context.Background()
tokenService := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: pat},
)

tokenClient := oauth2.NewClient(ctx, tokenService)
client := github.NewClient(tokenClient)
gh := scagh.New()
client := gh.Client()

// If not provided, get all repositories under organization.
if len(RepoList) == 0 {
lops := &github.RepositoryListByOrgOptions{Type: "all"}
repos, _, err := client.Repositories.ListByOrg(ctx, orgName, lops)
repos, _, err := client.GetRepositoriesByOrg(ctx, orgName)
errCheck(err, "Error listing organization's repos.")

// Convert to list of repository names.
Expand All @@ -66,7 +63,7 @@ func Run() {
// Process each repository.
for _, repoName := range RepoList {
// Get repo metadata.
repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
repo, _, err := client.GetRepository(ctx, orgName, repoName)
if err != nil {
fmt.Println(
"Skipped repo",
Expand All @@ -78,7 +75,7 @@ func Run() {
}

// Get head commit SHA of default branch.
defaultBranch, _, err := client.Repositories.GetBranch(
defaultBranch, _, err := client.GetBranch(
ctx,
orgName,
repoName,
Expand All @@ -98,7 +95,7 @@ func Run() {
defaultBranchSHA := defaultBranch.Commit.SHA

// Skip if scorecard file already exists in workflows folder.
scoreFileContent, _, _, err := client.Repositories.GetContents(
scoreFileContent, _, _, err := client.GetContents(
ctx,
orgName,
repoName,
Expand All @@ -116,7 +113,7 @@ func Run() {
}

// Skip if branch scorecard already exists.
scorecardBranch, _, err := client.Repositories.GetBranch(
scorecardBranch, _, err := client.GetBranch(
ctx,
orgName,
repoName,
Expand All @@ -138,7 +135,7 @@ func Run() {
Ref: github.String("refs/heads/scorecard"),
Object: &github.GitObject{SHA: defaultBranchSHA},
}
_, _, err = client.Git.CreateRef(ctx, orgName, repoName, ref)
_, _, err = client.CreateGitRef(ctx, orgName, repoName, ref)
if err != nil {
fmt.Println(
"Skipped repo",
Expand All @@ -155,7 +152,7 @@ func Run() {
Content: workflowContent,
Branch: github.String("scorecard"),
}
_, _, err = client.Repositories.CreateFile(
_, _, err = client.CreateFile(
ctx,
orgName,
repoName,
Expand All @@ -173,17 +170,15 @@ func Run() {
}

// Create Pull request.
pr := &github.NewPullRequest{
Title: github.String("Added Scorecard Workflow"),
Head: github.String("scorecard"),
Base: github.String(*defaultBranch.Name),
Body: github.String(
"Added the workflow for OpenSSF's Security Scorecard",
),
Draft: github.Bool(false),
}

_, _, err = client.PullRequests.Create(ctx, orgName, repoName, pr)
_, err = client.CreatePullRequest(
ctx,
orgName,
repoName,
*defaultBranch.Name,
"scorecard",
"Added Scorecard Workflow",
"Added the workflow for OpenSSF's Security Scorecard",
)
if err != nil {
fmt.Println(
"Skipped repo",
Expand Down

0 comments on commit 442d418

Please sign in to comment.