-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ready_to_merge.yml workflow
- Loading branch information
1 parent
8278e67
commit c03e73f
Showing
5 changed files
with
10,463 additions
and
10,303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
name: Ready to Merge | ||
on: | ||
pull_request: | ||
types: [opened, synchronize, reopened] | ||
# branches: | ||
# - main | ||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: ${{ github.ref }}-ready-to-merge | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
check_commit_status: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check if PR is mergeable | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
// Define a list of workflows that need to finish successfully if run before merging | ||
workflows_to_check = [ | ||
"all_packages.yml", | ||
"check-uncommitted-changes.yml", | ||
"mega-linter.yml", | ||
"e2e_tests.yml", | ||
"supabase-test.yml", | ||
]; | ||
// todo increase | ||
console.log("Sleeping 10s to give other runs a chance to start"); | ||
await sleep(10000); | ||
const { owner, repo } = context.repo; | ||
const commit_sha = context.payload.pull_request.head.sha; // Get the SHA of the head commit of the PR | ||
const branch_name = context.payload.pull_request.head.ref; | ||
const excludedCheckRunName = 'check_commit_status' // important you don't define name key for the job | ||
const maxRetries = 15; // Maximum number of retries | ||
async function fetchLatestWorkflowRun(workflowId) { | ||
console.log("Fetching workflow runs for: " + workflowId); | ||
try { | ||
const response = await github.rest.actions.listWorkflowRuns({ | ||
owner, | ||
repo, | ||
workflow_id: workflowId, | ||
branch: branch_name, | ||
per_page: 1, | ||
}); | ||
return response.data.workflow_runs[0]; | ||
} catch (error) { | ||
if (error.status === 404) { | ||
console.warn("Workflow not found: " + workflowId + ". It will be ignored."); | ||
return; | ||
} | ||
core.setFailed("Error fetching workflow runs for: " + workflowId + ". Error: " + error.message); | ||
process.exit(); | ||
} | ||
} | ||
async function fetchCheckRuns() { | ||
try { | ||
const response = await github.rest.checks.listForRef({ | ||
owner, | ||
repo, | ||
ref: commit_sha, | ||
per_page: 100, | ||
}); | ||
return response.data.check_runs; | ||
} catch (error) { | ||
core.setFailed("Error fetching check runs for: " + commit_sha + ". Error: " + error.message); | ||
process.exit(); | ||
} | ||
} | ||
async function sleep(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
for (const workflowId of workflows_to_check) { | ||
let retryCount = 0; | ||
let workflowRun = await fetchLatestWorkflowRun(workflowId); | ||
if (!workflowRun) { | ||
console.log("No workflow runs found for: " + workflowId); | ||
continue; | ||
} | ||
do { | ||
if (workflowRun.conclusion === "success") { | ||
console.log("Workflow run completed: " + workflowRun.name); | ||
break; | ||
} | ||
if (workflowRun.status === "in_progress") { | ||
console.log("Workflow run in progress: " + workflowRun.name); | ||
if (retryCount >= maxRetries) { | ||
core.setFailed( | ||
"Maximum retries reached with check runs still in progress." | ||
); | ||
process.exit(); | ||
} | ||
console.log("Waiting for 1 minute before rechecking..."); | ||
await sleep(60000); | ||
workflowRun = await fetchLatestWorkflowRun(workflowId); | ||
retryCount++; | ||
} | ||
if (workflowRun.conclusion === "failure") { | ||
console.log("Workflow run failed: " + workflowRun.name); | ||
const checkSuiteId = workflowRun.check_suite_id; | ||
const urlOfFailedCheckSuite = workflowRun.html_url; | ||
const checkRuns = await github.rest.checks.listForSuite({ | ||
owner, | ||
repo, | ||
check_suite_id: checkSuiteId, | ||
}); | ||
const failedCheckNames = checkRuns.data.check_runs | ||
.filter((checkRun) => checkRun.conclusion === "failure") | ||
.map((checkRun) => checkRun.name); | ||
core.setFailed("The workflow run '" + | ||
workflowRun.name + "' has failed. Please fix the failed checks: '" + | ||
failedCheckNames.join(", ") + "'. Check suite URL: " + | ||
urlOfFailedCheckSuite | ||
); | ||
process.exit(); | ||
} | ||
} while (true); | ||
} | ||
console.log("All workflow runs completed successfully."); | ||
console.log("Checking check runs..."); | ||
let checkRuns = await fetchCheckRuns(); | ||
for (const checkRun of checkRuns) { | ||
let retryCount = 0; | ||
do { | ||
if (checkRun.name === excludedCheckRunName) { | ||
console.log(`Skipping excluded check run: ${checkRun.name}`); | ||
continue; | ||
} | ||
if (checkRun.conclusion === "success") { | ||
console.log("Check run completed: " + checkRun.name); | ||
break; | ||
} | ||
if (checkRun.status === "in_progress") { | ||
console.log("Check run in progress: " + checkRun.name); | ||
if (retryCount >= maxRetries) { | ||
core.setFailed( | ||
"Maximum retries reached with check runs still in progress." | ||
); | ||
process.exit(); | ||
} | ||
console.log("Waiting for 1 minute before rechecking..."); | ||
await sleep(60000); | ||
checkRuns = await fetchCheckRuns(); | ||
retryCount++; | ||
} | ||
if (checkRun.conclusion === "failure") { | ||
console.log("Check run failed: " + checkRun.name); | ||
core.setFailed("The check run '" + | ||
checkRun.name + "' has failed. Please fix the failed checks. Check run URL: " + | ||
checkRun.html_url | ||
); | ||
process.exit(); | ||
} | ||
} while (true); | ||
} | ||
console.log("All check runs completed successfully."); |
Oops, something went wrong.