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

Include src of moved files in list of modified files #415

Merged
merged 2 commits into from
Jan 7, 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
6 changes: 6 additions & 0 deletions server/events/vcs/bitbucketserver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func (b *Client) GetModifiedFiles(repo models.Repo, pull models.PullRequest) ([]
}
for _, v := range changes.Values {
files = append(files, *v.Path.ToString)

// If the file was renamed, we'll want to run plan in the directory
// it was moved from as well.
if v.SrcPath != nil {
files = append(files, *v.SrcPath.ToString)
}
}
if *changes.IsLastPage {
break
Expand Down
3 changes: 3 additions & 0 deletions server/events/vcs/bitbucketserver/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type Changes struct {
Path struct {
ToString *string `json:"toString,omitempty" validate:"required"`
} `json:"path,omitempty" validate:"required"`
SrcPath *struct {
ToString *string `json:"toString,omitempty"`
} `json:"srcPath,omitempty"`
} `json:"values,omitempty" validate:"required"`
NextPageStart *int `json:"nextPageStart,omitempty"`
IsLastPage *bool `json:"isLastPage,omitempty" validate:"required"`
Expand Down
6 changes: 6 additions & 0 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func (g *GithubClient) GetModifiedFiles(repo models.Repo, pull models.PullReques
}
for _, f := range pageFiles {
files = append(files, f.GetFilename())

// If the file was renamed, we'll want to run plan in the directory
// it was moved from as well.
if f.GetStatus() == "renamed" {
files = append(files, f.GetPreviousFilename())
}
}
if resp.NextPage == 0 {
break
Expand Down
55 changes: 55 additions & 0 deletions server/events/vcs/github_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,61 @@ func TestGithubClient_GetModifiedFiles(t *testing.T) {
Equals(t, []string{"file1.txt", "file2.txt"}, files)
}

// GetModifiedFiles should include the source and destination of a moved
// file.
func TestGithubClient_GetModifiedFilesMovedFile(t *testing.T) {
resp := `[
{
"sha": "bbcd538c8e72b8c175046e27cc8f907076331401",
"filename": "new/filename.txt",
"previous_filename": "previous/filename.txt",
"status": "renamed",
"additions": 103,
"deletions": 21,
"changes": 124,
"blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
"raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
"contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e",
"patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
}
]`
testServer := httptest.NewTLSServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.RequestURI {
// The first request should hit this URL.
case "/api/v3/repos/owner/repo/pulls/1/files?per_page=300":
w.Write([]byte(resp)) // nolint: errcheck
return
default:
t.Errorf("got unexpected request at %q", r.RequestURI)
http.Error(w, "not found", http.StatusNotFound)
return
}
}))

testServerURL, err := url.Parse(testServer.URL)
Ok(t, err)
client, err := vcs.NewGithubClient(testServerURL.Host, "user", "pass")
Ok(t, err)
defer disableSSLVerification()()

files, err := client.GetModifiedFiles(models.Repo{
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
CloneURL: "",
SanitizedCloneURL: "",
VCSHost: models.VCSHost{
Type: models.Github,
Hostname: "github.com",
},
}, models.PullRequest{
Num: 1,
})
Ok(t, err)
Equals(t, []string{"new/filename.txt", "previous/filename.txt"}, files)
}

func TestGithubClient_UpdateStatus(t *testing.T) {
cases := []struct {
status models.CommitStatus
Expand Down
6 changes: 6 additions & 0 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func (g *GitlabClient) GetModifiedFiles(repo models.Repo, pull models.PullReques

for _, f := range mr.Changes {
files = append(files, f.NewPath)

// If the file was renamed, we'll want to run plan in the directory
// it was moved from as well.
if f.RenamedFile {
files = append(files, f.OldPath)
}
}
if resp.NextPage == 0 {
break
Expand Down