Skip to content

Commit 09d3c41

Browse files
authored
Merge pull request #705 from lexfrei/fix/empty-cherry-pick
Skip commits the target branch already contains
2 parents fee4df7 + 15cff37 commit 09d3c41

13 files changed

Lines changed: 668 additions & 96 deletions

‎CONTEXT.md‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ The set of commits selected from the original pull request's history and passed
2727
**Cherry-picked commits**:
2828
The new commits created on the target branch by `git cherry-pick`. The commit author is preserved from the input commits; the commit committer is overwritten by `git_committer_name` / `git_committer_email` (defaulting to `github-actions[bot]`). Each carries an `-x` trailer referencing the input commit as audit trail.
2929

30+
**Skipped commit**:
31+
A commit to backport whose changes the target branch already contains, so no cherry-picked commit is created for it.
32+
_Avoid_: empty commit, empty cherry-pick (git mechanics), already-backported commit (the changes may have reached the target another way)
33+
3034
**Merge method**:
3135
How a pull request is merged on GitHub. One of: squash-and-merge, rebase-and-merge, or merge commit. The action *observes* the original pull request's merge method (consumed by `cherry_picking: auto`) and *configures* the backport pull request's merge method (via `auto_merge_method`). Always disambiguate which pull request's merge method is meant.
3236

@@ -46,7 +50,8 @@ Git commit metadata. Cherry-picking preserves the commit author and overwrites t
4650
- An **original pull request** produces zero or more **backport pull requests**, one per attempted **target branch**.
4751
- A **backport pull request** lives on a **target branch** inside the **target repository**.
4852
- A **backport label** on the **original pull request** selects one **target branch**.
49-
- The **commits to backport** (input to cherry-pick) become **cherry-picked commits** (output, on the target branch) after `git cherry-pick`.
53+
- The **commits to backport** (input to cherry-pick) become **cherry-picked commits** (output, on the target branch) after `git cherry-pick`, except for **skipped commits**.
54+
- When every commit to backport is a **skipped commit**, no **backport pull request** is created for that **target branch**.
5055
- The **original pull request's** **merge method** determines which **commits to backport** are selected when `cherry_picking: auto`.
5156

5257
## Example dialogue
@@ -62,4 +67,5 @@ Git commit metadata. Cherry-picking preserves the commit author and overwrites t
6267
- "Downstream repo" — resolved to **target repository** in prose. Input names `downstream_repo`/`downstream_owner` are experimental and likely renamed on promotion.
6368
- "Merge commit" was overloaded across: a merge method, an intermediate commit in PR history (`merge_commits` input), and the result of auto-merge. Resolved by always pairing with context; no coined term for the intermediate case.
6469
- "Author" was used for three concepts: PR author, commit author, commit committer. Resolved by always qualifying ("PR author" / "commit author" / "commit committer").
65-
- "Successful" / "failed" / "skipped" / "attempted" — intentionally *not* glossarised. The output `was_successful` is documented precisely in `README.md`; surrounding adjectives are plain English.
70+
- "Successful" / "failed" / "skipped" / "attempted" — intentionally *not* glossarised. The output `was_successful` is documented precisely in `README.md`; surrounding adjectives are plain English. The one exception is **skipped commit**, a named category of commit.
71+
- "Skipped" merge commits: `merge_commits: skip` leaves merge commits out of the **commits to backport**. They are *excluded*, not **skipped commits**. The input value `skip` is frozen API; in prose, say "excluded" or "ignored".

