mirror of
https://code.forgejo.org/actions/git-backporting.git
synced 2025-05-18 11:39:12 -04:00
feat: config file as option (#42)
Fix https://github.com/lampajr/backporting/issues/37 This enhancement required a huge refactoring where all arguments defaults have been centralized in one single place ArgsParser#parse
This commit is contained in:
parent
a88adeec35
commit
5ead31f606
27 changed files with 1465 additions and 219 deletions
42
test/service/args/args-utils.test.ts
Normal file
42
test/service/args/args-utils.test.ts
Normal file
|
@ -0,0 +1,42 @@
|
|||
import { parseArgs, readConfigFile } from "@bp/service/args/args-utils";
|
||||
import { createTestFile, removeTestFile } from "../../support/utils";
|
||||
|
||||
const RANDOM_CONFIG_FILE_CONTENT_PATHNAME = "./args-utils-test-random-config-file.json";
|
||||
const RANDOM_CONFIG_FILE_CONTENT = {
|
||||
"dryRun": true,
|
||||
"auth": "your-git-service-auth-token",
|
||||
"targetBranch": "target-branch-name",
|
||||
"pullRequest": "https://github.com/user/repo/pull/123",
|
||||
"folder": "/path/to/local/folder",
|
||||
"gitUser": "YourGitUser",
|
||||
"gitEmail": "your-email@example.com",
|
||||
"title": "Backport: Original PR Title",
|
||||
"body": "Backport: Original PR Body",
|
||||
"bodyPrefix": "backport <original-pr-link>",
|
||||
"bpBranchName": "backport-branch-name",
|
||||
"reviewers": ["reviewer1", "reviewer2"],
|
||||
"assignees": ["assignee1", "assignee2"],
|
||||
"inheritReviewers": true,
|
||||
};
|
||||
|
||||
|
||||
describe("args utils test suite", () => {
|
||||
beforeAll(() => {
|
||||
// create a temporary file
|
||||
createTestFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(RANDOM_CONFIG_FILE_CONTENT));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// clean up all temporary files
|
||||
removeTestFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME);
|
||||
});
|
||||
|
||||
test("check parseArgs function", () => {
|
||||
const asString = JSON.stringify(RANDOM_CONFIG_FILE_CONTENT);
|
||||
expect(parseArgs(asString)).toStrictEqual(RANDOM_CONFIG_FILE_CONTENT);
|
||||
});
|
||||
|
||||
test("check readConfigFile function", () => {
|
||||
expect(readConfigFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME)).toStrictEqual(RANDOM_CONFIG_FILE_CONTENT);
|
||||
});
|
||||
});
|
|
@ -1,9 +1,45 @@
|
|||
import { Args } from "@bp/service/args/args.types";
|
||||
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
|
||||
import { addProcessArgs, resetProcessArgs, expectArrayEqual } from "../../../support/utils";
|
||||
import { addProcessArgs, resetProcessArgs, expectArrayEqual, createTestFile, removeTestFile } from "../../../support/utils";
|
||||
|
||||
export const SIMPLE_CONFIG_FILE_CONTENT_PATHNAME = "./cli-args-parser-test-simple-config-file-pulls-1.json";
|
||||
export const SIMPLE_CONFIG_FILE_CONTENT = {
|
||||
"targetBranch": "target",
|
||||
"pullRequest": "https://localhost/whatever/pulls/1",
|
||||
};
|
||||
|
||||
const RANDOM_CONFIG_FILE_CONTENT_PATHNAME = "./cli-args-parser-test-random-config-file.json";
|
||||
const RANDOM_CONFIG_FILE_CONTENT = {
|
||||
"dryRun": true,
|
||||
"auth": "your-git-service-auth-token",
|
||||
"targetBranch": "target-branch-name",
|
||||
"pullRequest": "https://github.com/user/repo/pull/123",
|
||||
"folder": "/path/to/local/folder",
|
||||
"gitUser": "YourGitUser",
|
||||
"gitEmail": "your-email@example.com",
|
||||
"title": "Backport: Original PR Title",
|
||||
"body": "Backport: Original PR Body",
|
||||
"bodyPrefix": "backport <original-pr-link>",
|
||||
"bpBranchName": "backport-branch-name",
|
||||
"reviewers": ["reviewer1", "reviewer2"],
|
||||
"assignees": ["assignee1", "assignee2"],
|
||||
"inheritReviewers": true,
|
||||
};
|
||||
|
||||
describe("cli args parser", () => {
|
||||
let parser: CLIArgsParser;
|
||||
|
||||
beforeAll(() => {
|
||||
// create a temporary file
|
||||
createTestFile(SIMPLE_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(SIMPLE_CONFIG_FILE_CONTENT));
|
||||
createTestFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(RANDOM_CONFIG_FILE_CONTENT));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// clean up all temporary files
|
||||
removeTestFile(SIMPLE_CONFIG_FILE_CONTENT_PATHNAME);
|
||||
removeTestFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// create a fresh new instance every time
|
||||
|
@ -23,9 +59,32 @@ describe("cli args parser", () => {
|
|||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(false);
|
||||
expect(args.auth).toEqual("");
|
||||
expect(args.gitUser).toEqual("GitHub");
|
||||
expect(args.gitEmail).toEqual("noreply@github.com");
|
||||
expect(args.auth).toEqual(undefined);
|
||||
expect(args.gitUser).toEqual(undefined);
|
||||
expect(args.gitEmail).toEqual(undefined);
|
||||
expect(args.folder).toEqual(undefined);
|
||||
expect(args.targetBranch).toEqual("target");
|
||||
expect(args.pullRequest).toEqual("https://localhost/whatever/pulls/1");
|
||||
expect(args.title).toEqual(undefined);
|
||||
expect(args.body).toEqual(undefined);
|
||||
expect(args.bodyPrefix).toEqual(undefined);
|
||||
expect(args.bpBranchName).toEqual(undefined);
|
||||
expect(args.reviewers).toEqual([]);
|
||||
expect(args.assignees).toEqual([]);
|
||||
expect(args.inheritReviewers).toEqual(true);
|
||||
});
|
||||
|
||||
test("with config file [default, short]", () => {
|
||||
addProcessArgs([
|
||||
"-cf",
|
||||
SIMPLE_CONFIG_FILE_CONTENT_PATHNAME,
|
||||
]);
|
||||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(false);
|
||||
expect(args.auth).toEqual(undefined);
|
||||
expect(args.gitUser).toEqual(undefined);
|
||||
expect(args.gitEmail).toEqual(undefined);
|
||||
expect(args.folder).toEqual(undefined);
|
||||
expect(args.targetBranch).toEqual("target");
|
||||
expect(args.pullRequest).toEqual("https://localhost/whatever/pulls/1");
|
||||
|
@ -48,9 +107,32 @@ describe("cli args parser", () => {
|
|||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(false);
|
||||
expect(args.auth).toEqual("");
|
||||
expect(args.gitUser).toEqual("GitHub");
|
||||
expect(args.gitEmail).toEqual("noreply@github.com");
|
||||
expect(args.auth).toEqual(undefined);
|
||||
expect(args.gitUser).toEqual(undefined);
|
||||
expect(args.gitEmail).toEqual(undefined);
|
||||
expect(args.folder).toEqual(undefined);
|
||||
expect(args.targetBranch).toEqual("target");
|
||||
expect(args.pullRequest).toEqual("https://localhost/whatever/pulls/1");
|
||||
expect(args.title).toEqual(undefined);
|
||||
expect(args.body).toEqual(undefined);
|
||||
expect(args.bodyPrefix).toEqual(undefined);
|
||||
expect(args.bpBranchName).toEqual(undefined);
|
||||
expect(args.reviewers).toEqual([]);
|
||||
expect(args.assignees).toEqual([]);
|
||||
expect(args.inheritReviewers).toEqual(true);
|
||||
});
|
||||
|
||||
test("with config file [default, long]", () => {
|
||||
addProcessArgs([
|
||||
"--config-file",
|
||||
SIMPLE_CONFIG_FILE_CONTENT_PATHNAME,
|
||||
]);
|
||||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(false);
|
||||
expect(args.auth).toEqual(undefined);
|
||||
expect(args.gitUser).toEqual(undefined);
|
||||
expect(args.gitEmail).toEqual(undefined);
|
||||
expect(args.folder).toEqual(undefined);
|
||||
expect(args.targetBranch).toEqual("target");
|
||||
expect(args.pullRequest).toEqual("https://localhost/whatever/pulls/1");
|
||||
|
@ -135,9 +217,78 @@ describe("cli args parser", () => {
|
|||
expect(args.body).toEqual("New Body");
|
||||
expect(args.bodyPrefix).toEqual("New Body Prefix");
|
||||
expect(args.bpBranchName).toEqual("bp_branch_name");
|
||||
expectArrayEqual(["al", "john", "jack"], args.reviewers!);
|
||||
expectArrayEqual(["pippo", "pluto", "paperino"], args.assignees!);
|
||||
expectArrayEqual(args.reviewers!, ["al", "john", "jack"]);
|
||||
expectArrayEqual(args.assignees!, ["pippo", "pluto", "paperino"]);
|
||||
expect(args.inheritReviewers).toEqual(false);
|
||||
});
|
||||
|
||||
test("override using config file", () => {
|
||||
addProcessArgs([
|
||||
"--config-file",
|
||||
RANDOM_CONFIG_FILE_CONTENT_PATHNAME,
|
||||
]);
|
||||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(true);
|
||||
expect(args.auth).toEqual("your-git-service-auth-token");
|
||||
expect(args.gitUser).toEqual("YourGitUser");
|
||||
expect(args.gitEmail).toEqual("your-email@example.com");
|
||||
expect(args.folder).toEqual("/path/to/local/folder");
|
||||
expect(args.targetBranch).toEqual("target-branch-name");
|
||||
expect(args.pullRequest).toEqual("https://github.com/user/repo/pull/123");
|
||||
expect(args.title).toEqual("Backport: Original PR Title");
|
||||
expect(args.body).toEqual("Backport: Original PR Body");
|
||||
expect(args.bodyPrefix).toEqual("backport <original-pr-link>");
|
||||
expect(args.bpBranchName).toEqual("backport-branch-name");
|
||||
expectArrayEqual(args.reviewers!, ["reviewer1", "reviewer2"]);
|
||||
expectArrayEqual(args.assignees!,["assignee1", "assignee2"]);
|
||||
expect(args.inheritReviewers).toEqual(true);
|
||||
});
|
||||
|
||||
test("ignore custom option when config file is set", () => {
|
||||
addProcessArgs([
|
||||
"--config-file",
|
||||
RANDOM_CONFIG_FILE_CONTENT_PATHNAME,
|
||||
"--dry-run",
|
||||
"--auth",
|
||||
"bearer-token",
|
||||
"--target-branch",
|
||||
"target",
|
||||
"--pull-request",
|
||||
"https://localhost/whatever/pulls/1",
|
||||
"--git-user",
|
||||
"Me",
|
||||
"--git-email",
|
||||
"me@email.com",
|
||||
"--title",
|
||||
"New Title",
|
||||
"--body",
|
||||
"New Body",
|
||||
"--body-prefix",
|
||||
"New Body Prefix",
|
||||
"--bp-branch-name",
|
||||
"bp_branch_name",
|
||||
"--reviewers",
|
||||
"al , john, jack",
|
||||
"--assignees",
|
||||
" pippo,pluto, paperino",
|
||||
"--no-inherit-reviewers",
|
||||
]);
|
||||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(true);
|
||||
expect(args.auth).toEqual("your-git-service-auth-token");
|
||||
expect(args.gitUser).toEqual("YourGitUser");
|
||||
expect(args.gitEmail).toEqual("your-email@example.com");
|
||||
expect(args.folder).toEqual("/path/to/local/folder");
|
||||
expect(args.targetBranch).toEqual("target-branch-name");
|
||||
expect(args.pullRequest).toEqual("https://github.com/user/repo/pull/123");
|
||||
expect(args.title).toEqual("Backport: Original PR Title");
|
||||
expect(args.body).toEqual("Backport: Original PR Body");
|
||||
expect(args.bodyPrefix).toEqual("backport <original-pr-link>");
|
||||
expect(args.bpBranchName).toEqual("backport-branch-name");
|
||||
expectArrayEqual(args.reviewers!, ["reviewer1", "reviewer2"]);
|
||||
expectArrayEqual(args.assignees!,["assignee1", "assignee2"]);
|
||||
expect(args.inheritReviewers).toEqual(true);
|
||||
});
|
||||
});
|
|
@ -1,10 +1,46 @@
|
|||
import { Args } from "@bp/service/args/args.types";
|
||||
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
|
||||
import { spyGetInput, expectArrayEqual } from "../../../support/utils";
|
||||
import { spyGetInput, expectArrayEqual, removeTestFile, createTestFile } from "../../../support/utils";
|
||||
|
||||
const SIMPLE_CONFIG_FILE_CONTENT_PATHNAME = "./gha-args-parser-test-simple-config-file-pulls-1.json";
|
||||
const SIMPLE_CONFIG_FILE_CONTENT = {
|
||||
"targetBranch": "target",
|
||||
"pullRequest": "https://localhost/whatever/pulls/1",
|
||||
};
|
||||
|
||||
const RANDOM_CONFIG_FILE_CONTENT_PATHNAME = "./gha-args-parser-test-random-config-file.json";
|
||||
const RANDOM_CONFIG_FILE_CONTENT = {
|
||||
"dryRun": true,
|
||||
"auth": "your-git-service-auth-token",
|
||||
"targetBranch": "target-branch-name",
|
||||
"pullRequest": "https://github.com/user/repo/pull/123",
|
||||
"folder": "/path/to/local/folder",
|
||||
"gitUser": "YourGitUser",
|
||||
"gitEmail": "your-email@example.com",
|
||||
"title": "Backport: Original PR Title",
|
||||
"body": "Backport: Original PR Body",
|
||||
"bodyPrefix": "backport <original-pr-link>",
|
||||
"bpBranchName": "backport-branch-name",
|
||||
"reviewers": ["reviewer1", "reviewer2"],
|
||||
"assignees": ["assignee1", "assignee2"],
|
||||
"inheritReviewers": true,
|
||||
};
|
||||
|
||||
describe("gha args parser", () => {
|
||||
let parser: GHAArgsParser;
|
||||
|
||||
beforeAll(() => {
|
||||
// create a temporary file
|
||||
createTestFile(SIMPLE_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(SIMPLE_CONFIG_FILE_CONTENT));
|
||||
createTestFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(RANDOM_CONFIG_FILE_CONTENT));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
// clean up all temporary files
|
||||
removeTestFile(SIMPLE_CONFIG_FILE_CONTENT_PATHNAME);
|
||||
removeTestFile(RANDOM_CONFIG_FILE_CONTENT_PATHNAME);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// create a fresh new instance every time
|
||||
parser = new GHAArgsParser();
|
||||
|
@ -14,14 +50,6 @@ describe("gha args parser", () => {
|
|||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test("getOrDefault", () => {
|
||||
spyGetInput({
|
||||
"present": "value"
|
||||
});
|
||||
expect(parser.getOrDefault("not-present", "default")).toStrictEqual("default");
|
||||
expect(parser.getOrDefault("present", "default")).toStrictEqual("value");
|
||||
});
|
||||
|
||||
test("getOrUndefined", () => {
|
||||
spyGetInput({
|
||||
"present": "value",
|
||||
|
@ -38,8 +66,8 @@ describe("gha args parser", () => {
|
|||
"blank": " ",
|
||||
});
|
||||
expectArrayEqual(parser.getAsCommaSeparatedList("present")!, ["value1", "value2", "value3"]);
|
||||
expect(parser.getAsCommaSeparatedList("empty")).toStrictEqual([]);
|
||||
expect(parser.getAsCommaSeparatedList("blank")).toStrictEqual([]);
|
||||
expect(parser.getAsCommaSeparatedList("empty")).toStrictEqual(undefined);
|
||||
expect(parser.getAsCommaSeparatedList("blank")).toStrictEqual(undefined);
|
||||
});
|
||||
|
||||
test("valid execution [default]", () => {
|
||||
|
@ -50,9 +78,9 @@ describe("gha args parser", () => {
|
|||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(false);
|
||||
expect(args.auth).toEqual("");
|
||||
expect(args.gitUser).toEqual("GitHub");
|
||||
expect(args.gitEmail).toEqual("noreply@github.com");
|
||||
expect(args.auth).toEqual(undefined);
|
||||
expect(args.gitUser).toEqual(undefined);
|
||||
expect(args.gitEmail).toEqual(undefined);
|
||||
expect(args.folder).toEqual(undefined);
|
||||
expect(args.targetBranch).toEqual("target");
|
||||
expect(args.pullRequest).toEqual("https://localhost/whatever/pulls/1");
|
||||
|
@ -92,9 +120,65 @@ describe("gha args parser", () => {
|
|||
expect(args.body).toEqual("New Body");
|
||||
expect(args.bodyPrefix).toEqual("New Body Prefix");
|
||||
expect(args.bpBranchName).toEqual("bp_branch_name");
|
||||
expectArrayEqual(["al", "john", "jack"], args.reviewers!);
|
||||
expectArrayEqual(["pippo", "pluto", "paperino"], args.assignees!);
|
||||
expectArrayEqual(args.reviewers!, ["al", "john", "jack"]);
|
||||
expectArrayEqual(args.assignees!, ["pippo", "pluto", "paperino"]);
|
||||
expect(args.inheritReviewers).toEqual(false);
|
||||
});
|
||||
|
||||
test("using config file", () => {
|
||||
spyGetInput({
|
||||
"config-file": SIMPLE_CONFIG_FILE_CONTENT_PATHNAME,
|
||||
});
|
||||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(false);
|
||||
expect(args.auth).toEqual(undefined);
|
||||
expect(args.gitUser).toEqual(undefined);
|
||||
expect(args.gitEmail).toEqual(undefined);
|
||||
expect(args.folder).toEqual(undefined);
|
||||
expect(args.targetBranch).toEqual("target");
|
||||
expect(args.pullRequest).toEqual("https://localhost/whatever/pulls/1");
|
||||
expect(args.title).toEqual(undefined);
|
||||
expect(args.body).toEqual(undefined);
|
||||
expect(args.bodyPrefix).toEqual(undefined);
|
||||
expect(args.bpBranchName).toEqual(undefined);
|
||||
expect(args.reviewers).toEqual([]);
|
||||
expect(args.assignees).toEqual([]);
|
||||
expect(args.inheritReviewers).toEqual(true);
|
||||
});
|
||||
|
||||
test("ignore custom options when using config file", () => {
|
||||
spyGetInput({
|
||||
"config-file": RANDOM_CONFIG_FILE_CONTENT_PATHNAME,
|
||||
"dry-run": "true",
|
||||
"auth": "bearer-token",
|
||||
"target-branch": "target",
|
||||
"pull-request": "https://localhost/whatever/pulls/1",
|
||||
"git-user": "Me",
|
||||
"git-email": "me@email.com",
|
||||
"title": "New Title",
|
||||
"body": "New Body",
|
||||
"body-prefix": "New Body Prefix",
|
||||
"bp-branch-name": "bp_branch_name",
|
||||
"reviewers": "al , john, jack",
|
||||
"assignees": " pippo,pluto, paperino",
|
||||
"no-inherit-reviewers": "true",
|
||||
});
|
||||
|
||||
const args: Args = parser.parse();
|
||||
expect(args.dryRun).toEqual(true);
|
||||
expect(args.auth).toEqual("your-git-service-auth-token");
|
||||
expect(args.gitUser).toEqual("YourGitUser");
|
||||
expect(args.gitEmail).toEqual("your-email@example.com");
|
||||
expect(args.folder).toEqual("/path/to/local/folder");
|
||||
expect(args.targetBranch).toEqual("target-branch-name");
|
||||
expect(args.pullRequest).toEqual("https://github.com/user/repo/pull/123");
|
||||
expect(args.title).toEqual("Backport: Original PR Title");
|
||||
expect(args.body).toEqual("Backport: Original PR Body");
|
||||
expect(args.bodyPrefix).toEqual("backport <original-pr-link>");
|
||||
expect(args.bpBranchName).toEqual("backport-branch-name");
|
||||
expectArrayEqual(args.reviewers!, ["reviewer1", "reviewer2"]);
|
||||
expectArrayEqual(args.assignees!,["assignee1", "assignee2"]);
|
||||
expect(args.inheritReviewers).toEqual(true);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue