Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set source in status checks #492

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/events/command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type GitlabMergeRequestGetter interface {

// DefaultCommandRunner is the first step when processing a comment command.
type DefaultCommandRunner struct {
VCSClient vcs.ClientProxy
VCSClient vcs.Client
GithubPullGetter GithubPullGetter
GitlabMergeRequestGetter GitlabMergeRequestGetter
CommitStatusUpdater CommitStatusUpdater
Expand Down
4 changes: 2 additions & 2 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ var pullLogger *logging.SimpleLogger
var workingDir events.WorkingDir
var pendingPlanFinder *mocks.MockPendingPlanFinder

func setup(t *testing.T) *vcsmocks.MockClientProxy {
func setup(t *testing.T) *vcsmocks.MockClient {
RegisterMockTestingT(t)
projectCommandBuilder = mocks.NewMockProjectCommandBuilder()
eventParsing = mocks.NewMockEventParsing()
ghStatus = mocks.NewMockCommitStatusUpdater()
vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
githubGetter = mocks.NewMockGithubPullGetter()
gitlabGetter = mocks.NewMockGitlabMergeRequestGetter()
logger := logmocks.NewMockSimpleLogging()
Expand Down
4 changes: 2 additions & 2 deletions server/events/commit_status_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ type CommitStatusUpdater interface {

// DefaultCommitStatusUpdater implements CommitStatusUpdater.
type DefaultCommitStatusUpdater struct {
Client vcs.ClientProxy
Client vcs.Client
}

// Update updates the commit status.
func (d *DefaultCommitStatusUpdater) Update(repo models.Repo, pull models.PullRequest, status models.CommitStatus, command CommandName) error {
description := fmt.Sprintf("%s %s", strings.Title(command.String()), strings.Title(status.String()))
return d.Client.UpdateStatus(repo, pull, status, description)
return d.Client.UpdateStatus(repo, pull, status, "Atlantis", description)
}

// UpdateProjectResult updates the commit status based on the status of res.
Expand Down
16 changes: 8 additions & 8 deletions server/events/commit_status_updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ var status = models.SuccessCommitStatus

func TestUpdate(t *testing.T) {
RegisterMockTestingT(t)
client := mocks.NewMockClientProxy()
client := mocks.NewMockClient()
s := events.DefaultCommitStatusUpdater{Client: client}
err := s.Update(repoModel, pullModel, status, events.PlanCommand)
Ok(t, err)
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, status, "Plan Success")
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, status, "Atlantis", "Plan Success")
}

func TestUpdateProjectResult_Error(t *testing.T) {
Expand All @@ -44,11 +44,11 @@ func TestUpdateProjectResult_Error(t *testing.T) {
BaseRepo: repoModel,
Pull: pullModel,
}
client := mocks.NewMockClientProxy()
client := mocks.NewMockClient()
s := events.DefaultCommitStatusUpdater{Client: client}
err := s.UpdateProjectResult(ctx, events.PlanCommand, events.CommandResult{Error: errors.New("err")})
Ok(t, err)
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, models.FailedCommitStatus, "Plan Failed")
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, models.FailedCommitStatus, "Atlantis", "Plan Failed")
}

func TestUpdateProjectResult_Failure(t *testing.T) {
Expand All @@ -57,11 +57,11 @@ func TestUpdateProjectResult_Failure(t *testing.T) {
BaseRepo: repoModel,
Pull: pullModel,
}
client := mocks.NewMockClientProxy()
client := mocks.NewMockClient()
s := events.DefaultCommitStatusUpdater{Client: client}
err := s.UpdateProjectResult(ctx, events.PlanCommand, events.CommandResult{Failure: "failure"})
Ok(t, err)
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, models.FailedCommitStatus, "Plan Failed")
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, models.FailedCommitStatus, "Atlantis", "Plan Failed")
}

