Skip to content

Commit

Permalink
EVEREST-107 add retry loop and refactor auto-update-base
Browse files Browse the repository at this point in the history
  • Loading branch information
recharte committed Jan 22, 2025
1 parent 9679682 commit 0a8580e
Showing 1 changed file with 74 additions and 36 deletions.
110 changes: 74 additions & 36 deletions .github/workflows/auto-update-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,88 @@ jobs:
with:
github-token: ${{ secrets.ROBOT_TOKEN }}
script: |
try {
const { data: pulls } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
});
for (const pull of pulls) {
const hasAutoUpdateLabel = pull.labels.some(label => label.name === "auto-update-base");
if (hasAutoUpdateLabel) {
try {
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull.number,
});
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Mergeable state: ${pr.data.mergeable_state}`);
if (pr.data.mergeable_state === 'dirty') {
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Skipping update due to merge conflict.`);
continue;
}
if (pr.data.mergeable_state !== 'behind') {
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Base branch is up-to-date.`);
continue;
}
let retriesNeeded = true;
let attemptCount = 0;
const maxAttempts = 10; // Set a limit for the maximum number of attempts
let errorEncountered = false; // Track if any errors occur
while (retriesNeeded && attemptCount < maxAttempts) {
retriesNeeded = false; // Assume no retries needed, unless 'unknown' state is found.
attemptCount += 1;
console.log(`[UPDATE] PR #${pull.number}: "${pull.title}" - Updating base branch...`);
try {
const { data: pulls } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
});
for (const pull of pulls) {
const hasAutoUpdateLabel = pull.labels.some(label => label.name === "auto-update-base");
if (hasAutoUpdateLabel) {
try {
await github.rest.pulls.updateBranch({
const pr = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull.number,
});
console.log(`[SUCCESS] PR #${pull.number}: "${pull.title}" - Base branch updated.`);
} catch (updateError) {
console.error(`[ERROR] PR #${pull.number}: Failed to update base branch - ${updateError.message}`);
switch (pr.data.mergeable_state) {
case 'unknown':
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - GitHub is still computing the mergeability. Will retry later.`);
retriesNeeded = true;
break;
case 'clean':
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Base branch is up-to-date.`);
break;
case 'dirty':
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Skipping update due to merge conflict.`);
break;
case 'blocked':
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Skipping update due to blocked status.`);
break;
case 'behind':
console.log(`[UPDATE] PR #${pull.number}: "${pull.title}" - Updating base branch...`);
try {
await github.rest.pulls.updateBranch({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pull.number,
});
console.log(`[SUCCESS] PR #${pull.number}: "${pull.title}" - Base branch updated.`);
} catch (updateError) {
console.error(`[ERROR] PR #${pull.number}: Failed to update base branch - ${updateError.message}`);
errorEncountered = true;
}
break;
default:
console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Mergeable state: ${pr.data.mergeable_state}. Cannot update base branch.`);
}
} catch (diffError) {
console.error(`[ERROR] PR #${pull.number}: Failed to compare commits - ${diffError.message}`);
errorEncountered = true;
}
} catch (diffError) {
console.error(`[ERROR] PR #${pull.number}: Failed to compare commits - ${diffError.message}`);
}
}
} catch (error) {
console.error(`[ERROR] Failed to list pull requests - ${error.message}`);
errorEncountered = true;
}
} catch (error) {
console.error(`[ERROR] Failed to list pull requests - ${error.message}`);
if (retriesNeeded && attemptCount < maxAttempts) {
console.log(`[INFO] Retrying for PRs with 'unknown' mergeable state (Attempt ${attemptCount}/${maxAttempts})...`);
await new Promise(resolve => setTimeout(resolve, 5000));
}
}
if (attemptCount >= maxAttempts) {
console.error(`[ERROR] Maximum attempts (${maxAttempts}) reached. Some PRs may still have 'unknown' mergeable state.`);
errorEncountered = true;
}
if (errorEncountered) {
core.setFailed(`[ERROR] Some PRs failed to update base branch.`);
} else {
console.log(`[INFO] All PRs with 'auto-update-base' label have been processed.`);
}

0 comments on commit 0a8580e

Please sign in to comment.