Skip to content

Commit

Permalink
feat: add --cherry-pick-options to add to all cherry-pick run (#116)
Browse files Browse the repository at this point in the history
Fixes: #111
  • Loading branch information
earl-warren committed Apr 2, 2024
1 parent 53cc505 commit fe6be83
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 81 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ This tool comes with some inputs that allow users to override the default behavi
| No squash | --no-squash | N | If provided the backporting will try to backport all pull request commits without squashing | false |
| Strategy | --strategy | N | Cherry pick merging strategy, see [git-merge](https://git-scm.com/docs/git-merge#_merge_strategies) doc for all possible values | "recursive" |
| Strategy Option | --strategy-option | N | Cherry pick merging strategy option, see [git-merge](https://git-scm.com/docs/git-merge#_merge_strategies) doc for all possible values | "theirs" |
| Cherry-pick Options | --cherry-pick-options | N | Additional cherry-pick options, see [git-cherry-pick](https://git-scm.com/docs/git-cherry-pick) doc for all possible values | "theirs" |
| Additional comments | --comments | N | Semicolon separated list of additional comments to be posted to the backported pull request | [] |
| Dry Run | -d, --dry-run | N | If enabled the tool does not push nor create anything remotely, use this to skip PR creation | false |

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ inputs:
description: Cherry-pick merge strategy option
required: false
default: "theirs"
cherry-pick-options:
description: >
Additional cherry-pick options
required: false
comments:
description: >
Semicolon separated list of additional comments to be posted to the backported pull request
Expand Down
17 changes: 13 additions & 4 deletions dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ArgsParser {
squash: this.getOrDefault(args.squash, true),
strategy: this.getOrDefault(args.strategy),
strategyOption: this.getOrDefault(args.strategyOption),
cherryPickOptions: this.getOrDefault(args.cherryPickOptions),
comments: this.getOrDefault(args.comments)
};
}
Expand Down Expand Up @@ -205,6 +206,7 @@ class CLIArgsParser extends args_parser_1.default {
.option("--no-squash", "if provided the tool will backport all commits as part of the pull request")
.option("--strategy <strategy>", "cherry-pick merge strategy, default to 'recursive'", undefined)
.option("--strategy-option <strategy-option>", "cherry-pick merge strategy option, default to 'theirs'")
.option("--cherry-pick-options <options>", "additional cherry-pick options")
.option("--comments <comments>", "semicolon separated list of additional comments to be posted to the backported pull request", args_utils_1.getAsSemicolonSeparatedList)
.option("-cf, --config-file <config-file>", "configuration file containing all valid options, the json must match Args interface");
}
Expand Down Expand Up @@ -240,6 +242,7 @@ class CLIArgsParser extends args_parser_1.default {
squash: opts.squash,
strategy: opts.strategy,
strategyOption: opts.strategyOption,
cherryPickOptions: opts.cherryPickOptions,
comments: opts.comments,
};
}
Expand Down Expand Up @@ -359,6 +362,7 @@ class PullRequestConfigsParser extends configs_parser_1.default {
folder: `${folder.startsWith("/") ? "" : process.cwd() + "/"}${args.folder ?? this.getDefaultFolder()}`,
mergeStrategy: args.strategy,
mergeStrategyOption: args.strategyOption,
cherryPickOptions: args.cherryPickOptions,
originalPullRequest: pr,
backportPullRequests: this.generateBackportPullRequestsData(pr, args, targetBranches, bpBranchNames),
git: {
Expand Down Expand Up @@ -555,9 +559,14 @@ class GitCLIService {
* @param cwd repository in which the sha should be cherry picked to
* @param sha commit sha
*/
async cherryPick(cwd, sha, strategy = "recursive", strategyOption = "theirs") {
async cherryPick(cwd, sha, strategy = "recursive", strategyOption = "theirs", cherryPickOptions) {
this.logger.info(`Cherry picking ${sha}`);
const options = ["cherry-pick", "-m", "1", `--strategy=${strategy}`, `--strategy-option=${strategyOption}`, sha];
let options = ["cherry-pick", "-m", "1", `--strategy=${strategy}`, `--strategy-option=${strategyOption}`];
if (cherryPickOptions !== undefined) {
options = options.concat(cherryPickOptions.split(" "));
}
options.push(sha);
this.logger.debug(`Cherry picking command git ${options}`);
try {
await this.git(cwd).raw(options);
}
Expand Down Expand Up @@ -1461,7 +1470,7 @@ class Runner {
// 7. apply all changes to the new branch
this.logger.debug("Cherry picking commits..");
for (const sha of originalPR.commits) {
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption);
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions);
}
if (!configs.dryRun) {
// 8. push the new branch to origin
Expand Down Expand Up @@ -23801,4 +23810,4 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ module.exports = __webpack_exports__;
/******/
/******/ })()
;
;
14 changes: 11 additions & 3 deletions dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ArgsParser {
squash: this.getOrDefault(args.squash, true),
strategy: this.getOrDefault(args.strategy),
strategyOption: this.getOrDefault(args.strategyOption),
cherryPickOptions: this.getOrDefault(args.cherryPickOptions),
comments: this.getOrDefault(args.comments)
};
}
Expand Down Expand Up @@ -208,6 +209,7 @@ class GHAArgsParser extends args_parser_1.default {
squash: !(0, args_utils_1.getAsBooleanOrDefault)((0, core_1.getInput)("no-squash")),
strategy: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("strategy")),
strategyOption: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("strategy-option")),
cherryPickOptions: (0, args_utils_1.getOrUndefined)((0, core_1.getInput)("cherry-pick-options")),
comments: (0, args_utils_1.getAsSemicolonSeparatedList)((0, core_1.getInput)("comments")),
};
}
Expand Down Expand Up @@ -327,6 +329,7 @@ class PullRequestConfigsParser extends configs_parser_1.default {
folder: `${folder.startsWith("/") ? "" : process.cwd() + "/"}${args.folder ?? this.getDefaultFolder()}`,
mergeStrategy: args.strategy,
mergeStrategyOption: args.strategyOption,
cherryPickOptions: args.cherryPickOptions,
originalPullRequest: pr,
backportPullRequests: this.generateBackportPullRequestsData(pr, args, targetBranches, bpBranchNames),
git: {
Expand Down Expand Up @@ -523,9 +526,14 @@ class GitCLIService {
* @param cwd repository in which the sha should be cherry picked to
* @param sha commit sha
*/
async cherryPick(cwd, sha, strategy = "recursive", strategyOption = "theirs") {
async cherryPick(cwd, sha, strategy = "recursive", strategyOption = "theirs", cherryPickOptions) {
this.logger.info(`Cherry picking ${sha}`);
const options = ["cherry-pick", "-m", "1", `--strategy=${strategy}`, `--strategy-option=${strategyOption}`, sha];
let options = ["cherry-pick", "-m", "1", `--strategy=${strategy}`, `--strategy-option=${strategyOption}`];
if (cherryPickOptions !== undefined) {
options = options.concat(cherryPickOptions.split(" "));
}
options.push(sha);
this.logger.debug(`Cherry picking command git ${options}`);
try {
await this.git(cwd).raw(options);
}
Expand Down Expand Up @@ -1429,7 +1437,7 @@ class Runner {
// 7. apply all changes to the new branch
this.logger.debug("Cherry picking commits..");
for (const sha of originalPR.commits) {
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption);
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions);
}
if (!configs.dryRun) {
// 8. push the new branch to origin
Expand Down
1 change: 1 addition & 0 deletions src/service/args/args-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default abstract class ArgsParser {
squash: this.getOrDefault(args.squash, true),
strategy: this.getOrDefault(args.strategy),
strategyOption: this.getOrDefault(args.strategyOption),
cherryPickOptions: this.getOrDefault(args.cherryPickOptions),
comments: this.getOrDefault(args.comments)
};
}
Expand Down
1 change: 1 addition & 0 deletions src/service/args/args.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export interface Args {
squash?: boolean, // if false use squashed/merged commit otherwise backport all commits as part of the pr
strategy?: string, // cherry-pick merge strategy
strategyOption?: string, // cherry-pick merge strategy option
cherryPickOptions?: string, // additional cherry-pick options
comments?: string[], // additional comments to be posted
}
2 changes: 2 additions & 0 deletions src/service/args/cli/cli-args-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default class CLIArgsParser extends ArgsParser {
.option("--no-squash", "if provided the tool will backport all commits as part of the pull request")
.option("--strategy <strategy>", "cherry-pick merge strategy, default to 'recursive'", undefined)
.option("--strategy-option <strategy-option>", "cherry-pick merge strategy option, default to 'theirs'")
.option("--cherry-pick-options <options>", "additional cherry-pick options")
.option("--comments <comments>", "semicolon separated list of additional comments to be posted to the backported pull request", getAsSemicolonSeparatedList)
.option("-cf, --config-file <config-file>", "configuration file containing all valid options, the json must match Args interface");
}
Expand Down Expand Up @@ -67,6 +68,7 @@ export default class CLIArgsParser extends ArgsParser {
squash: opts.squash,
strategy: opts.strategy,
strategyOption: opts.strategyOption,
cherryPickOptions: opts.cherryPickOptions,
comments: opts.comments,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/service/args/gha/gha-args-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class GHAArgsParser extends ArgsParser {
squash: !getAsBooleanOrDefault(getInput("no-squash")),
strategy: getOrUndefined(getInput("strategy")),
strategyOption: getOrUndefined(getInput("strategy-option")),
cherryPickOptions: getOrUndefined(getInput("cherry-pick-options")),
comments: getAsSemicolonSeparatedList(getInput("comments")),
};
}
Expand Down
1 change: 1 addition & 0 deletions src/service/configs/configs.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Configs {
folder: string,
mergeStrategy?: string, // cherry-pick merge strategy
mergeStrategyOption?: string, // cherry-pick merge strategy option
cherryPickOptions?: string, // additional cherry-pick options
originalPullRequest: GitPullRequest,
backportPullRequests: BackportPullRequest[],
}
Expand Down
1 change: 1 addition & 0 deletions src/service/configs/pullrequest/pr-configs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class PullRequestConfigsParser extends ConfigsParser {
folder: `${folder.startsWith("/") ? "" : process.cwd() + "/"}${args.folder ?? this.getDefaultFolder()}`,
mergeStrategy: args.strategy,
mergeStrategyOption: args.strategyOption,
cherryPickOptions: args.cherryPickOptions,
originalPullRequest: pr,
backportPullRequests: this.generateBackportPullRequestsData(pr, args, targetBranches, bpBranchNames),
git: {
Expand Down
9 changes: 7 additions & 2 deletions src/service/git/git-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ export default class GitCLIService {
* @param cwd repository in which the sha should be cherry picked to
* @param sha commit sha
*/
async cherryPick(cwd: string, sha: string, strategy = "recursive", strategyOption = "theirs"): Promise<void> {
async cherryPick(cwd: string, sha: string, strategy = "recursive", strategyOption = "theirs", cherryPickOptions: string | undefined): Promise<void> {
this.logger.info(`Cherry picking ${sha}`);

const options = ["cherry-pick", "-m", "1", `--strategy=${strategy}`, `--strategy-option=${strategyOption}`, sha];
let options = ["cherry-pick", "-m", "1", `--strategy=${strategy}`, `--strategy-option=${strategyOption}`];
if (cherryPickOptions !== undefined) {
options = options.concat(cherryPickOptions.split(" "));
}
options.push(sha);
this.logger.debug(`Cherry picking command git ${options}`);
try {
await this.git(cwd).raw(options);
} catch(error) {
Expand Down
2 changes: 1 addition & 1 deletion src/service/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default class Runner {
// 7. apply all changes to the new branch
this.logger.debug("Cherry picking commits..");
for (const sha of originalPR.commits!) {
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption);
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions);
}

if (!configs.dryRun) {
Expand Down
14 changes: 13 additions & 1 deletion test/service/args/cli/cli-args-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("with config file [default, short]", () => {
Expand Down Expand Up @@ -110,6 +111,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("valid execution [default, long]", () => {
Expand Down Expand Up @@ -141,6 +143,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("with config file [default, long]", () => {
Expand Down Expand Up @@ -170,6 +173,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("valid execution [override, short]", () => {
Expand Down Expand Up @@ -208,6 +212,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("valid execution [override, long]", () => {
Expand Down Expand Up @@ -264,6 +269,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("override using config file", () => {
Expand Down Expand Up @@ -293,6 +299,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("ignore custom option when config file is set", () => {
Expand Down Expand Up @@ -351,6 +358,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("override squash to false", () => {
Expand Down Expand Up @@ -383,7 +391,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(false);
});

test("override cherry pick strategies", () => {
test("override cherry pick strategies and options", () => {
addProcessArgs([
"--target-branch",
"target",
Expand All @@ -393,6 +401,8 @@ describe("cli args parser", () => {
"ort",
"--strategy-option",
"ours",
"--cherry-pick-options",
"--allow-empty -x",
]);

const args: Args = parser.parse();
Expand All @@ -416,6 +426,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual("ort");
expect(args.strategyOption).toEqual("ours");
expect(args.cherryPickOptions).toEqual("--allow-empty -x");
});

test("additional pr comments", () => {
Expand Down Expand Up @@ -479,6 +490,7 @@ describe("cli args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("invalid execution with empty target branch", () => {
Expand Down
6 changes: 6 additions & 0 deletions test/service/args/gha/gha-args-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe("gha args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("valid execution [override]", () => {
Expand Down Expand Up @@ -113,6 +114,7 @@ describe("gha args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("using config file", () => {
Expand Down Expand Up @@ -140,6 +142,7 @@ describe("gha args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("ignore custom options when using config file", () => {
Expand Down Expand Up @@ -182,6 +185,7 @@ describe("gha args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});

test("override squash to false", () => {
Expand Down Expand Up @@ -235,6 +239,7 @@ describe("gha args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual("ort");
expect(args.strategyOption).toEqual("ours");
expect(args.cherryPickOptions).toEqual(undefined);
});

test("additional pr comments", () => {
Expand Down Expand Up @@ -287,6 +292,7 @@ describe("gha args parser", () => {
expect(args.squash).toEqual(true);
expect(args.strategy).toEqual(undefined);
expect(args.strategyOption).toEqual(undefined);
expect(args.cherryPickOptions).toEqual(undefined);
});


Expand Down

0 comments on commit fe6be83

Please sign in to comment.