func TestUpdateProjectResult(t *testing.T) {
Expand Down Expand Up @@ -127,11 +127,11 @@ func TestUpdateProjectResult(t *testing.T) {
}
resp := events.CommandResult{ProjectResults: results}

client := mocks.NewMockClientProxy()
client := mocks.NewMockClient()
s := events.DefaultCommitStatusUpdater{Client: client}
err := s.UpdateProjectResult(ctx, events.PlanCommand, resp)
Ok(t, err)
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, c.Expected, "Plan "+strings.Title(c.Expected.String()))
client.VerifyWasCalledOnce().UpdateStatus(repoModel, pullModel, c.Expected, "Atlantis", "Plan "+strings.Title(c.Expected.String()))
})
}
}
2 changes: 1 addition & 1 deletion server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type ProjectCommandBuilder interface {
type DefaultProjectCommandBuilder struct {
ParserValidator *yaml.ParserValidator
ProjectFinder ProjectFinder
VCSClient vcs.ClientProxy
VCSClient vcs.Client
WorkingDir WorkingDir
WorkingDirLocker WorkingDirLocker
AllowRepoConfig bool
Expand Down
12 changes: 6 additions & 6 deletions server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ projects:
err := ioutil.WriteFile(filepath.Join(tmpDir, "main.tf"), nil, 0600)
Ok(t, err)

vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(baseRepo, pull)).ThenReturn([]string{"main.tf"}, nil)

builder := &events.DefaultProjectCommandBuilder{
Expand Down Expand Up @@ -467,7 +467,7 @@ projects:
err := ioutil.WriteFile(filepath.Join(tmpDir, "main.tf"), nil, 0600)
Ok(t, err)

vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(baseRepo, pull)).ThenReturn([]string{"main.tf"}, nil)

builder := &events.DefaultProjectCommandBuilder{
Expand Down Expand Up @@ -539,7 +539,7 @@ func TestDefaultProjectCommandBuilder_BuildMultiPlanNoAtlantisYAML(t *testing.T)
matchers.AnyModelsRepo(),
matchers.AnyModelsPullRequest(),
AnyString())).ThenReturn(tmpDir, nil)
vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]string{"project1/main.tf", "project2/main.tf"}, nil)

builder := &events.DefaultProjectCommandBuilder{
Expand Down Expand Up @@ -592,7 +592,7 @@ func TestDefaultProjectCommandBuilder_BuildMultiPlanNoAtlantisYAMLNoModified(t *
matchers.AnyModelsRepo(),
matchers.AnyModelsPullRequest(),
AnyString())).ThenReturn(tmpDir, nil)
vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]string{}, nil)

builder := &events.DefaultProjectCommandBuilder{
Expand Down Expand Up @@ -662,7 +662,7 @@ projects:
matchers.AnyModelsRepo(),
matchers.AnyModelsPullRequest(),
AnyString())).ThenReturn(tmpDir, nil)
vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]string{
"project1/main.tf", "project2/main.tf", "project3/main.tf",
}, nil)
Expand Down Expand Up @@ -726,7 +726,7 @@ projects:
matchers.AnyModelsRepo(),
matchers.AnyModelsPullRequest(),
AnyString())).ThenReturn(tmpDir, nil)
vcsClient := vcsmocks.NewMockClientProxy()
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]string{"main.tf"}, nil)

