Skip to content

Commit

Permalink
Refactor --skip-clone-no-changes work.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkysow committed Aug 18, 2020
1 parent dd8bbe9 commit 44f4972
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 51 deletions.
1 change: 1 addition & 0 deletions server/events/matchers/models_pullrequest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/events/matchers/models_repo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/events/matchers/ptr_to_logging_simplelogger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions server/events/mock_workingdir_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,19 @@ func (p *DefaultProjectCommandBuilder) BuildApplyCommands(ctx *CommandContext, c
func (p *DefaultProjectCommandBuilder) buildPlanAllCommands(ctx *CommandContext, commentFlags []string, verbose bool) ([]models.ProjectCommandContext, error) {
// We'll need the list of modified files.
modifiedFiles, err := p.VCSClient.GetModifiedFiles(ctx.BaseRepo, ctx.Pull)

if err != nil {
return nil, err
}
ctx.Log.Debug("%d files were modified in this pull request", len(modifiedFiles))

if p.SkipCloneNoChanges && p.VCSClient.IsSupportDownloadSingleFile(ctx.BaseRepo) {
if p.SkipCloneNoChanges && p.VCSClient.SupportsSingleFileDownload(ctx.BaseRepo) {
hasRepoCfg, repoCfgData, err := p.VCSClient.DownloadRepoConfigFile(ctx.Pull)
if err != nil {
return nil, errors.Wrapf(err, "downloading %s", yaml.AtlantisYAMLFilename)
}

if hasRepoCfg {
repoCfg, err := p.ParserValidator.ParseRepoCfg(repoCfgData, "", p.GlobalCfg, ctx.BaseRepo.ID())
repoCfg, err := p.ParserValidator.ParseRepoCfgData(repoCfgData, p.GlobalCfg, ctx.BaseRepo.ID())
if err != nil {
return nil, errors.Wrapf(err, "parsing %s", yaml.AtlantisYAMLFilename)
}
Expand All @@ -130,6 +129,9 @@ func (p *DefaultProjectCommandBuilder) buildPlanAllCommands(ctx *CommandContext,
ctx.Log.Info("skipping repo clone since no project was modified")
return []models.ProjectCommandContext{}, nil
}
// NOTE: We discard this work here and end up doing it again after
// cloning to ensure all the return values are set properly with
// the actual clone directory.
}
}

Expand Down Expand Up @@ -158,7 +160,7 @@ func (p *DefaultProjectCommandBuilder) buildPlanAllCommands(ctx *CommandContext,
if hasRepoCfg {
// If there's a repo cfg then we'll use it to figure out which projects
// should be planed.
repoCfg, err := p.ParserValidator.ParseRepoCfg([]byte{}, repoDir, p.GlobalCfg, ctx.BaseRepo.ID())
repoCfg, err := p.ParserValidator.ParseRepoCfg(repoDir, p.GlobalCfg, ctx.BaseRepo.ID())
if err != nil {
return nil, errors.Wrapf(err, "parsing %s", yaml.AtlantisYAMLFilename)
}
Expand Down Expand Up @@ -340,7 +342,7 @@ func (p *DefaultProjectCommandBuilder) getCfg(ctx *CommandContext, projectName s
}

var repoConfig valid.RepoCfg
repoConfig, err = p.ParserValidator.ParseRepoCfg([]byte{}, repoDir, p.GlobalCfg, ctx.BaseRepo.ID())
repoConfig, err = p.ParserValidator.ParseRepoCfg(repoDir, p.GlobalCfg, ctx.BaseRepo.ID())
if err != nil {
return
}
Expand Down
40 changes: 40 additions & 0 deletions server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,43 @@ projects:
})
}
}

// Test that we don't clone the repo if there were no changes based on the atlantis.yaml file.
func TestDefaultProjectCommandBuilder_SkipCloneNoChanges(t *testing.T) {
atlantisYAML := `
version: 3
projects:
- dir: dir1`

RegisterMockTestingT(t)
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]string{"main.tf"}, nil)
When(vcsClient.SupportsSingleFileDownload(matchers.AnyModelsRepo())).ThenReturn(true)
When(vcsClient.DownloadRepoConfigFile(matchers.AnyModelsPullRequest())).ThenReturn(true, []byte(atlantisYAML), nil)
workingDir := mocks.NewMockWorkingDir()

builder := &events.DefaultProjectCommandBuilder{
WorkingDirLocker: events.NewDefaultWorkingDirLocker(),
WorkingDir: workingDir,
ParserValidator: &yaml.ParserValidator{},
VCSClient: vcsClient,
ProjectFinder: &events.DefaultProjectFinder{},
CommentBuilder: &events.CommentParser{},
GlobalCfg: valid.NewGlobalCfg(true, false, false),
SkipCloneNoChanges: true,
}

