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: Fix GitHub pull request mergeability for multiple required workflow runs #5057

Merged
merged 21 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0bbf42a
Fix GitHub pull request mergeability for workflows with multiple runs
ajax-ryzhyi-r Nov 4, 2024
246abf5
Merge remote-tracking branch 'origin/main' into fix_github_pr_mergeab…
ajax-ryzhyi-r Nov 4, 2024
faecb4b
Merge remote-tracking branch 'origin/main' into fix_github_pr_mergeab…
ajax-ryzhyi-r Nov 4, 2024
ffebd15
Refactor fix to use runNumber for determining latest workflow run
ajax-ryzhyi-r Nov 4, 2024
8ae2d46
Refactor fix to use runNumber for determining latest workflow run
ajax-ryzhyi-r Nov 4, 2024
c354153
Refactor fix to use runNumber for determining latest workflow run
ajax-ryzhyi-r Nov 4, 2024
d7bafaa
Refactor fix to use runNumber for determining latest workflow run
ajax-ryzhyi-r Nov 4, 2024
e6a927e
Add trailing whitespaces for test files
ajax-ryzhyi-r Nov 4, 2024
69dbdd9
Merge branch 'main' into fix_github_pr_mergeability
ajax-ryzhyi-r Nov 5, 2024
7ac6b8d
Merge branch 'main' into fix_github_pr_mergeability
X-Guardian Nov 5, 2024
99c00e3
Revert EOF LFs in test files
ajax-ryzhyi-r Nov 6, 2024
7dc0b29
Merge branch 'main' into fix_github_pr_mergeability
ajax-ryzhyi-r Nov 6, 2024
4594e28
Fix comments
ajax-ryzhyi-r Nov 7, 2024
101e7e7
Fix comments
ajax-ryzhyi-r Nov 7, 2024
c97685d
Merge branch 'main' into fix_github_pr_mergeability
ajax-ryzhyi-r Nov 7, 2024
df00c99
Fix comment
ajax-ryzhyi-r Nov 12, 2024
bd76d67
Merge branch 'main' into fix_github_pr_mergeability
ajax-ryzhyi-r Nov 12, 2024
c107dae
Merge branch 'main' into fix_github_pr_mergeability
X-Guardian Nov 16, 2024
b1ab021
Merge branch 'main' into fix_github_pr_mergeability
X-Guardian Nov 17, 2024
ea10ec2
Merge branch 'main' into fix_github_pr_mergeability
X-Guardian Nov 17, 2024
7f95b8f
Merge branch 'main' into fix_github_pr_mergeability
X-Guardian Nov 19, 2024
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
52 changes: 44 additions & 8 deletions server/events/vcs/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ type WorkflowRun struct {
RepositoryFileUrl githubv4.String
RepositoryName githubv4.String
}
RunNumber githubv4.Int
}

type CheckRun struct {
Expand Down Expand Up @@ -709,6 +710,24 @@ pagination:
return reviewDecision, requiredChecks, requiredWorkflows, checkRuns, statusContexts, nil
}

// GetLatestCheckRun returns the checkRun with the highest runNumber, i.e., the latest checkRun whose status we need to evaluate.
func GetLatestCheckRun(checkRuns []CheckRun) (CheckRun, error) {
X-Guardian marked this conversation as resolved.
Show resolved Hide resolved
latestCheckRunNumber := 0
for _, checkRun := range checkRuns {
if int(checkRun.CheckSuite.WorkflowRun.RunNumber) > latestCheckRunNumber {
X-Guardian marked this conversation as resolved.
Show resolved Hide resolved
latestCheckRunNumber = int(checkRun.CheckSuite.WorkflowRun.RunNumber)
}
}

for _, checkRun := range checkRuns {
X-Guardian marked this conversation as resolved.
Show resolved Hide resolved
if int(checkRun.CheckSuite.WorkflowRun.RunNumber) == latestCheckRunNumber {
X-Guardian marked this conversation as resolved.
Show resolved Hide resolved
return checkRun, nil
}
}

return CheckRun{}, errors.New("theres are not check runs passed")
}

func CheckRunPassed(checkRun CheckRun) bool {
return checkRun.Conclusion == "SUCCESS" || checkRun.Conclusion == "SKIPPED" || checkRun.Conclusion == "NEUTRAL"
}
Expand All @@ -718,11 +737,21 @@ func StatusContextPassed(statusContext StatusContext, vcsstatusname string) bool
}

