Skip to content

Commit

Permalink
Merge pull request #578 from runatlantis/runstep
Browse files Browse the repository at this point in the history
Add PROJECT_NAME env var to custom run step
  • Loading branch information
lkysow authored Apr 8, 2019
2 parents 2f54257 + cf490f8 commit 9115e8f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion server/events/pending_plan_finder.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package events

import (
"github.com/runatlantis/atlantis/server/events/runtime"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/runtime"
)

//go:generate pegomock generate -m --use-experimental-model-gen --package mocks -o mocks/mock_pending_plan_finder.go PendingPlanFinder
Expand Down
13 changes: 7 additions & 6 deletions server/events/runtime/run_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,20 @@ func (r *RunStepRunner) Run(ctx models.ProjectCommandContext, command string, pa
}
baseEnvVars := os.Environ()
customEnvVars := map[string]string{
"WORKSPACE": ctx.Workspace,
"ATLANTIS_TERRAFORM_VERSION": tfVersion,
"DIR": path,
"PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)),
"BASE_BRANCH_NAME": ctx.Pull.BaseBranch,
"BASE_REPO_NAME": ctx.BaseRepo.Name,
"BASE_REPO_OWNER": ctx.BaseRepo.Owner,
"DIR": path,
"HEAD_BRANCH_NAME": ctx.Pull.HeadBranch,
"HEAD_REPO_NAME": ctx.HeadRepo.Name,
"HEAD_REPO_OWNER": ctx.HeadRepo.Owner,
"HEAD_BRANCH_NAME": ctx.Pull.HeadBranch,
"BASE_BRANCH_NAME": ctx.Pull.BaseBranch,
"PULL_NUM": fmt.Sprintf("%d", ctx.Pull.Num),
"PLANFILE": filepath.Join(path, GetPlanFilename(ctx.Workspace, ctx.ProjectName)),
"PROJECT_NAME": ctx.ProjectName,
"PULL_AUTHOR": ctx.Pull.Author,
"PULL_NUM": fmt.Sprintf("%d", ctx.Pull.Num),
"USER_NAME": ctx.User.Username,
"WORKSPACE": ctx.Workspace,
}

finalEnvVars := baseEnvVars
Expand Down
63 changes: 35 additions & 28 deletions server/events/runtime/run_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

func TestRunStepRunner_Run(t *testing.T) {
cases := []struct {
Command string
ExpOut string
ExpErr string
Command string
ProjectName string
ExpOut string
ExpErr string
}{
{
Command: "",
Expand Down Expand Up @@ -46,8 +47,13 @@ func TestRunStepRunner_Run(t *testing.T) {
ExpErr: "exit status 127: running \"lkjlkj\" in",
},
{
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan\n",
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/myworkspace.tfplan project=\n",
},
{
Command: "echo workspace=$WORKSPACE version=$ATLANTIS_TERRAFORM_VERSION dir=$DIR planfile=$PLANFILE project=$PROJECT_NAME",
ProjectName: "my/project/name",
ExpOut: "workspace=myworkspace version=0.11.0 dir=$DIR planfile=$DIR/my::project::name-myworkspace.tfplan project=my/project/name\n",
},
{
Command: "echo base_repo_name=$BASE_REPO_NAME base_repo_owner=$BASE_REPO_OWNER head_repo_name=$HEAD_REPO_NAME head_repo_owner=$HEAD_REPO_OWNER head_branch_name=$HEAD_BRANCH_NAME base_branch_name=$BASE_BRANCH_NAME pull_num=$PULL_NUM pull_author=$PULL_AUTHOR",
Expand All @@ -65,33 +71,34 @@ func TestRunStepRunner_Run(t *testing.T) {
r := runtime.RunStepRunner{
DefaultTFVersion: defaultVersion,
}
ctx := models.ProjectCommandContext{
BaseRepo: models.Repo{
Name: "basename",
Owner: "baseowner",
},
HeadRepo: models.Repo{
Name: "headname",
Owner: "headowner",
},
Pull: models.PullRequest{
Num: 2,
HeadBranch: "add-feat",
BaseBranch: "master",
Author: "acme",
},
User: models.User{
Username: "acme-user",
},
Log: logging.NewNoopLogger(),
Workspace: "myworkspace",
RepoRelDir: "mydir",
TerraformVersion: projVersion,
}
for _, c := range cases {
t.Run(c.Command, func(t *testing.T) {
tmpDir, cleanup := TempDir(t)
defer cleanup()
ctx := models.ProjectCommandContext{
BaseRepo: models.Repo{
Name: "basename",
Owner: "baseowner",
},
HeadRepo: models.Repo{
Name: "headname",
Owner: "headowner",
},
Pull: models.PullRequest{
Num: 2,
HeadBranch: "add-feat",
BaseBranch: "master",
Author: "acme",
},
User: models.User{
Username: "acme-user",
},
Log: logging.NewNoopLogger(),
Workspace: "myworkspace",
RepoRelDir: "mydir",
TerraformVersion: projVersion,
ProjectName: c.ProjectName,
}
out, err := r.Run(ctx, c.Command, tmpDir)
if c.ExpErr != "" {
ErrContains(t, c.ExpErr, err)
Expand Down
4 changes: 2 additions & 2 deletions server/events/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package runtime

import (
"fmt"
"github.com/pkg/errors"
"regexp"
"strings"

"github.com/hashicorp/go-version"
version "github.com/hashicorp/go-version"
"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/terraform"
"github.com/runatlantis/atlantis/server/logging"
Expand Down

0 comments on commit 9115e8f

Please sign in to comment.