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

Allow absolute project dirs and fix AZDO #888

Merged
merged 2 commits into from
Jan 3, 2020
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
13 changes: 9 additions & 4 deletions server/events/vcs/azuredevops_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -49,8 +50,8 @@ func NewAzureDevopsClient(hostname string, token string) (*AzureDevopsClient, er
return client, nil
}

// GetModifiedFiles returns the names of files that were modified in the merge request.
// The names include the path to the file from the repo root, ex. parent/child/file.txt.
// GetModifiedFiles returns the names of files that were modified in the merge request
// relative to the repo root, e.g. parent/child/file.txt.
func (g *AzureDevopsClient) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
var files []string

Expand All @@ -66,13 +67,17 @@ func (g *AzureDevopsClient) GetModifiedFiles(repo models.Repo, pull models.PullR

for _, change := range r.Changes {
item := change.GetItem()
files = append(files, item.GetPath())
// Convert the path to a relative path from the repo's root.
relativePath := filepath.Clean("./" + item.GetPath())
files = append(files, relativePath)

// If the file was renamed, we'll want to run plan in the directory
// it was moved from as well.
changeType := azuredevops.Rename.String()
if change.ChangeType == &changeType {
files = append(files, change.GetSourceServerItem())
// Convert the path to a relative path from the repo's root.
relativePath = filepath.Clean("./" + change.GetSourceServerItem())
files = append(files, relativePath)
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/events/vcs/azuredevops_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestAzureDevopsClient_GetModifiedFiles(t *testing.T) {
"changeType": "add"
}
]}`
resp := fmt.Sprintf(itemRespTemplate, "file1.txt", "file1.txt", "file2.txt", "file2.txt")
resp := fmt.Sprintf(itemRespTemplate, "/file1.txt", "/file1.txt", "/file2.txt", "/file2.txt")
testServer := httptest.NewTLSServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
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 @@ -38,8 +38,8 @@ func NewClient(httpClient *http.Client, username string, password string, atlant
}
}

// GetModifiedFiles returns the names of files that were modified in the merge request.
// The names include the path to the file from the repo root, ex. parent/child/file.txt.
// GetModifiedFiles returns the names of files that were modified in the merge request
// relative to the repo root, e.g. parent/child/file.txt.
func (b *Client) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
var files []string

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 @@ -58,8 +58,8 @@ func NewClient(httpClient *http.Client, username string, password string, baseUR
}, nil
}

// GetModifiedFiles returns the names of files that were modified in the merge request.
// The names include the path to the file from the repo root, ex. parent/child/file.txt.
// GetModifiedFiles returns the names of files that were modified in the merge request
// relative to the repo root, e.g. parent/child/file.txt.
func (b *Client) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
var files []string

Expand Down
2 changes: 2 additions & 0 deletions server/events/vcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

// Client is used to make API calls to a VCS host like GitHub or GitLab.
type Client interface {
// GetModifiedFiles returns the names of files that were modified in the merge request
// relative to the repo root, e.g. parent/child/file.txt.
GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error)
CreateComment(repo models.Repo, pullNum int, comment string) error
PullIsApproved(repo models.Repo, pull models.PullRequest) (bool, error)
Expand Down
4 changes: 2 additions & 2 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ func NewGithubClient(hostname string, user string, pass string) (*GithubClient,
}, 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.
// GetModifiedFiles returns the names of files that were modified in the pull request
// relative to the repo root, e.g. parent/child/file.txt.
func (g *GithubClient) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
var files []string
nextPage := 0
Expand Down
4 changes: 2 additions & 2 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func NewGitlabClient(hostname string, token string, logger *logging.SimpleLogger
return client, nil
}

// GetModifiedFiles returns the names of files that were modified in the merge request.
// The names include the path to the file from the repo root, ex. parent/child/file.txt.
// GetModifiedFiles returns the names of files that were modified in the merge request
// relative to the repo root, e.g. parent/child/file.txt.
func (g *GitlabClient) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]string, error) {
const maxPerPage = 100
var files []string
Expand Down
8 changes: 4 additions & 4 deletions server/events/yaml/raw/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ func (p Project) Validate() error {

func (p Project) ToValid() valid.Project {
var v valid.Project
cleanedDir := filepath.Clean(*p.Dir)
if cleanedDir == "/" {
cleanedDir = "."
}
// Prepend ./ and then run .Clean() so we're guaranteed to have a relative
// directory. This is necessary because we use this dir without sanitation
// in DefaultProjectFinder.
cleanedDir := filepath.Clean("./" + *p.Dir)
v.Dir = cleanedDir

if p.Workspace == nil || *p.Workspace == "" {
Expand Down
60 changes: 59 additions & 1 deletion server/events/yaml/raw/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,9 @@ func TestProject_ToValid(t *testing.T) {
},
},
},
// Directories.
{
description: "dir with /",
description: "dir set to /",
input: raw.Project{
Dir: String("/"),
},
Expand All @@ -295,6 +296,20 @@ func TestProject_ToValid(t *testing.T) {
},
},
},
{
description: "dir starting with /",
input: raw.Project{
Dir: String("/a/b/c"),
},
exp: valid.Project{
Dir: "a/b/c",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
},
},
{
description: "dir with trailing slash",
input: raw.Project{
Expand Down Expand Up @@ -324,6 +339,49 @@ func TestProject_ToValid(t *testing.T) {
},
},
},
{
description: "dir set to ./",
input: raw.Project{
Dir: String("./"),
},
exp: valid.Project{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
},
},
{
description: "dir set to ././",
input: raw.Project{
Dir: String("././"),
},
exp: valid.Project{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
},
},
{
description: "dir set to .",
input: raw.Project{
Dir: String("."),
},
exp: valid.Project{
Dir: ".",
Workspace: "default",
Autoplan: valid.Autoplan{
WhenModified: []string{"**/*.tf*"},
Enabled: true,
},
},
},

{
description: "workspace set to empty string",
input: raw.Project{
Expand Down