Skip to content

Commit c156b63

Browse files
fix: include rebase/abort errors in PR comment instead of swallowing
Co-Authored-By: Niels Swimberghe <3382717+Swimburger@users.noreply.github.com>
1 parent b1e6c01 commit c156b63

2 files changed

Lines changed: 56 additions & 17 deletions

File tree

dist/index.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39647,6 +39647,8 @@ async function pushWithFallback(branchName, owner, repo, octokit) {
3964739647
core.info(`Regular push to '${branchName}' failed. Attempting to rebase on remote branch.`);
3964839648
}
3964939649
// Try pull --rebase then push
39650+
let rebaseErrorMsg = null;
39651+
let abortErrorMsg = null;
3965039652
try {
3965139653
await exec.exec("git", ["pull", "--rebase", "origin", branchName], {
3965239654
silent: false,
@@ -39655,28 +39657,43 @@ async function pushWithFallback(branchName, owner, repo, octokit) {
3965539657
core.info(`Successfully pushed to '${branchName}' after rebasing on remote changes.`);
3965639658
return true;
3965739659
}
39658-
catch {
39659-
core.info(`Rebase failed (likely due to merge conflicts). Aborting rebase.`);
39660+
catch (rebaseError) {
39661+
rebaseErrorMsg =
39662+
rebaseError instanceof Error
39663+
? rebaseError.message
39664+
: "Unknown error";
39665+
core.info(`Rebase failed (likely due to merge conflicts): ${rebaseErrorMsg}. Aborting rebase.`);
3966039666
// Abort the rebase so the working tree is clean
3966139667
try {
3966239668
await exec.exec("git", ["rebase", "--abort"], { silent: true });
3966339669
}
39664-
catch {
39665-
// rebase --abort can fail if there's no rebase in progress, ignore
39670+
catch (abortError) {
39671+
abortErrorMsg =
39672+
abortError instanceof Error
39673+
? abortError.message
39674+
: "Unknown error";
39675+
core.info(`rebase --abort failed: ${abortErrorMsg}. The working tree may be in an unexpected state.`);
3966639676
}
3966739677
}
3966839678
// Last resort: leave a comment on the existing PR
3966939679
const existingPRNumber = await prExists(owner, repo, branchName, octokit);
3967039680
if (existingPRNumber) {
3967139681
core.info(`Could not push to '${branchName}' due to conflicts. Leaving a comment on PR #${existingPRNumber}.`);
39682+
let commentBody = `⚠️ **Sync failed**: The latest \`fern api update\` detected changes, but they could not be pushed to this branch due to merge conflicts.\n\n` +
39683+
`**To resolve**, either:\n` +
39684+
`- Merge or close this PR so the next run creates a fresh one, or\n` +
39685+
`- Manually rebase this branch on \`${github.context.ref.replace("refs/heads/", "")}\` and re-run the workflow.`;
39686+
if (rebaseErrorMsg) {
39687+
commentBody += `\n\n**Rebase error:** \`${rebaseErrorMsg}\``;
39688+
}
39689+
if (abortErrorMsg) {
39690+
commentBody += `\n**Rebase abort error:** \`${abortErrorMsg}\` — the working tree may be in an unexpected state.`;
39691+
}
3967239692
await octokit.rest.issues.createComment({
3967339693
owner,
3967439694
repo,
3967539695
issue_number: existingPRNumber,
39676-
body: `⚠️ **Sync failed**: The latest \`fern api update\` detected changes, but they could not be pushed to this branch due to merge conflicts.\n\n` +
39677-
`**To resolve**, either:\n` +
39678-
`- Merge or close this PR so the next run creates a fresh one, or\n` +
39679-
`- Manually rebase this branch on \`${github.context.ref.replace("refs/heads/", "")}\` and re-run the workflow.`,
39696+
body: commentBody,
3968039697
});
3968139698
core.setFailed(`Failed to push changes to '${branchName}' due to conflicts. A comment has been left on PR #${existingPRNumber}.`);
3968239699
}

src/sync.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ async function pushWithFallback(
523523
}
524524

525525
// Try pull --rebase then push
526+
let rebaseErrorMsg: string | null = null;
527+
let abortErrorMsg: string | null = null;
526528
try {
527529
await exec.exec("git", ["pull", "--rebase", "origin", branchName], {
528530
silent: false,
@@ -536,15 +538,25 @@ async function pushWithFallback(
536538
`Successfully pushed to '${branchName}' after rebasing on remote changes.`,
537539
);
538540
return true;
539-
} catch {
541+
} catch (rebaseError) {
542+
rebaseErrorMsg =
543+
rebaseError instanceof Error
544+
? rebaseError.message
545+
: "Unknown error";
540546
core.info(
541-
`Rebase failed (likely due to merge conflicts). Aborting rebase.`,
547+
`Rebase failed (likely due to merge conflicts): ${rebaseErrorMsg}. Aborting rebase.`,
542548
);
543549
// Abort the rebase so the working tree is clean
544550
try {
545551
await exec.exec("git", ["rebase", "--abort"], { silent: true });
546-
} catch {
547-
// rebase --abort can fail if there's no rebase in progress, ignore
552+
} catch (abortError) {
553+
abortErrorMsg =
554+
abortError instanceof Error
555+
? abortError.message
556+
: "Unknown error";
557+
core.info(
558+
`rebase --abort failed: ${abortErrorMsg}. The working tree may be in an unexpected state.`,
559+
);
548560
}
549561
}
550562

@@ -554,15 +566,25 @@ async function pushWithFallback(
554566
core.info(
555567
`Could not push to '${branchName}' due to conflicts. Leaving a comment on PR #${existingPRNumber}.`,
556568
);
569+
570+
let commentBody =
571+
`⚠️ **Sync failed**: The latest \`fern api update\` detected changes, but they could not be pushed to this branch due to merge conflicts.\n\n` +
572+
`**To resolve**, either:\n` +
573+
`- Merge or close this PR so the next run creates a fresh one, or\n` +
574+
`- Manually rebase this branch on \`${github.context.ref.replace("refs/heads/", "")}\` and re-run the workflow.`;
575+
576+
if (rebaseErrorMsg) {
577+
commentBody += `\n\n**Rebase error:** \`${rebaseErrorMsg}\``;
578+
}
579+
if (abortErrorMsg) {
580+
commentBody += `\n**Rebase abort error:** \`${abortErrorMsg}\` — the working tree may be in an unexpected state.`;
581+
}
582+
557583
await octokit.rest.issues.createComment({
558584
owner,
559585
repo,
560586
issue_number: existingPRNumber,
561-
body:
562-
`⚠️ **Sync failed**: The latest \`fern api update\` detected changes, but they could not be pushed to this branch due to merge conflicts.\n\n` +
563-
`**To resolve**, either:\n` +
564-
`- Merge or close this PR so the next run creates a fresh one, or\n` +
565-
`- Manually rebase this branch on \`${github.context.ref.replace("refs/heads/", "")}\` and re-run the workflow.`,
587+
body: commentBody,
566588
});
567589
core.setFailed(
568590
`Failed to push changes to '${branchName}' due to conflicts. A comment has been left on PR #${existingPRNumber}.`,

0 commit comments

Comments
 (0)