Skip to content

Commit

Permalink
Merge pull request #184 from runatlantis/autoplan-notifications
Browse files Browse the repository at this point in the history
Don't comment back on autoplan no projects.
  • Loading branch information
lkysow authored Jul 16, 2018
2 parents 7163ed5 + 84078dc commit 0d1c08a
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 29 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# v0.4.2

## Features
* Don't comment on pull request if autoplan determines there are no projects to plan in.
This was getting very noisy for users who use their repos for more than just Terraform ([#183](https://github.com/runatlantis/atlantis/issues/183)).

## Bugfixes
None

## Backwards Incompatibilities / Notes:
None

## Downloads
* [atlantis_darwin_amd64.zip](https://github.com/runatlantis/atlantis/releases/download/v0.4.2/atlantis_darwin_amd64.zip)
* [atlantis_linux_386.zip](https://github.com/runatlantis/atlantis/releases/download/v0.4.2/atlantis_linux_386.zip)
* [atlantis_linux_amd64.zip](https://github.com/runatlantis/atlantis/releases/download/v0.4.2/atlantis_linux_amd64.zip)
* [atlantis_linux_arm.zip](https://github.com/runatlantis/atlantis/releases/download/v0.4.2/atlantis_linux_arm.zip)

## Docker
`runatlantis/atlantis:v0.4.2`

# v0.4.1

## Features
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/spf13/viper"
)

const atlantisVersion = "0.4.1"
const atlantisVersion = "0.4.2"

func main() {
v := viper.New()
Expand Down
9 changes: 8 additions & 1 deletion server/events/command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ func (c *DefaultCommandRunner) RunAutoplanCommand(baseRepo models.Repo, headRepo
c.updatePull(ctx, AutoplanCommand{}, CommandResult{Error: err})
return
}
if len(projectCmds) == 0 {
log.Info("determined there was no project to run plan in")
if err := c.CommitStatusUpdater.Update(baseRepo, pull, vcs.Success, Plan); err != nil {
ctx.Log.Warn("unable to update commit status: %s", err)
}
return
}

var results []ProjectResult
for _, cmd := range projectCmds {
Expand Down Expand Up @@ -240,7 +247,7 @@ func (c *DefaultCommandRunner) updatePull(ctx *CommandContext, command CommandIn
if err := c.CommitStatusUpdater.UpdateProjectResult(ctx, command.CommandName(), res); err != nil {
ctx.Log.Warn("unable to update commit status: %s", err)
}
comment := c.MarkdownRenderer.Render(res, command.CommandName(), ctx.Log.History.String(), command.IsVerbose(), command.IsAutoplan())
comment := c.MarkdownRenderer.Render(res, command.CommandName(), ctx.Log.History.String(), command.IsVerbose())
c.VCSClient.CreateComment(ctx.BaseRepo, ctx.Pull.Num, comment) // nolint: errcheck
}

Expand Down
7 changes: 1 addition & 6 deletions server/events/markdown_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type ProjectResultTmplData struct {

// Render formats the data into a markdown string.
// nolint: interfacer
func (m *MarkdownRenderer) Render(res CommandResult, cmdName CommandName, log string, verbose bool, autoplan bool) string {
func (m *MarkdownRenderer) Render(res CommandResult, cmdName CommandName, log string, verbose bool) string {
commandStr := strings.Title(cmdName.String())
common := CommonData{commandStr, verbose, log}
if res.Error != nil {
Expand All @@ -67,9 +67,6 @@ func (m *MarkdownRenderer) Render(res CommandResult, cmdName CommandName, log st
if res.Failure != "" {
return m.renderTemplate(failureWithLogTmpl, FailureData{res.Failure, common})
}
if len(res.ProjectResults) == 0 && autoplan {
return m.renderTemplate(autoplanNoProjectsWithLogTmpl, common)
}
return m.renderProjectResults(res.ProjectResults, common)
}

Expand Down Expand Up @@ -148,11 +145,9 @@ var errTmplText = "**{{.Command}} Error**\n" +
"```\n" +
"{{.Error}}\n" +
"```\n"
var autoplanNoProjectsTmplText = "Ran `plan` in 0 projects because Atlantis detected no Terraform changes or could not determine where to run `plan`.\n"
var errTmpl = template.Must(template.New("").Parse(errTmplText))
var errWithLogTmpl = template.Must(template.New("").Parse(errTmplText + logTmpl))
var failureTmplText = "**{{.Command}} Failed**: {{.Failure}}\n"
var failureTmpl = template.Must(template.New("").Parse(failureTmplText))
var failureWithLogTmpl = template.Must(template.New("").Parse(failureTmplText + logTmpl))
var autoplanNoProjectsWithLogTmpl = template.Must(template.New("").Parse(autoplanNoProjectsTmplText + logTmpl))
var logTmpl = "{{if .Verbose}}\n<details><summary>Log</summary>\n <p>\n\n```\n{{.Log}}```\n</p></details>{{end}}\n"
23 changes: 10 additions & 13 deletions server/events/markdown_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestRenderErr(t *testing.T) {
}
for _, verbose := range []bool{true, false} {
t.Log("testing " + c.Description)
s := r.Render(res, c.Command, "log", verbose, false)
s := r.Render(res, c.Command, "log", verbose)
if !verbose {
Equals(t, c.Expected, s)
} else {
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestRenderFailure(t *testing.T) {
}
for _, verbose := range []bool{true, false} {
t.Log("testing " + c.Description)
s := r.Render(res, c.Command, "log", verbose, false)
s := r.Render(res, c.Command, "log", verbose)
if !verbose {
Equals(t, c.Expected, s)
} else {
Expand All @@ -109,26 +109,23 @@ func TestRenderErrAndFailure(t *testing.T) {
Error: errors.New("error"),
Failure: "failure",
}
s := r.Render(res, events.Plan, "", false, false)
s := r.Render(res, events.Plan, "", false)
Equals(t, "**Plan Error**\n```\nerror\n```\n\n", s)
}

func TestRenderAutoplanNoResults(t *testing.T) {
// If there are no project results during an autoplan we should still comment
// back because the user might expect some output.
r := events.MarkdownRenderer{}
res := events.CommandResult{}
s := r.Render(res, events.Plan, "", false, true)
Equals(t, "Ran `plan` in 0 projects because Atlantis detected no Terraform changes or could not determine where to run `plan`.\n\n", s)
}

func TestRenderProjectResults(t *testing.T) {
cases := []struct {
Description string
Command events.CommandName
ProjectResults []events.ProjectResult
Expected string
}{
{
"no projects",
events.Plan,
[]events.ProjectResult{},
"Ran Plan for 0 projects:\n\n\n",
},
{
"single successful plan",
events.Plan,
Expand Down Expand Up @@ -305,7 +302,7 @@ func TestRenderProjectResults(t *testing.T) {
}
for _, verbose := range []bool{true, false} {
t.Run(c.Description, func(t *testing.T) {
s := r.Render(res, c.Command, "log", verbose, false)
s := r.Render(res, c.Command, "log", verbose)
if !verbose {
Equals(t, c.Expected, s)
} else {
Expand Down
19 changes: 13 additions & 6 deletions server/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ func TestGitHubWorkflow(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
RegisterMockTestingT(t)

cases := []struct {
Description string
// RepoDir is relative to testfixtures/test-repos.
Expand Down Expand Up @@ -105,7 +103,7 @@ func TestGitHubWorkflow(t *testing.T) {
Description: "modules modules only",
RepoDir: "modules",
ModifiedFiles: []string{"modules/null/main.tf"},
ExpAutoplanCommentFile: "exp-output-autoplan-only-modules.txt",
ExpAutoplanCommentFile: "",
CommentAndReplies: []string{
"atlantis plan -d staging", "exp-output-plan-staging.txt",
"atlantis plan -d production", "exp-output-plan-production.txt",
Expand Down Expand Up @@ -152,6 +150,8 @@ func TestGitHubWorkflow(t *testing.T) {
}
for _, c := range cases {
t.Run(c.Description, func(t *testing.T) {
RegisterMockTestingT(t)

ctrl, vcsClient, githubGetter, atlantisWorkspace := setupE2E(t)
// Set the repo to be cloned through the testing backdoor.
repoDir, headSHA, cleanup := initializeRepo(t, c.RepoDir)
Expand All @@ -162,12 +162,14 @@ func TestGitHubWorkflow(t *testing.T) {
w := httptest.NewRecorder()
When(githubGetter.GetPullRequest(AnyRepo(), AnyInt())).ThenReturn(GitHubPullRequestParsed(headSHA), nil)
When(vcsClient.GetModifiedFiles(AnyRepo(), matchers.AnyModelsPullRequest())).ThenReturn(c.ModifiedFiles, nil)
expNumTimesCalledCreateComment := 0

// First, send the open pull request event and trigger an autoplan.
pullOpenedReq := GitHubPullRequestOpenedEvent(t, headSHA)
ctrl.Post(w, pullOpenedReq)
responseContains(t, w, 200, "Processing...")
if c.ExpAutoplanCommentFile != "" {
expNumTimesCalledCreateComment++
_, _, autoplanComment := vcsClient.VerifyWasCalledOnce().CreateComment(AnyRepo(), AnyInt(), AnyString()).GetCapturedArguments()
assertCommentEquals(t, c.ExpAutoplanCommentFile, autoplanComment, c.RepoDir)
}
Expand All @@ -181,7 +183,12 @@ func TestGitHubWorkflow(t *testing.T) {
w = httptest.NewRecorder()
ctrl.Post(w, commentReq)
responseContains(t, w, 200, "Processing...")
_, _, atlantisComment := vcsClient.VerifyWasCalled(Times((i/2)+2)).CreateComment(AnyRepo(), AnyInt(), AnyString()).GetCapturedArguments()
// Each comment warrants a response. The comments are at the
// even indices.
if i%2 == 0 {
expNumTimesCalledCreateComment++
}
_, _, atlantisComment := vcsClient.VerifyWasCalled(Times(expNumTimesCalledCreateComment)).CreateComment(AnyRepo(), AnyInt(), AnyString()).GetCapturedArguments()
assertCommentEquals(t, expOutputFile, atlantisComment, c.RepoDir)
}

Expand All @@ -190,8 +197,8 @@ func TestGitHubWorkflow(t *testing.T) {
w = httptest.NewRecorder()
ctrl.Post(w, pullClosedReq)
responseContains(t, w, 200, "Pull request cleaned successfully")
numPrevComments := (len(c.CommentAndReplies) / 2) + 1
_, _, pullClosedComment := vcsClient.VerifyWasCalled(Times(numPrevComments+1)).CreateComment(AnyRepo(), AnyInt(), AnyString()).GetCapturedArguments()
expNumTimesCalledCreateComment++
_, _, pullClosedComment := vcsClient.VerifyWasCalled(Times(expNumTimesCalledCreateComment)).CreateComment(AnyRepo(), AnyInt(), AnyString()).GetCapturedArguments()
assertCommentEquals(t, c.ExpMergeCommentFile, pullClosedComment, c.RepoDir)
})
}
Expand Down

This file was deleted.

0 comments on commit 0d1c08a

Please sign in to comment.