Skip to content

Commit

Permalink
Add interface for github.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Sep 16, 2017
1 parent 95d67a0 commit b53e50b
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ test: ## Run tests, coverage reports, and clean (coverage taints the compiled co
test-coverage:
./scripts/coverage.sh

test-coverage-html:
./scripts/coverage.sh --html

dist: ## Package up everything in static/ using go-bindata-assetfs so it can be served by a single binary
go-bindata-assetfs -pkg server static/... && mv bindata_assetfs.go server

Expand Down
26 changes: 17 additions & 9 deletions github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ import (
"github.com/pkg/errors"
)

// Client is used to perform GitHub actions.
type Client struct {
// ConcreteClient is used to perform GitHub actions.
type ConcreteClient struct {
client *github.Client
ctx context.Context
}

type Client interface {
GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error)
CreateComment(repo models.Repo, pull models.PullRequest, comment string) error
PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error)
GetPullRequest(repo models.Repo, num int) (*github.PullRequest, *github.Response, error)
UpdateStatus(repo models.Repo, pull models.PullRequest, state string, description string, context string) error
}

// NewClient returns a valid GitHub client.
func NewClient(hostname string, user string, pass string) (*Client, error) {
func NewClient(hostname string, user string, pass string) (*ConcreteClient, error) {
tp := github.BasicAuthTransport{
Username: strings.TrimSpace(user),
Password: strings.TrimSpace(pass),
Expand All @@ -38,15 +46,15 @@ func NewClient(hostname string, user string, pass string) (*Client, error) {
client.BaseURL = base
}

return &Client{
return &ConcreteClient{
client: client,
ctx: context.Background(),
}, nil
}

// GetModifiedFiles returns the names of files that were modified in the pull request.
// The names include the path to the file from the repo root, ex. parent/child/file.txt.
func (c *Client) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
func (c *ConcreteClient) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
var files []string
nextPage := 0
for {
Expand All @@ -72,13 +80,13 @@ func (c *Client) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]
}

// CreateComment creates a comment on the pull request.
func (c *Client) CreateComment(repo models.Repo, pull models.PullRequest, comment string) error {
func (c *ConcreteClient) CreateComment(repo models.Repo, pull models.PullRequest, comment string) error {
_, _, err := c.client.Issues.CreateComment(c.ctx, repo.Owner, repo.Name, pull.Num, &github.IssueComment{Body: &comment})
return err
}

// PullIsApproved returns true if the pull request was approved.
func (c *Client) PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error) {
func (c *ConcreteClient) PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error) {
reviews, _, err := c.client.PullRequests.ListReviews(c.ctx, repo.Owner, repo.Name, pull.Num, nil)
if err != nil {
return false, errors.Wrap(err, "getting reviews")
Expand All @@ -92,13 +100,13 @@ func (c *Client) PullIsApproved(repo models.Repo, pull models.PullRequest) (bool
}

// GetPullRequest returns the pull request.
func (c *Client) GetPullRequest(repo models.Repo, num int) (*github.PullRequest, *github.Response, error) {
func (c *ConcreteClient) GetPullRequest(repo models.Repo, num int) (*github.PullRequest, *github.Response, error) {
return c.client.PullRequests.Get(c.ctx, repo.Owner, repo.Name, num)
}

// UpdateStatus updates the status badge on the pull request.
// See https://github.com/blog/1227-commit-status-api.
func (c *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, state string, description string, context string) error {
func (c *ConcreteClient) UpdateStatus(repo models.Repo, pull models.PullRequest, state string, description string, context string) error {
status := &github.RepoStatus{
State: github.String(state),
Description: github.String(description),
Expand Down
2 changes: 1 addition & 1 deletion server/apply_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

type ApplyExecutor struct {
github *github.Client
github github.Client
githubStatus *GithubStatus
terraform *terraform.Client
githubCommentRenderer *GithubCommentRenderer
Expand Down
2 changes: 1 addition & 1 deletion server/command_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type CommandHandler struct {
planExecutor *PlanExecutor
applyExecutor *ApplyExecutor
helpExecutor *HelpExecutor
githubClient *github.Client
githubClient github.Client
eventParser *EventParser
logger *logging.SimpleLogger
}
Expand Down
13 changes: 8 additions & 5 deletions server/github_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import (
type Status int

const (
statusContext = "Atlantis"
Pending Status = iota
statusContext = "Atlantis"
PlanStep = "plan"
ApplyStep = "apply"
)

const (
Pending Status = iota
Success
Failure
Error
PlanStep = "plan"
ApplyStep = "apply"
)

type GithubStatus struct {
client *github.Client
client github.Client
}

func (s Status) String() string {
Expand Down
3 changes: 3 additions & 0 deletions server/github_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package server_test

func
2 changes: 1 addition & 1 deletion server/help_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type HelpExecutor struct {
github *github.Client
github github.Client
}

var helpComment = "```cmake\n" +
Expand Down
2 changes: 1 addition & 1 deletion server/plan_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// PlanExecutor handles everything related to running terraform plan
// including integration with S3, Terraform, and GitHub
type PlanExecutor struct {
github *github.Client
github github.Client
githubStatus *GithubStatus
s3Bucket string
terraform *terraform.Client
Expand Down
2 changes: 1 addition & 1 deletion server/pull_closed_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type PullClosedExecutor struct {
locking *locking.Client
github *github.Client
github github.Client
workspace *Workspace
}

Expand Down

0 comments on commit b53e50b

Please sign in to comment.