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

fix: set ctx.PullRequestStatus in import_command_runner for import_requirements #2936

Merged
merged 2 commits into from
Jan 8, 2023

Conversation

krrrr38
Copy link
Contributor

@krrrr38 krrrr38 commented Jan 5, 2023

what

why

import_requirements not works fine, cause ctx.PullRequestStatus needs to be set by command runner like apply_command_runner

tests

diff --git a/server/events/command_requirement_handler.go b/server/events/command_requirement_handler.go
index 5b487c51..d86c4409 100644
--- a/server/events/command_requirement_handler.go
+++ b/server/events/command_requirement_handler.go
@@ -1,6 +1,8 @@
 package events

 import (
+       "fmt"
+
        "github.com/runatlantis/atlantis/server/core/config/raw"
        "github.com/runatlantis/atlantis/server/core/config/valid"
        "github.com/runatlantis/atlantis/server/events/command"
@@ -48,7 +50,7 @@ func (a *DefaultCommandRequirementHandler) ValidateImportProject(repoDir string,
                switch req {
                case raw.ApprovedRequirement:
                        if !ctx.PullReqStatus.ApprovalStatus.IsApproved {
-                               return "Pull request must be approved by at least one person other than the author before running import.", nil
+                               return fmt.Sprintf("Pull request must be approved by at least one person other than the author before running import. ctx.PullReqStatus=%+v", ctx.PullReqStatus), nil

references

// only if they rely on the mergeability requirement.
// All PullRequestStatus fields are set to false by default when error.
ctx.Log.Warn("unable to get pull request status: %s. Continuing with mergeable and approved assumed false", err)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same logic with apply_command_runner

// Get the mergeable status before we set any build statuses of our own.
// We do this here because when we set a "Pending" status, if users have
// required the Atlantis status checks to pass, then we've now changed
// the mergeability status of the pull request.
// This sets the approved, mergeable, and sqlocked status in the context.
ctx.PullRequestStatus, err = a.pullReqStatusFetcher.FetchPullStatus(baseRepo, pull, a.VCSStatusName)
if err != nil {
// On error we continue the request with mergeable assumed false.
// We want to continue because not all apply's will need this status,
// only if they rely on the mergeability requirement.
// All PullRequestStatus fields are set to false by default when error.
ctx.Log.Warn("unable to get pull request status: %s. Continuing with mergeable and approved assumed false", err)
}

}
}

func (f *pullReqStatusFetcher) FetchPullStatus(repo models.Repo, pull models.PullRequest, vcsstatusname string) (pullStatus models.PullReqStatus, err error) {
approvalStatus, err := f.client.PullIsApproved(repo, pull)
func (f *pullReqStatusFetcher) FetchPullStatus(pull models.PullRequest) (pullStatus models.PullReqStatus, err error) {
Copy link
Contributor Author

@krrrr38 krrrr38 Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce following parameters

  • repo models.Repo
    • pull models.PullRequest is enough.
  • vcsstatusname string
    • this is a server configuration, so it should have be in the pullReqStatusFetcher struct.


importCommandRunner.Run(ctx, cmd)

Assert(t, ctx.PullRequestStatus.Mergeable == true, "PullRequestStatus must be set for import_requirements")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this test case

@krrrr38 krrrr38 marked this pull request as ready for review January 5, 2023 08:15
@krrrr38 krrrr38 requested a review from a team as a code owner January 5, 2023 08:15
@krrrr38
Copy link
Contributor Author

krrrr38 commented Jan 5, 2023

It might be better to remove ctx.PullRequestStatus and fetch it on command_requirement_handler.go 🤔 This is because ctx.PullRequestStatus should not be set as mutable value.

I tried to move a.PullReqStatusFetcher.FetchPullStatus(ctx.Pull) into command_requirement_handler. But if there are multiple projects, it calls multiple times even if PR is just one. So keep asis impl.

@krrrr38 krrrr38 marked this pull request as draft January 5, 2023 10:30
@krrrr38 krrrr38 marked this pull request as ready for review January 5, 2023 18:27
Copy link
Member

@GenPage GenPage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nitrocode nitrocode modified the milestones: 0.22.2, 0.22.3 Jan 5, 2023
@nitrocode nitrocode merged commit ca50756 into runatlantis:main Jan 8, 2023
@nitrocode
Copy link
Member

nitrocode commented Jan 8, 2023

Thanks @krrrr38 for the fix.

I'm unsure how this PR was not merged into the last version. My mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

import_requirements doesn't check current PullRequestStatus
3 participants