func ExpectedCheckPassed(expectedContext githubv4.String, checkRuns []CheckRun, statusContexts []StatusContext, vcsstatusname string) bool {
// If there are no WorkflowRuns, we assume there's only one checkRun with the given name.
// If there are WorkflowRuns, we assume there can be multiple checkRuns with the given name,
// so we retrieve the latest checkRun.
matchedCheckRuns := make([]CheckRun, 0)
for _, checkRun := range checkRuns {
if checkRun.Name == expectedContext {
if checkRun.CheckSuite.WorkflowRun == nil {
return CheckRunPassed(checkRun)
} else if checkRun.Name == expectedContext {
matchedCheckRuns = append(matchedCheckRuns, checkRun)
}
}
X-Guardian marked this conversation as resolved.
Show resolved Hide resolved
latestCheckRun, err := GetLatestCheckRun(matchedCheckRuns)
if err == nil {
return CheckRunPassed(latestCheckRun)
}

for _, statusContext := range statusContexts {
if statusContext.Context == expectedContext {
Expand All @@ -734,17 +763,24 @@ func ExpectedCheckPassed(expectedContext githubv4.String, checkRuns []CheckRun,
}

func (g *GithubClient) ExpectedWorkflowPassed(expectedWorkflow WorkflowFileReference, checkRuns []CheckRun) (bool, error) {
matchedCheckRuns := make([]CheckRun, 0)
for _, checkRun := range checkRuns {
if checkRun.CheckSuite.WorkflowRun == nil {
continue
} else {
match, err := g.WorkflowRunMatchesWorkflowFileReference(*checkRun.CheckSuite.WorkflowRun, expectedWorkflow)
if err != nil {
return false, err
}
if match {
matchedCheckRuns = append(matchedCheckRuns, checkRun)
}
}
match, err := g.WorkflowRunMatchesWorkflowFileReference(*checkRun.CheckSuite.WorkflowRun, expectedWorkflow)
if err != nil {
return false, err
}
if match {
return CheckRunPassed(checkRun), nil
}
}

latestCheckRun, err := GetLatestCheckRun(matchedCheckRuns)
if err == nil {
return CheckRunPassed(latestCheckRun), nil
}

return false, nil
Expand Down
6 changes: 6 additions & 0 deletions server/events/vcs/github_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,12 @@ func TestGithubClient_PullIsMergeableWithAllowMergeableBypassApply(t *testing.T)
`"APPROVED"`,
true,
},
{
"blocked",
"ruleset-workflow-passed-multiple-runs.json",
`"APPROVED"`,
true,
},
{
"blocked",
"ruleset-workflow-passed-sha-match.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
}
},
"runNumber": 1
}
}
}
Expand All @@ -92,4 +93,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
}
},
"runNumber": 1
}
}
}
Expand All @@ -92,4 +93,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"data": {
"repository": {
"pullRequest": {
"reviewDecision": null,
"baseRef": {
"branchProtectionRule": {
"requiredStatusChecks": []
},
"rules": {
"pageInfo": {
"endCursor": "QWERTY",
"hasNextPage": false
},
"nodes": [
{
"type": "REQUIRED_STATUS_CHECKS",
"repositoryRuleset": {
"enforcement": "ACTIVE"
},
"parameters": {
"requiredStatusChecks": [
{
"context": "atlantis/apply"
}
]
}
},
{
"type": "WORKFLOWS",
"repositoryRuleset": {
"enforcement": "ACTIVE"
},
"parameters": {
"workflows": [
{
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryId": 120519269,
"sha": null
}
]
}
}
]
}
},
"commits": {
"nodes": [
{
"commit": {
"statusCheckRollup": {
"contexts": {
"pageInfo": {
"endCursor": "QWERTY",
"hasNextPage": false
},
"nodes": [
{
"__typename": "StatusContext",
"context": "atlantis/apply",
"state": "PENDING",
"isRequired": true
},
{
"__typename": "StatusContext",
"context": "atlantis/plan",
"state": "SUCCESS",
"isRequired": false
},
{
"__typename": "CheckRun",
"name": "my required check",
"conclusion": "FAILURE",
"isRequired": true,
"checkSuite": {
"workflowRun": {
"file": {
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
},
"runNumber": 1
}
}
},
{
"__typename": "CheckRun",
"name": "my required check",
"conclusion": "SUCCESS",
"isRequired": true,
"checkSuite": {
"workflowRun": {
"file": {
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
},
"runNumber": 2
}
}
}
]
}
}
}
}
]
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
}
},
"runNumber": 1
}
}
}
Expand All @@ -92,4 +93,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
}
},
"runNumber": 1
}
}
}
Expand All @@ -92,4 +93,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
"path": ".github/workflows/my-required-workflow.yaml",
"repositoryFileUrl": "https://github.com/runatlantis/atlantis/blob/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/.github/workflows/my-required-workflow.yaml",
"repositoryName": "runatlantis/atlantis"
}
},
"runNumber": 1
}
}
}
Expand All @@ -92,4 +93,4 @@
}
}
}
}
}