Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default_mainline input #372

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ Regex pattern to match github labels which will be copied from the original pull
Note that labels matching `label_pattern` are excluded.
By default, no labels are copied.

### `default_mainline`

Default: `''` (disabled)

The mainline (parent-number) to use when cherry-picking.
This allows the action to cherry-pick merge commits.
See https://git-scm.com/docs/git-cherry-pick for more information.
By default, the action fails when it encounters a merge commit.

### `github_token`

Default: `${{ github.token }}`
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ inputs:
Regex pattern to match github labels which will be copied from the original pull request to the backport pull request.
Note that labels matching `label_pattern` are excluded.
By default, no labels are copied.
default_mainline:
description: >
The mainline (parent-number) to use when cherry-picking.
This allows the action to cherry-pick merge commits.
See https://git-scm.com/docs/git-cherry-pick for more information.
By default, the action fails when it encounters a merge commit.
github_token:
description: >
Token to authenticate requests to GitHub.
Expand Down
27 changes: 20 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Backport {
continue;
}
try {
yield git.cherryPick(commitShas, this.config.pwd);
yield git.cherryPick(commitShas, this.config.pwd, this.config.default_mainline);
}
catch (error) {
const message = this.composeMessageForBackportScriptFailure(target, 4, baseref, headref, branchname);
Expand Down Expand Up @@ -419,12 +419,17 @@ function checkout(branch, start, pwd) {
});
}
exports.checkout = checkout;
function cherryPick(commitShas, pwd) {
function cherryPick(commitShas, pwd, default_mainline) {
return __awaiter(this, void 0, void 0, function* () {
const { exitCode } = yield git("cherry-pick", ["-x", ...commitShas], pwd);
const args = ["-x"];
if (default_mainline !== undefined) {
args.push(`--mainline=${default_mainline}`);
}
args.push(...commitShas);
const { exitCode } = yield git("cherry-pick", args, pwd);
if (exitCode !== 0) {
yield git("cherry-pick", ["--abort"], pwd);
throw new Error(`'git cherry-pick -x ${commitShas}' failed with exit code ${exitCode}`);
throw new Error(`'git cherry-pick ${args.join(" ")}' failed with exit code ${exitCode}`);
}
});
}
Expand Down Expand Up @@ -637,6 +642,7 @@ const github_1 = __nccwpck_require__(5928);
* Is separated from backport for testing purposes
*/
function run() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const token = core.getInput("github_token", { required: true });
const pwd = core.getInput("github_workspace", { required: true });
Expand All @@ -645,14 +651,21 @@ function run() {
const title = core.getInput("pull_title");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const github = new github_1.Github(token);
const backport = new backport_1.Backport(github, {
const default_mainline = core.getInput("default_mainline");
const config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
pull: { description, title },
copy_labels_pattern: copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
target_branches: target_branches === "" ? undefined : target_branches,
});
default_mainline: default_mainline === "" ? undefined : Number(default_mainline),
};
// todo: improve config validation
if (isNaN((_a = config.default_mainline) !== null && _a !== void 0 ? _a : -1)) {
throw new Error(`When defined, \`default_mainline\` must be a number but it was '${default_mainline}''`);
}
const github = new github_1.Github(token);
const backport = new backport_1.Backport(github, config);
return backport.run();
});
}
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/backport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type PRContent = {
body: string;
};

type Config = {
export type Config = {
pwd: string;
labels: {
pattern?: RegExp;
Expand All @@ -22,6 +22,7 @@ type Config = {
};
copy_labels_pattern?: RegExp;
target_branches?: string;
default_mainline?: number;
};

enum Output {
Expand Down Expand Up @@ -148,7 +149,11 @@ export class Backport {
}

try {
await git.cherryPick(commitShas, this.config.pwd);
await git.cherryPick(
commitShas,
this.config.pwd,
this.config.default_mainline,
);
} catch (error) {
const message = this.composeMessageForBackportScriptFailure(
target,
Expand Down
15 changes: 12 additions & 3 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ export async function checkout(branch: string, start: string, pwd: string) {
}
}

export async function cherryPick(commitShas: string[], pwd: string) {
const { exitCode } = await git("cherry-pick", ["-x", ...commitShas], pwd);
export async function cherryPick(
commitShas: string[],
pwd: string,
default_mainline?: number,
) {
const args = ["-x"];
if (default_mainline !== undefined) {
args.push(`--mainline=${default_mainline}`);
}
args.push(...commitShas);
const { exitCode } = await git("cherry-pick", args, pwd);
if (exitCode !== 0) {
await git("cherry-pick", ["--abort"], pwd);
throw new Error(
`'git cherry-pick -x ${commitShas}' failed with exit code ${exitCode}`,
`'git cherry-pick ${args.join(" ")}' failed with exit code ${exitCode}`,
);
}
}
20 changes: 16 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from "@actions/core";
import { Backport } from "./backport";
import { Backport, Config } from "./backport";
import { Github } from "./github";

/**
Expand All @@ -15,17 +15,29 @@ async function run(): Promise<void> {
const title = core.getInput("pull_title");
const copy_labels_pattern = core.getInput("copy_labels_pattern");
const target_branches = core.getInput("target_branches");
const default_mainline = core.getInput("default_mainline");

const github = new Github(token);
const backport = new Backport(github, {
const config: Config = {
pwd,
labels: { pattern: pattern === "" ? undefined : new RegExp(pattern) },
pull: { description, title },
copy_labels_pattern:
copy_labels_pattern === "" ? undefined : new RegExp(copy_labels_pattern),
target_branches:
target_branches === "" ? undefined : (target_branches as string),
});
default_mainline:
default_mainline === "" ? undefined : Number(default_mainline),
};

// todo: improve config validation
if (isNaN(config.default_mainline ?? -1)) {
throw new Error(
`When defined, \`default_mainline\` must be a number but it was '${default_mainline}''`,
);
}

const github = new Github(token);
const backport = new Backport(github, config);

return backport.run();
}
Expand Down