bcr_reviewer: wait until all runs of the dismiss_approvals workflow are completed before reviewing PRs (#1917)
Addressing
https://github.com/bazelbuild/bazel-central-registry/pull/1621#issuecomment-1994504063
diff --git a/actions/bcr-pr-reviewer/index.js b/actions/bcr-pr-reviewer/index.js
index 278a7c7..d35a5b5 100644
--- a/actions/bcr-pr-reviewer/index.js
+++ b/actions/bcr-pr-reviewer/index.js
@@ -323,9 +323,61 @@
}
}
+async function waitForDismissApprovalsWorkflow(octokit, owner, repo) {
+ // List all workflow of this repository
+ const workflows = await octokit.rest.actions.listRepoWorkflows({
+ owner,
+ repo,
+ });
+
+ // Find the workflow file for .github/workflows/dismiss_approvals.yml
+ const dismissApprovalsWorkflow = workflows.data.workflows.find(workflow => workflow.path === '.github/workflows/dismiss_approvals.yml');
+ if (!dismissApprovalsWorkflow) {
+ setFailed('The dismiss_approvals workflow is not found');
+ return false;
+ }
+ console.log(`Found dismiss_approvals workflow: ${dismissApprovalsWorkflow.id}`);
+
+ // Wait until all runs of the dismiss_approvals workflow are completed
+ // https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository
+ shouldWaitStatues = ['queued', 'in_progress', 'requested', 'pending', 'waiting'];
+ let response;
+ while (true) {
+ total_count = 0;
+ for (const status of shouldWaitStatues) {
+ response = await octokit.rest.actions.listWorkflowRuns({
+ owner,
+ repo,
+ workflow_id: dismissApprovalsWorkflow.id,
+ status: status,
+ });
+
+ for (const run of response.data.workflow_runs) {
+ console.log(`Dismiss approvals workflow run #${run.run_number} is ${run.status}`);
+ }
+
+ total_count += response.data.total_count;
+ }
+
+ if (total_count > 0) {
+ console.log('Waiting 5s for dismiss approvals workflow runs to complete');
+ await new Promise(resolve => setTimeout(resolve, 5000));
+ } else {
+ console.log('All dismiss approvals workflow runs are completed');
+ break;
+ }
+ }
+ return true;
+}
+
async function runPrReviewer(octokit) {
const { owner, repo } = context.repo;
+ // Wait until all runs of the dismiss_approvals workflow are completed
+ if (!await waitForDismissApprovalsWorkflow(octokit, owner, repo)) {
+ return;
+ }
+
// Get all open PRs from the repo
const prs = await octokit.rest.pulls.list({
owner,