‎src/backport.ts‎

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
RequestError,
88
} from "./github.js";
99
import { GithubApi } from "./github.js";
10-
import { GitApi, GitRefNotFoundError } from "./git.js";
10+
import { CherryPickResult, GitApi, GitRefNotFoundError } from "./git.js";
1111
import {
1212
CommentContext,
1313
formatInitialComment,
@@ -23,6 +23,7 @@ import {
2323
} from "./errors.js";
2424
import {
2525
composeFailureMessage,
26+
composeMessageForSkippedCommits,
2627
composeMessageForSuccess,
2728
composeMessageForSuccessWithConflicts,
2829
composeMessageToResolveCommittedConflicts,
@@ -318,18 +319,24 @@ export class Backport {
318319
await updateSummary(
319320
formatRunComment(results, remainingTargets, commentCtx),
320321
);
321-
if (result.status === "success_with_conflicts") {
322-
// Best-effort: a failure to post the resolve-conflicts comment
323-
// shouldn't flip a success_with_conflicts target into a hard run
324-
// failure or block subsequent targets.
325-
try {
322+
// Best-effort: a failure to comment on the backport pull request
323+
// shouldn't flip the target into a hard run failure or block
324+
// subsequent targets.
325+
try {
326+
if (result.status === "success_with_conflicts") {
326327
await this.commentResolveConflictsOnDraftPr(result, context);
327-
} catch (error) {
328-
console.error(
329-
"Failed to post resolve-conflicts comment on draft PR:",
330-
error,
331-
);
332328
}
329+
if (
330+
result.status === "success" ||
331+
result.status === "success_with_conflicts"
332+
) {
333+
await this.commentSkippedCommitsOnBackportPr(result, context);
334+
}
335+
} catch (error) {
336+
console.error(
337+
"Failed to post comment on the backport pull request:",
338+
error,
339+
);
333340
}
334341
} else {
335342
await this.handleTargetResultLegacy(result, context);
@@ -411,7 +418,7 @@ export class Backport {
411418
};
412419
}
413420

414-
let uncommittedShas: string[] | null;
421+
let cherryPickResult: CherryPickResult;
415422

416423
if (
417424
this.config.commits.cherry_picking_merge_mode === "whitespace_tolerant"
@@ -420,7 +427,7 @@ export class Backport {
420427
}
421428

422429
try {
423-
uncommittedShas = await this.git.cherryPick(
430+
cherryPickResult = await this.git.cherryPick(
424431
commitShasToCherryPick,
425432
this.config.experimental.conflict_resolution,
426433
this.config.pwd,
@@ -443,6 +450,17 @@ export class Backport {
443450
};
444451
}
445452

453+
if (cherryPickResult.status === "empty") {
454+
console.log(
455+
`Nothing to backport to ${targetBranch}, it already contains these changes`,
456+
);
457+
return {
458+
status: "skipped",
459+
targetBranch,
460+
reason: "target already contains changes",
461+
};
462+
}
463+
446464
console.info(`Push branch to ${remote}`);
447465
try {
448466
await this.git.push(branchname, remote, this.config.pwd);
@@ -481,7 +499,7 @@ export class Backport {
481499
head: branchname,
482500
base: targetBranch,
483501
maintainer_can_modify: true,
484-
draft: uncommittedShas !== null,
502+
draft: cherryPickResult.status === "conflicts",
485503
});
486504
} catch (error) {
487505
if (!(error instanceof RequestError)) throw error;
@@ -524,20 +542,22 @@ export class Backport {
524542
{ owner: context.workflowOwner, repo: context.workflowRepo },
525543
);
526544

527-
if (uncommittedShas !== null) {
545+
if (cherryPickResult.status === "conflicts") {
528546
return {
529547
status: "success_with_conflicts",
530548
targetBranch,
531549
newPrNumber: new_pr.number,
532550
branchname,
533-
uncommittedShas,
551+
uncommittedShas: cherryPickResult.uncommittedShas,
552+
skippedShas: cherryPickResult.skippedShas,
534553
};
535554
}
536555
return {
537556
status: "success",
538557
targetBranch,
539558
newPrNumber: new_pr.number,
540559
branchname,
560+
skippedShas: cherryPickResult.skippedShas,
541561
};
542562
} catch (error) {
543563
if (error instanceof Error) {
@@ -595,8 +615,14 @@ export class Backport {
595615
branchname,
596616
result.uncommittedShas,
597617
this.config.experimental.conflict_resolution,
618+
result.skippedShas,
598619
)
599-
: composeMessageForSuccess(newPrNumber, targetBranch, downstream);
620+
: composeMessageForSuccess(
621+
newPrNumber,
622+
targetBranch,
623+
downstream,
624+
result.skippedShas,
625+
);
600626

601627
await this.github.createComment({
602628
owner: workflowOwner,
@@ -608,6 +634,33 @@ export class Backport {
608634
if (result.status === "success_with_conflicts") {
609635
await this.commentResolveConflictsOnDraftPr(result, context);
610636
}
637+
638+
await this.commentSkippedCommitsOnBackportPr(result, context);
639+
}
640+
641+
private async commentSkippedCommitsOnBackportPr(
642+
result: Extract<
643+
TargetResult,
644+
{ status: "success" | "success_with_conflicts" }
645+
>,
646+
context: BackportContext,
647+
): Promise<void> {
648+
if (result.skippedShas.length === 0) return;
649+
650+
// The backport PR may live in another target repository, where a bare #N points elsewhere.
651+
const originalRepo = this.shouldUseDownstreamRepo()
652+
? `${context.workflowOwner}/${context.workflowRepo}`
653+
: "";
654+
await this.github.createComment({
655+
owner: context.targetOwner,
656+
repo: context.targetRepo,
657+
issue_number: result.newPrNumber,
658+
body: composeMessageForSkippedCommits(
659+
result.targetBranch,
660+
`${originalRepo}#${context.pullNumber}`,
661+
result.skippedShas,
662+
),
663+
});
611664
}
612665

613666
private async commentResolveConflictsOnDraftPr(

‎src/comments.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TargetResult,
99
} from "./errors.js";
1010
import { GitRefNotFoundError } from "./git.js";
11+
import { describeSkippedCommits } from "./utils.js";
1112

1213
/**
1314
* Per-run context required to render the summary comment.
@@ -124,16 +125,21 @@ function formatTable(
124125
function formatStatusCell(result: TargetResult): string {
125126
switch (result.status) {
126127
case "success":
127-
return `:white_check_mark: Created #${result.newPrNumber}`;
128+
return `:white_check_mark: Created #${result.newPrNumber}${formatSkippedSuffix(result.skippedShas)}`;
128129
case "success_with_conflicts":
129-
return `:warning: Drafted with conflicts #${result.newPrNumber}`;
130+
return `:warning: Drafted with conflicts #${result.newPrNumber}${formatSkippedSuffix(result.skippedShas)}`;
130131
case "skipped":
131132
return `:heavy_minus_sign: Skipped (${result.reason})`;
132133
case "failed":
133134
return ":x: Failed";
134135
}
135136
}
136137

138+
function formatSkippedSuffix(skippedShas: string[]): string {
139+
if (skippedShas.length === 0) return "";
140+
return ` — ${describeSkippedCommits(skippedShas)}`;
141+
}
142+
137143
/**
138144
* Renders the collapsible `<details>` block for a single failed target.
139145
*

‎src/errors.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ export type TargetResult =
8080
targetBranch: string;
8181
newPrNumber: number;
8282
branchname: string;
83+
skippedShas: string[];
8384
}
8485
| {
8586
status: "success_with_conflicts";
8687
targetBranch: string;
8788
newPrNumber: number;
8889
branchname: string;
8990
uncommittedShas: string[];
91+
skippedShas: string[];
9092
}
9193
| { status: "skipped"; targetBranch: string; reason: string }
9294
| {

0 commit comments

Comments
 (0)