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 PROJECT_NAME env var to custom run step #578

Merged
merged 3 commits into from
Apr 8, 2019
Merged
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/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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikovirtala what I intended was you add the ProjectName here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. You give the ProjectName a value inside the cases struct. - That's what I was wondering how to do it. As easy as its part. :)

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