builder := &events.DefaultProjectCommandBuilder{
Expand Down
2 changes: 1 addition & 1 deletion server/events/pull_closed_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type PullCleaner interface {
// request.
type PullClosedExecutor struct {
Locker locking.Locker
VCSClient vcs.ClientProxy
VCSClient vcs.Client
WorkingDir WorkingDir
Logger logging.SimpleLogging
DB *db.BoltDB
Expand Down
4 changes: 2 additions & 2 deletions server/events/pull_closed_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestCleanUpPullNoLocks(t *testing.T) {
RegisterMockTestingT(t)
w := mocks.NewMockWorkingDir()
l := lockmocks.NewMockLocker()
cp := vcsmocks.NewMockClientProxy()
cp := vcsmocks.NewMockClient()
tmp, cleanup := TempDir(t)
defer cleanup()
db, err := db.New(tmp)
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestCleanUpPullComments(t *testing.T) {
for _, c := range cases {
func() {
w := mocks.NewMockWorkingDir()
cp := vcsmocks.NewMockClientProxy()
cp := vcsmocks.NewMockClient()
l := lockmocks.NewMockLocker()
tmp, cleanup := TempDir(t)
defer cleanup()
Expand Down
4 changes: 2 additions & 2 deletions server/events/vcs/bitbucketcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (b *Client) PullIsMergeable(repo models.Repo, pull models.PullRequest) (boo
}

// UpdateStatus updates the status of a commit.
func (b *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, status models.CommitStatus, description string) error {
func (b *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, status models.CommitStatus, src string, description string) error {
bbState := "FAILED"
switch status {
case models.PendingCommitStatus:
Expand All @@ -158,7 +158,7 @@ func (b *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, status
}

bodyBytes, err := json.Marshal(map[string]string{
"key": "atlantis",
"key": src,
"url": b.AtlantisURL,
"state": bbState,
"description": description,
Expand Down
4 changes: 2 additions & 2 deletions server/events/vcs/bitbucketserver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (b *Client) PullIsMergeable(repo models.Repo, pull models.PullRequest) (boo
}

// UpdateStatus updates the status of a commit.
func (b *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, status models.CommitStatus, description string) error {
func (b *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, status models.CommitStatus, src string, description string) error {
bbState := "FAILED"
switch status {
case models.PendingCommitStatus:
Expand All @@ -221,7 +221,7 @@ func (b *Client) UpdateStatus(repo models.Repo, pull models.PullRequest, status
}

bodyBytes, err := json.Marshal(map[string]string{
"key": "atlantis",
"key": src,
"url": b.AtlantisURL,
"state": bbState,
"description": description,
Expand Down
7 changes: 6 additions & 1 deletion server/events/vcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ type Client interface {
CreateComment(repo models.Repo, pullNum int, comment string) error
PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error)
PullIsMergeable(repo models.Repo, pull models.PullRequest) (bool, error)
UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, description string) error
// UpdateStatus updates the commit status to state for pull. src is the
// source of this status. This should be relatively static across runs,
// ex. plan/atlantis or apply/atlantis.
// description is a description of this particular status update and can
// change across runs.
UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, src string, description string) error
MergePull(pull models.PullRequest) error
}
6 changes: 3 additions & 3 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ func (g *GithubClient) GetPullRequest(repo models.Repo, num int) (*github.PullRe

// UpdateStatus updates the status badge on the pull request.
// See https://github.com/blog/1227-commit-status-api.
func (g *GithubClient) UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, description string) error {
const statusContext = "Atlantis"
func (g *GithubClient) UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, src string, description string) error {
ghState := "error"
switch state {
case models.PendingCommitStatus:
Expand All @@ -168,10 +167,11 @@ func (g *GithubClient) UpdateStatus(repo models.Repo, pull models.PullRequest, s
case models.FailedCommitStatus:
ghState = "failure"
}

status := &github.RepoStatus{
State: github.String(ghState),
Description: github.String(description),
Context: github.String(statusContext)}
Context: github.String(src)}
_, _, err := g.client.Repositories.CreateStatus(g.ctx, repo.Owner, repo.Name, pull.HeadCommit, status)
return err
}
Expand Down
4 changes: 2 additions & 2 deletions server/events/vcs/github_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestGithubClient_UpdateStatus(t *testing.T) {
case "/api/v3/repos/owner/repo/statuses/":
body, err := ioutil.ReadAll(r.Body)
Ok(t, err)
exp := fmt.Sprintf(`{"state":"%s","description":"description","context":"Atlantis"}%s`, c.expState, "\n")
exp := fmt.Sprintf(`{"state":"%s","description":"description","context":"src"}%s`, c.expState, "\n")
Equals(t, exp, string(body))
defer r.Body.Close() // nolint: errcheck
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestGithubClient_UpdateStatus(t *testing.T) {
},
}, models.PullRequest{
Num: 1,
}, c.status, "description")
}, c.status, "src", "description")
Ok(t, err)
})
}
Expand Down
6 changes: 2 additions & 4 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ func (g *GitlabClient) PullIsMergeable(repo models.Repo, pull models.PullRequest
}

// UpdateStatus updates the build status of a commit.
func (g *GitlabClient) UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, description string) error {
const statusContext = "Atlantis"

func (g *GitlabClient) UpdateStatus(repo models.Repo, pull models.PullRequest, state models.CommitStatus, src string, description string) error {
gitlabState := gitlab.Failed
switch state {
case models.PendingCommitStatus:
Expand All @@ -190,7 +188,7 @@ func (g *GitlabClient) UpdateStatus(repo models.Repo, pull models.PullRequest, s
}
_, _, err := g.Client.Commits.SetCommitStatus(repo.FullName, pull.HeadCommit, &gitlab.SetCommitStatusOptions{
State: gitlabState,
Context: gitlab.String(statusContext),
Context: gitlab.String(src),
Description: gitlab.String(description),
})
return err
Expand Down
62 changes: 54 additions & 8 deletions server/events/vcs/mocks/mock_client.go

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

Loading