var actCtxs []models.ProjectCommandContext
var err error
actCtxs, err = builder.BuildAutoplanCommands(&events.CommandContext{
BaseRepo: models.Repo{},
HeadRepo: models.Repo{},
Pull: models.PullRequest{},
User: models.User{},
Log: nil,
PullMergeable: true,
})
Ok(t, err)
Equals(t, 0, len(actCtxs))
workingDir.VerifyWasCalled(Never()).Clone(matchers.AnyPtrToLoggingSimpleLogger(), matchers.AnyModelsRepo(), matchers.AnyModelsRepo(), matchers.AnyModelsPullRequest(), AnyString())
}
10 changes: 8 additions & 2 deletions server/events/project_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,14 @@ func (p *DefaultProjectFinder) DetermineProjectsViaConfig(log *logging.SimpleLog
}
if match {
log.Debug("file %q matched pattern", file)
// Skipping checking existing if remote atlantis.yaml was used
if len(absRepoDir) != 0 {
// If we're checking using an atlantis.yaml file we downloaded
// directly from the repo (when doing a no-clone check) then
// absRepoDir will be empty. Since we didn't clone the repo
// yet we can't do this check. If there was a file modified
// in a deleted directory then when we finally do clone the repo
// we'll call this function again and then we'll detect the
// directory was deleted.
if absRepoDir != "" {
_, err := os.Stat(filepath.Join(absRepoDir, project.Dir))
if err == nil {
projects = append(projects, project)
Expand Down
2 changes: 1 addition & 1 deletion server/events/vcs/azuredevops_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func SplitAzureDevopsRepoFullName(repoFullName string) (owner string, project st
return repoFullName[:lastSlashIdx], "", repoFullName[lastSlashIdx+1:]
}

func (g *AzureDevopsClient) IsSupportDownloadSingleFile(repo models.Repo) bool {
func (g *AzureDevopsClient) SupportsSingleFileDownload(repo models.Repo) bool {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion server/events/vcs/bitbucketcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (b *Client) makeRequest(method string, path string, reqBody io.Reader) ([]b
return respBody, nil
}

func (b *Client) IsSupportDownloadSingleFile(models.Repo) bool {
func (b *Client) SupportsSingleFileDownload(models.Repo) bool {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion server/events/vcs/bitbucketserver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (b *Client) makeRequest(method string, path string, reqBody io.Reader) ([]b
return respBody, nil
}

func (b *Client) IsSupportDownloadSingleFile(repo models.Repo) bool {
func (b *Client) SupportsSingleFileDownload(repo models.Repo) bool {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion server/events/vcs/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ type Client interface {
// The first return value indicate that repo contain atlantis.yaml or not
// if BaseRepo had one repo config file, its content will placed on the second return value
DownloadRepoConfigFile(pull models.PullRequest) (bool, []byte, error)
IsSupportDownloadSingleFile(repo models.Repo) bool
SupportsSingleFileDownload(repo models.Repo) bool
}
5 changes: 3 additions & 2 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
"context"
"encoding/base64"
"fmt"
"github.com/runatlantis/atlantis/server/events/yaml"
"net/http"
"net/url"
"strings"

"github.com/runatlantis/atlantis/server/events/yaml"

"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/vcs/common"
"github.com/runatlantis/atlantis/server/logging"
Expand Down Expand Up @@ -363,6 +364,6 @@ func (g *GithubClient) DownloadRepoConfigFile(pull models.PullRequest) (bool, []
return true, decodedData, nil
}

func (g *GithubClient) IsSupportDownloadSingleFile(repo models.Repo) bool {
func (g *GithubClient) SupportsSingleFileDownload(repo models.Repo) bool {
return true
}
5 changes: 3 additions & 2 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ package vcs

import (
"fmt"
"github.com/runatlantis/atlantis/server/events/yaml"
"net"
"net/http"
"net/url"
"strings"

"github.com/runatlantis/atlantis/server/events/yaml"

"github.com/runatlantis/atlantis/server/events/vcs/common"

version "github.com/hashicorp/go-version"
Expand Down Expand Up @@ -284,6 +285,6 @@ func (g *GitlabClient) DownloadRepoConfigFile(pull models.PullRequest) (bool, []
return true, bytes, nil
}

func (g *GitlabClient) IsSupportDownloadSingleFile(repo models.Repo) bool {
func (g *GitlabClient) SupportsSingleFileDownload(repo models.Repo) bool {
return true
}
20 changes: 20 additions & 0 deletions server/events/vcs/mocks/matchers/slice_of_byte.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 88 additions & 4 deletions server/events/vcs/mocks/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/events/vcs/not_configured_vcs_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (a *NotConfiguredVCSClient) err() error {
return fmt.Errorf("atlantis was not configured to support repos from %s", a.Host.String())
}

func (a *NotConfiguredVCSClient) IsSupportDownloadSingleFile(repo models.Repo) bool {
func (a *NotConfiguredVCSClient) SupportsSingleFileDownload(repo models.Repo) bool {
return false
}

Expand Down
4 changes: 2 additions & 2 deletions server/events/vcs/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ func (d *ClientProxy) DownloadRepoConfigFile(pull models.PullRequest) (bool, []b
return d.clients[pull.BaseRepo.VCSHost.Type].DownloadRepoConfigFile(pull)
}

func (d *ClientProxy) IsSupportDownloadSingleFile(repo models.Repo) bool {
return d.clients[repo.VCSHost.Type].IsSupportDownloadSingleFile(repo)
func (d *ClientProxy) SupportsSingleFileDownload(repo models.Repo) bool {
return d.clients[repo.VCSHost.Type].SupportsSingleFileDownload(repo)
}
Loading

0 comments on commit 44f4972

Please sign in to comment.