Skip to content

Commit

Permalink
sync: conform to PullStatusFetcher interface. (#157)
Browse files Browse the repository at this point in the history
* sync: conform to PullStatusFetcher interface.

* chore: savepoint

* refactor lyft specific mergeability in separate package.
  • Loading branch information
nishkrishnan authored Dec 23, 2021
1 parent b36920d commit 5a86426
Show file tree
Hide file tree
Showing 29 changed files with 1,146 additions and 912 deletions.
13 changes: 8 additions & 5 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/runatlantis/atlantis/server/events/mocks/matchers"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/events/vcs"
lyft_vcs "github.com/runatlantis/atlantis/server/events/vcs/lyft"
vcsmocks "github.com/runatlantis/atlantis/server/events/vcs/mocks"
"github.com/runatlantis/atlantis/server/events/webhooks"
"github.com/runatlantis/atlantis/server/events/yaml"
Expand Down Expand Up @@ -743,10 +744,12 @@ func TestGitHubWorkflowWithPolicyCheck(t *testing.T) {

// Setup test dependencies.
w := httptest.NewRecorder()
When(githubClient.PullIsSQMergeable(AnyRepo(), matchers.AnyModelsPullRequest(), AnyStatus())).ThenReturn(true, nil)
When(githubClient.PullIsApproved(AnyRepo(), matchers.AnyModelsPullRequest())).ThenReturn(models.ApprovalStatus{
IsApproved: true,
}, nil)
// TODO: move to separate test, these checks are lyft specific. Should probably be part of a larger refactor
When(githubClient.GetRepoStatuses(AnyRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]*github.RepoStatus{}, nil)
When(githubClient.GetRepoChecks(AnyRepo(), matchers.AnyModelsPullRequest())).ThenReturn([]*github.CheckRun{}, nil)
When(githubGetter.GetPullRequest(AnyRepo(), AnyInt())).ThenReturn(GitHubPullRequestParsed(headSHA), nil)
When(vcsClient.GetModifiedFiles(AnyRepo(), matchers.AnyModelsPullRequest())).ThenReturn(c.ModifiedFiles, nil)

Expand Down Expand Up @@ -1001,9 +1004,7 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
)

e2eMockGithubClient := vcsmocks.NewMockIGithubClient()
e2ePullReqStatusFetcher := vcs.SQBasedPullStatusFetcher{
GithubClient: e2eMockGithubClient,
}
e2ePullReqStatusFetcher := lyft_vcs.NewSQBasedPullStatusFetcher(e2eMockGithubClient, vcs.NewLyftPullMergeabilityChecker("atlantis"))

applyCommandRunner := events.NewApplyCommandRunner(
e2eVCSClient,
Expand All @@ -1019,7 +1020,7 @@ func setupE2E(t *testing.T, repoDir string) (events_controllers.VCSEventsControl
parallelPoolSize,
silenceNoProjects,
false,
&e2ePullReqStatusFetcher,
e2ePullReqStatusFetcher,
)

approvePoliciesCommandRunner := events.NewApprovePoliciesCommandRunner(
Expand Down Expand Up @@ -1149,6 +1150,7 @@ func GitHubPullRequestParsed(headSHA string) *github.PullRequest {
if headSHA == "" {
headSHA = "13940d121be73f656e2132c6d7b4c8e87878ac8d"
}
cleanstate := "clean"
return &github.PullRequest{
Number: github.Int(2),
State: github.String("open"),
Expand All @@ -1171,6 +1173,7 @@ func GitHubPullRequestParsed(headSHA string) *github.PullRequest {
User: &github.User{
Login: github.String("atlantisbot"),
},
MergeableState: &cleanstate,
}
}

Expand Down
6 changes: 5 additions & 1 deletion server/controllers/github_app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ func (g *GithubAppController) ExchangeCode(w http.ResponseWriter, r *http.Reques

g.Logger.Debug("Exchanging GitHub app code for app credentials")
creds := &vcs.GithubAnonymousCredentials{}
client, err := vcs.NewGithubClient(g.GithubHostname, creds, g.Logger, g.GithubStatusName)

// TODO: unify this in a single inject.go file
mergeabilityChecker := vcs.NewLyftPullMergeabilityChecker(g.GithubStatusName)
client, err := vcs.NewGithubClient(g.GithubHostname, creds, g.Logger, mergeabilityChecker)

if err != nil {
g.respond(w, logging.Error, http.StatusInternalServerError, "Failed to exchange code for github app: %s", err)
return
Expand Down
2 changes: 1 addition & 1 deletion server/events/apply_requirement_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (a *AggregateApplyRequirements) ValidateProject(repoDir string, ctx models.
for _, req := range ctx.ApplyRequirements {
switch req {
case raw.ApprovedApplyRequirement:
if !ctx.PullReqStatus.Approved.IsApproved {
if !ctx.PullReqStatus.ApprovalStatus.IsApproved {
return "Pull request must be approved by at least one person other than the author before running apply.", nil
}
// this should come before mergeability check since mergeability is a superset of this check.
Expand Down
11 changes: 3 additions & 8 deletions server/events/command_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,10 @@ type CommandContext struct {
// See https://help.github.com/articles/about-pull-request-merges/.
HeadRepo models.Repo
Pull models.PullRequest
Scope stats.Scope
// User is the user that triggered this command.
User models.User
Log logging.SimpleLogging
Scope stats.Scope
// PullMergeable is true if Pull is able to be merged. This is available in
// the CommandContext because we want to collect this information before we
// set our own build statuses which can affect mergeability if users have
// required the Atlantis status to be successful prior to merging.
PullMergeable bool
User models.User
Log logging.SimpleLogging

// Current PR state
PullRequestStatus models.PullReqStatus
Expand Down
7 changes: 3 additions & 4 deletions server/events/command_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
stats "github.com/lyft/gostats"
"github.com/runatlantis/atlantis/server/core/db"
"github.com/runatlantis/atlantis/server/events/vcs"
lyft_vcs "github.com/runatlantis/atlantis/server/events/vcs/lyft"
"github.com/runatlantis/atlantis/server/events/yaml/valid"
"github.com/runatlantis/atlantis/server/logging"

Expand Down Expand Up @@ -136,9 +137,7 @@ func setup(t *testing.T) *vcsmocks.MockClient {
defaultBoltDB,
)

pullReqStatusFetcher := vcs.SQBasedPullStatusFetcher{
GithubClient: githubClient,
}
pullReqStatusFetcher := lyft_vcs.NewSQBasedPullStatusFetcher(githubClient, vcs.NewLyftPullMergeabilityChecker("atlantis"))

applyCommandRunner = events.NewApplyCommandRunner(
vcsClient,
Expand All @@ -154,7 +153,7 @@ func setup(t *testing.T) *vcsmocks.MockClient {
parallelPoolSize,
SilenceNoProjects,
false,
&pullReqStatusFetcher,
pullReqStatusFetcher,
)

approvePoliciesCommandRunner = events.NewApprovePoliciesCommandRunner(
Expand Down
14 changes: 6 additions & 8 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const (
LogStreamingClearMsg = "\n-----Starting New Process-----"
)

type PullReqStatus struct {
ApprovalStatus ApprovalStatus
Mergeable bool
SQLocked bool
}

// Repo is a VCS repository.
type Repo struct {
// FullName is the owner and repo name separated
Expand Down Expand Up @@ -145,12 +151,6 @@ func NewRepo(vcsHostType VCSHostType, repoFullName string, cloneURL string, vcsU
}, nil
}

type PullReqStatus struct {
Approved ApprovalStatus
Mergeable bool
SQLocked bool
}

type ApprovalStatus struct {
IsApproved bool
ApprovedBy string
Expand Down Expand Up @@ -385,8 +385,6 @@ type ProjectCommandContext struct {
Log logging.SimpleLogging
// Scope is the scope for reporting stats setup for this context
Scope stats.Scope
// PullMergeable is true if the pull request for this project is able to be merged.
PullMergeable bool
// PullReqStatus holds state about the PR that requires additional computation outside models.PullRequest
PullReqStatus PullReqStatus
// CurrentProjectPlanStatus is the status of the current project prior to this command.
Expand Down
Loading

0 comments on commit 5a86426

Please sign in to comment.