Skip to content

Commit

Permalink
fix: Enable --skip-clone-no-changes for fork PRs (#3891)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulminator committed Oct 25, 2023
1 parent 5e1f50b commit a0ca05d
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 21 deletions.
12 changes: 11 additions & 1 deletion server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,17 @@ func (p *DefaultProjectCommandBuilder) buildAllCommandsByCfg(ctx *command.Contex

if p.SkipCloneNoChanges && p.VCSClient.SupportsSingleFileDownload(ctx.Pull.BaseRepo) {
repoCfgFile := p.GlobalCfg.RepoConfigFile(ctx.Pull.BaseRepo.ID())
hasRepoCfg, repoCfgData, err := p.VCSClient.GetFileContent(ctx.Pull, repoCfgFile)

var hasRepoCfg bool
var repoCfgData []byte
var err error
if ctx.HeadRepo.Owner != ctx.Pull.BaseRepo.Owner {
hasRepoCfg, repoCfgData, err = p.VCSClient.GetFileContent(ctx.HeadRepo, ctx.Pull.HeadBranch, repoCfgFile)

} else {
hasRepoCfg, repoCfgData, err = p.VCSClient.GetFileContent(ctx.Pull.BaseRepo, ctx.Pull.HeadBranch, repoCfgFile)
}

if err != nil {
return nil, errors.Wrapf(err, "downloading %s", repoCfgFile)
}
Expand Down
2 changes: 1 addition & 1 deletion server/events/project_command_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ parallel_plan: true`,
vcsClient := vcsmocks.NewMockClient()
When(vcsClient.GetModifiedFiles(Any[models.Repo](), Any[models.PullRequest]())).ThenReturn(c.ModifiedFiles, nil)
When(vcsClient.SupportsSingleFileDownload(Any[models.Repo]())).ThenReturn(true)
When(vcsClient.GetFileContent(Any[models.PullRequest](), Any[string]())).ThenReturn(true, []byte(c.AtlantisYAML), nil)
When(vcsClient.GetFileContent(Any[models.Repo](), Any[string](), Any[string]())).ThenReturn(true, []byte(c.AtlantisYAML), nil)
workingDir := mocks.NewMockWorkingDir()

logger := logging.NewNoopLogger(t)
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 @@ -398,7 +398,7 @@ func (g *AzureDevopsClient) SupportsSingleFileDownload(repo models.Repo) bool {
return false
}

func (g *AzureDevopsClient) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
func (g *AzureDevopsClient) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {
return false, []byte{}, fmt.Errorf("Not Implemented")
}

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 @@ -274,7 +274,7 @@ func (b *Client) SupportsSingleFileDownload(models.Repo) bool {
// GetFileContent a repository file content from VCS (which support fetch a single file from repository)
// The first return value indicates whether the repo contains a file or not
// if BaseRepo had a file, its content will placed on the second return value
func (b *Client) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
func (b *Client) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {
return false, []byte{}, fmt.Errorf("Not Implemented")
}

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 @@ -358,7 +358,7 @@ func (b *Client) SupportsSingleFileDownload(repo models.Repo) bool {
// GetFileContent a repository file content from VCS (which support fetch a single file from repository)
// The first return value indicates whether the repo contains a file or not
// if BaseRepo had a file, its content will placed on the second return value
func (b *Client) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
func (b *Client) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {
return false, []byte{}, fmt.Errorf("not implemented")
}

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 @@ -46,7 +46,7 @@ type Client interface {
// GetFileContent a repository file content from VCS (which support fetch a single file from repository)
// The first return value indicates whether the repo contains a file or not
// if BaseRepo had a file, its content will placed on the second return value
GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error)
GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error)
SupportsSingleFileDownload(repo models.Repo) bool
GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error)

Expand Down
9 changes: 5 additions & 4 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,11 @@ func (g *GithubClient) ExchangeCode(code string) (*GithubAppTemporarySecrets, er
// GetFileContent a repository file content from VCS (which support fetch a single file from repository)
// The first return value indicates whether the repo contains a file or not
// if BaseRepo had a file, its content will placed on the second return value
func (g *GithubClient) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
opt := github.RepositoryContentGetOptions{Ref: pull.HeadBranch}
fileContent, _, resp, err := g.client.Repositories.GetContents(g.ctx, pull.BaseRepo.Owner, pull.BaseRepo.Name, fileName, &opt)
g.logger.Debug("GET /repos/%v/%v/contents/%s returned: %v", pull.BaseRepo.Owner, pull.BaseRepo.Name, fileName, resp.StatusCode)
func (g *GithubClient) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {
opt := github.RepositoryContentGetOptions{Ref: branch}

fileContent, _, resp, err := g.client.Repositories.GetContents(g.ctx, repo.Owner, repo.Name, fileName, &opt)
g.logger.Debug("GET /repos/%v/%v/contents/%s returned: %v", repo.Owner, repo.Name, fileName, resp.StatusCode)

if resp.StatusCode == http.StatusNotFound {
return false, []byte{}, nil
Expand Down
9 changes: 5 additions & 4 deletions server/events/vcs/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,12 @@ func (g *GitlabClient) GetTeamNamesForUser(repo models.Repo, user models.User) (
// GetFileContent a repository file content from VCS (which support fetch a single file from repository)
// The first return value indicates whether the repo contains a file or not
// if BaseRepo had a file, its content will placed on the second return value
func (g *GitlabClient) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
opt := gitlab.GetRawFileOptions{Ref: gitlab.String(pull.HeadBranch)}
func (g *GitlabClient) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {

bytes, resp, err := g.Client.RepositoryFiles.GetRawFile(pull.BaseRepo.FullName, fileName, &opt)
g.logger.Debug("GET /projects/%s/repository/files/%s/raw returned: %d", pull.BaseRepo.FullName, fileName, resp.StatusCode)
opt := gitlab.GetRawFileOptions{Ref: gitlab.String(branch)}

bytes, resp, err := g.Client.RepositoryFiles.GetRawFile(repo.FullName, fileName, &opt)
g.logger.Debug("GET /projects/%s/repository/files/%s/raw returned: %d", repo.FullName, fileName, resp.StatusCode)
if resp.StatusCode == http.StatusNotFound {
return false, []byte{}, nil
}
Expand Down
8 changes: 4 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 @@ -67,7 +67,7 @@ func (a *NotConfiguredVCSClient) SupportsSingleFileDownload(repo models.Repo) bo
return false
}

func (a *NotConfiguredVCSClient) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
func (a *NotConfiguredVCSClient) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {
return true, []byte{}, a.err()
}
func (a *NotConfiguredVCSClient) GetCloneURL(VCSHostType models.VCSHostType, repo string) (string, error) {
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 @@ -96,8 +96,8 @@ func (d *ClientProxy) GetTeamNamesForUser(repo models.Repo, user models.User) ([
return d.clients[repo.VCSHost.Type].GetTeamNamesForUser(repo, user)
}

func (d *ClientProxy) GetFileContent(pull models.PullRequest, fileName string) (bool, []byte, error) {
return d.clients[pull.BaseRepo.VCSHost.Type].GetFileContent(pull, fileName)
func (d *ClientProxy) GetFileContent(repo models.Repo, branch string, fileName string) (bool, []byte, error) {
return d.clients[repo.VCSHost.Type].GetFileContent(repo, branch, fileName)
}

func (d *ClientProxy) SupportsSingleFileDownload(repo models.Repo) bool {
Expand Down

0 comments on commit a0ca05d

Please sign in to comment.