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:
Andrea Lamparelli 2023-07-05 22:11:23 +02:00 committed by GitHub
parent a88adeec35
commit 5ead31f606
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 1465 additions and 219 deletions

View 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);
});
});

View file

@ -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);
});
});

View file

@ -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);
});
});

View file

@ -4,7 +4,31 @@ import PullRequestConfigsParser from "@bp/service/configs/pullrequest/pr-configs
import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";
import { mockGitHubClient } from "../../../support/mock/git-client-mock-support";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../../support/utils";
import { mergedPullRequestFixture, openPullRequestFixture, notMergedPullRequestFixture, repo, targetOwner } from "../../../support/mock/github-data";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
const GITHUB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME = "./github-pr-configs-parser-simple-pr-merged.json";
const GITHUB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT = {
"targetBranch": "prod",
"pullRequest": `https://github.com/${targetOwner}/${repo}/pull/${mergedPullRequestFixture.number}`,
};
const GITHUB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME = "./github-pr-configs-parser-complex-pr-merged.json";
const GITHUB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT = {
"dryRun": false,
"auth": "my-auth-token",
"pullRequest": `https://github.com/${targetOwner}/${repo}/pull/${mergedPullRequestFixture.number}`,
"targetBranch": "prod",
"gitUser": "Me",
"gitEmail": "me@email.com",
"title": "New Title",
"body": "New Body",
"bodyPrefix": "New Body Prefix -",
"reviewers": ["user1", "user2"],
"assignees": ["user3", "user4"],
"inheritReviewers": true, // not taken into account
};
describe("github pull request config parser", () => {
@ -12,17 +36,33 @@ describe("github pull request config parser", () => {
const openPRUrl = `https://github.com/${targetOwner}/${repo}/pull/${openPullRequestFixture.number}`;
const notMergedPRUrl = `https://github.com/${targetOwner}/${repo}/pull/${notMergedPullRequestFixture.number}`;
let parser: PullRequestConfigsParser;
let argsParser: CLIArgsParser;
let configParser: PullRequestConfigsParser;
beforeAll(() => {
// create a temporary file
createTestFile(GITHUB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITHUB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT));
createTestFile(GITHUB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITHUB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT));
GitClientFactory.reset();
GitClientFactory.getOrCreate(GitClientType.GITHUB, "whatever", "http://localhost/api/v3");
});
afterAll(() => {
// clean up all temporary files
removeTestFile(GITHUB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME);
removeTestFile(GITHUB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME);
});
beforeEach(() => {
mockGitHubClient("http://localhost/api/v3");
parser = new PullRequestConfigsParser();
// reset process.env variables
resetProcessArgs();
// create a fresh new instance every time
argsParser = new CLIArgsParser();
configParser = new PullRequestConfigsParser();
});
afterEach(() => {
@ -42,7 +82,7 @@ describe("github pull request config parser", () => {
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -113,7 +153,7 @@ describe("github pull request config parser", () => {
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
@ -138,7 +178,7 @@ describe("github pull request config parser", () => {
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
@ -189,7 +229,7 @@ describe("github pull request config parser", () => {
inheritReviewers: true,
};
expect(async () => await parser.parseAndValidate(args)).rejects.toThrow("Provided pull request is closed and not merged!");
expect(async () => await configParser.parseAndValidate(args)).rejects.toThrow("Provided pull request is closed and not merged!");
});
@ -209,7 +249,7 @@ describe("github pull request config parser", () => {
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -283,7 +323,7 @@ describe("github pull request config parser", () => {
inheritReviewers: true, // not taken into account
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -357,7 +397,7 @@ describe("github pull request config parser", () => {
inheritReviewers: false,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -414,4 +454,133 @@ describe("github pull request config parser", () => {
bpBranchName: undefined,
});
});
test("using simple config file", async () => {
addProcessArgs([
"-cf",
GITHUB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME,
]);
const args: Args = argsParser.parse();
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
user: "GitHub",
email: "noreply@github.com"
});
expect(configs.auth).toEqual(undefined);
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
author: "gh-user",
url: "https://api.github.com/repos/owner/reponame/pulls/2368",
htmlUrl: "https://github.com/owner/reponame/pull/2368",
state: "closed",
merged: true,
mergedBy: "that-s-a-user",
title: "PR Title",
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
targetRepo: {
owner: "owner",
project: "reponame",
cloneUrl: "https://github.com/owner/reponame.git"
},
sourceRepo: {
owner: "fork",
project: "reponame",
cloneUrl: "https://github.com/fork/reponame.git"
},
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"]
});
expect(configs.backportPullRequest).toEqual({
author: "GitHub",
url: undefined,
htmlUrl: undefined,
title: "[prod] PR Title",
body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
targetRepo: {
owner: "owner",
project: "reponame",
cloneUrl: "https://github.com/owner/reponame.git"
},
sourceRepo: {
owner: "owner",
project: "reponame",
cloneUrl: "https://github.com/owner/reponame.git"
},
bpBranchName: undefined,
});
});
test("using complex config file", async () => {
addProcessArgs([
"-cf",
GITHUB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME,
]);
const args: Args = argsParser.parse();
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
user: "Me",
email: "me@email.com"
});
expect(configs.auth).toEqual("my-auth-token");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
author: "gh-user",
url: "https://api.github.com/repos/owner/reponame/pulls/2368",
htmlUrl: "https://github.com/owner/reponame/pull/2368",
state: "closed",
merged: true,
mergedBy: "that-s-a-user",
title: "PR Title",
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
targetRepo: {
owner: "owner",
project: "reponame",
cloneUrl: "https://github.com/owner/reponame.git"
},
sourceRepo: {
owner: "fork",
project: "reponame",
cloneUrl: "https://github.com/fork/reponame.git"
},
bpBranchName: undefined,
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
author: "Me",
url: undefined,
htmlUrl: undefined,
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: ["user1", "user2"],
assignees: ["user3", "user4"],
targetRepo: {
owner: "owner",
project: "reponame",
cloneUrl: "https://github.com/owner/reponame.git"
},
sourceRepo: {
owner: "owner",
project: "reponame",
cloneUrl: "https://github.com/owner/reponame.git"
},
bpBranchName: undefined,
});
});
});

View file

@ -5,6 +5,31 @@ import GitClientFactory from "@bp/service/git/git-client-factory";
import { GitClientType } from "@bp/service/git/git.types";
import { getAxiosMocked } from "../../../support/mock/git-client-mock-support";
import { CLOSED_NOT_MERGED_MR, MERGED_SQUASHED_MR, OPEN_MR } from "../../../support/mock/gitlab-data";
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
import { createTestFile, removeTestFile, spyGetInput } from "../../../support/utils";
const GITLAB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME = "./gitlab-pr-configs-parser-simple-pr-merged.json";
const GITLAB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT = {
"targetBranch": "prod",
"pullRequest": `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`,
};
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME = "./gitlab-pr-configs-parser-complex-pr-merged.json";
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT = {
"dryRun": false,
"auth": "my-token",
"pullRequest": `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`,
"targetBranch": "prod",
"gitUser": "Me",
"gitEmail": "me@email.com",
"title": "New Title",
"body": "New Body",
"bodyPrefix": "New Body Prefix -",
"reviewers": [],
"assignees": ["user3", "user4"],
"inheritReviewers": false,
};
jest.mock("axios", () => {
return {
@ -20,15 +45,27 @@ describe("gitlab merge request config parser", () => {
const openPRUrl = `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${OPEN_MR.iid}`;
const notMergedPRUrl = `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${CLOSED_NOT_MERGED_MR.iid}`;
let parser: PullRequestConfigsParser;
let argsParser: GHAArgsParser;
let configParser: PullRequestConfigsParser;
beforeAll(() => {
// create a temporary file
createTestFile(GITLAB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITLAB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT));
createTestFile(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT));
GitClientFactory.reset();
GitClientFactory.getOrCreate(GitClientType.GITLAB, "whatever", "my.gitlab.host.com");
});
afterAll(() => {
// clean up all temporary files
removeTestFile(GITLAB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME);
removeTestFile(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME);
});
beforeEach(() => {
parser = new PullRequestConfigsParser();
argsParser = new GHAArgsParser();
configParser = new PullRequestConfigsParser();
});
afterEach(() => {
@ -38,24 +75,24 @@ describe("gitlab merge request config parser", () => {
test("parse configs from merge request", async () => {
const args: Args = {
dryRun: false,
auth: "",
auth: undefined,
pullRequest: mergedPRUrl,
targetBranch: "prod",
gitUser: "GitLab",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
user: "GitLab",
user: "Gitlab",
email: "noreply@gitlab.com"
});
expect(configs.auth).toEqual("");
expect(configs.auth).toEqual(undefined);
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
@ -84,7 +121,7 @@ describe("gitlab merge request config parser", () => {
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
author: "GitLab",
author: "Gitlab",
url: undefined,
htmlUrl: undefined,
title: "[prod] Update test.txt",
@ -113,21 +150,21 @@ describe("gitlab merge request config parser", () => {
pullRequest: mergedPRUrl,
targetBranch: "prod",
folder: "/tmp/test",
gitUser: "GitLab",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual("/tmp/test");
expect(configs.git).toEqual({
user: "GitLab",
user: "Gitlab",
email: "noreply@gitlab.com"
});
});
@ -138,20 +175,20 @@ describe("gitlab merge request config parser", () => {
auth: "whatever",
pullRequest: openPRUrl,
targetBranch: "prod",
gitUser: "GitLab",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.targetBranch).toEqual("prod");
expect(configs.git).toEqual({
user: "GitLab",
user: "Gitlab",
email: "noreply@gitlab.com"
});
expect(configs.originalPullRequest).toEqual({
@ -189,14 +226,14 @@ describe("gitlab merge request config parser", () => {
auth: "whatever",
pullRequest: notMergedPRUrl,
targetBranch: "prod",
gitUser: "GitLab",
gitUser: "Gitlab",
gitEmail: "noreply@gitlab.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
};
expect(async () => await parser.parseAndValidate(args)).rejects.toThrow("Provided pull request is closed and not merged!");
expect(async () => await configParser.parseAndValidate(args)).rejects.toThrow("Provided pull request is closed and not merged!");
});
test("override backport pr data inherting reviewers", async () => {
@ -215,7 +252,7 @@ describe("gitlab merge request config parser", () => {
inheritReviewers: true,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -288,7 +325,7 @@ describe("gitlab merge request config parser", () => {
inheritReviewers: true, // not taken into account
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -361,7 +398,7 @@ describe("gitlab merge request config parser", () => {
inheritReviewers: false,
};
const configs: Configs = await parser.parseAndValidate(args);
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
@ -417,4 +454,131 @@ describe("gitlab merge request config parser", () => {
bpBranchName: undefined,
});
});
test("using simple config file", async () => {
spyGetInput({
"config-file": GITLAB_MERGED_PR_SIMPLE_CONFIG_FILE_CONTENT_PATHNAME,
});
const args: Args = argsParser.parse();
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
user: "Gitlab",
email: "noreply@gitlab.com"
});
expect(configs.auth).toEqual(undefined);
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
author: "superuser",
url: "https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1",
htmlUrl: "https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1",
state: "merged",
merged: true,
mergedBy: "superuser",
title: "Update test.txt",
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
sourceRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
author: "Gitlab",
url: undefined,
htmlUrl: undefined,
title: "[prod] Update test.txt",
body: "**Backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1\r\n\r\nThis is the body",
reviewers: ["superuser"],
assignees: [],
targetRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
sourceRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
bpBranchName: undefined,
});
});
test("using complex config file", async () => {
spyGetInput({
"config-file": GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME,
});
const args: Args = argsParser.parse();
const configs: Configs = await configParser.parseAndValidate(args);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
user: "Me",
email: "me@email.com"
});
expect(configs.auth).toEqual("my-token");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
author: "superuser",
url: "https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1",
htmlUrl: "https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1",
state: "merged",
merged: true,
mergedBy: "superuser",
title: "Update test.txt",
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
sourceRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
author: "Me",
url: undefined,
htmlUrl: undefined,
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
sourceRepo: {
owner: "superuser",
project: "backporting-example",
cloneUrl: "https://my.gitlab.host.com/superuser/backporting-example.git"
},
bpBranchName: undefined,
});
});
});

View file

@ -3,15 +3,42 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitHubClient from "@bp/service/git/github/github-client";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import { addProcessArgs, resetProcessArgs } from "../../support/utils";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../support/utils";
import { mockGitHubClient } from "../../support/mock/git-client-mock-support";
const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME = "./cli-github-runner-pr-merged-with-overrides.json";
const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT = {
"dryRun": false,
"auth": "my-auth-token",
"pullRequest": "https://github.com/owner/reponame/pull/2368",
"targetBranch": "target",
"gitUser": "Me",
"gitEmail": "me@email.com",
"title": "New Title",
"body": "New Body",
"bodyPrefix": "New Body Prefix - ",
"bpBranchName": "bp_branch_name",
"reviewers": [],
"assignees": ["user3", "user4"],
"inheritReviewers": false,
};
jest.mock("@bp/service/git/git-cli");
jest.spyOn(GitHubClient.prototype, "createPullRequest");
let parser: ArgsParser;
let runner: Runner;
beforeAll(() => {
// create a temporary file
createTestFile(GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT));
});
afterAll(() => {
// clean up all temporary files
removeTestFile(GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME);
});
beforeEach(() => {
mockGitHubClient();
@ -391,4 +418,43 @@ describe("cli runner", () => {
}
);
});
test("using config file with overrides", async () => {
addProcessArgs([
"--config-file",
GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME,
]);
await runner.execute();
const cwd = process.cwd() + "/bp";
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp_branch_name");
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/2368/head:pr/2368");
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp_branch_name");
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
owner: "owner",
repo: "reponame",
head: "bp_branch_name",
base: "target",
title: "New Title",
body: "New Body Prefix - New Body",
reviewers: [],
assignees: ["user3", "user4"],
}
);
});
});

View file

@ -3,8 +3,25 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitLabClient from "@bp/service/git/gitlab/gitlab-client";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import { addProcessArgs, resetProcessArgs } from "../../support/utils";
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../support/utils";
import { getAxiosMocked } from "../../support/mock/git-client-mock-support";
import { MERGED_SQUASHED_MR } from "../../support/mock/gitlab-data";
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME = "./cli-gitlab-runner-pr-merged-with-overrides.json";
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT = {
"dryRun": false,
"auth": "my-token",
"pullRequest": `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`,
"targetBranch": "prod",
"gitUser": "Me",
"gitEmail": "me@email.com",
"title": "New Title",
"body": "New Body",
"bodyPrefix": `**This is a backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`,
"reviewers": [],
"assignees": ["user3", "user4"],
"inheritReviewers": false,
};
jest.mock("axios", () => {
return {
@ -27,6 +44,16 @@ jest.spyOn(GitLabClient.prototype, "createPullRequest");
let parser: ArgsParser;
let runner: Runner;
beforeAll(() => {
// create a temporary file
createTestFile(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT));
});
afterAll(() => {
// clean up all temporary files
removeTestFile(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME);
});
beforeEach(() => {
// create CLI arguments parser
parser = new CLIArgsParser();
@ -306,4 +333,44 @@ describe("cli runner", () => {
}
);
});
test("using config file with overrides", async () => {
addProcessArgs([
"--config-file",
GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME,
]);
await runner.execute();
const cwd = process.cwd() + "/bp";
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "prod");
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-prod-ebb1eca696c42fd067658bd9b5267709f78ef38e");
// 0 occurrences as the mr is already merged and the owner is the same for
// both source and target repositories
expect(GitCLIService.prototype.fetch).toBeCalledTimes(0);
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "ebb1eca696c42fd067658bd9b5267709f78ef38e");
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-prod-ebb1eca696c42fd067658bd9b5267709f78ef38e");
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca696c42fd067658bd9b5267709f78ef38e",
base: "prod",
title: "New Title",
body: expect.stringContaining("**This is a backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
reviewers: [],
assignees: ["user3", "user4"],
}
);
});
});

View file

@ -3,15 +3,43 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitHubClient from "@bp/service/git/github/github-client";
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
import { spyGetInput } from "../../support/utils";
import { createTestFile, removeTestFile, spyGetInput } from "../../support/utils";
import { mockGitHubClient } from "../../support/mock/git-client-mock-support";
const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME = "./gha-github-runner-pr-merged-with-overrides.json";
const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT = {
"dryRun": false,
"auth": "my-auth-token",
"pullRequest": "https://github.com/owner/reponame/pull/2368",
"targetBranch": "target",
"gitUser": "Me",
"gitEmail": "me@email.com",
"title": "New Title",
"body": "New Body",
"bodyPrefix": "New Body Prefix - ",
"bpBranchName": "bp_branch_name",
"reviewers": [],
"assignees": ["user3", "user4"],
"inheritReviewers": false,
};
jest.mock("@bp/service/git/git-cli");
jest.spyOn(GitHubClient.prototype, "createPullRequest");
let parser: ArgsParser;
let runner: Runner;
beforeAll(() => {
// create a temporary file
createTestFile(GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT));
});
afterAll(() => {
// clean up all temporary files
removeTestFile(GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME);
});
beforeEach(() => {
mockGitHubClient();
@ -231,4 +259,42 @@ describe("gha runner", () => {
}
);
});
test("using config file with overrides", async () => {
spyGetInput({
"config-file": GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT_PATHNAME,
});
await runner.execute();
const cwd = process.cwd() + "/bp";
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "target");
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp_branch_name");
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/2368/head:pr/2368");
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp_branch_name");
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
owner: "owner",
repo: "reponame",
head: "bp_branch_name",
base: "target",
title: "New Title",
body: "New Body Prefix - New Body",
reviewers: [],
assignees: ["user3", "user4"],
}
);
});
});

View file

@ -3,8 +3,25 @@ import Runner from "@bp/service/runner/runner";
import GitCLIService from "@bp/service/git/git-cli";
import GitLabClient from "@bp/service/git/gitlab/gitlab-client";
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
import { spyGetInput } from "../../support/utils";
import { createTestFile, removeTestFile, spyGetInput } from "../../support/utils";
import { getAxiosMocked } from "../../support/mock/git-client-mock-support";
import { MERGED_SQUASHED_MR } from "../../support/mock/gitlab-data";
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME = "./gha-gitlab-runner-pr-merged-with-overrides.json";
const GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT = {
"dryRun": false,
"auth": "my-token",
"pullRequest": `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`,
"targetBranch": "prod",
"gitUser": "Me",
"gitEmail": "me@email.com",
"title": "New Title",
"body": "New Body",
"bodyPrefix": `**This is a backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`,
"reviewers": [],
"assignees": ["user3", "user4"],
"inheritReviewers": false,
};
jest.mock("axios", () => {
return {
@ -26,6 +43,16 @@ jest.spyOn(GitLabClient.prototype, "createPullRequest");
let parser: ArgsParser;
let runner: Runner;
beforeAll(() => {
// create a temporary file
createTestFile(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME, JSON.stringify(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT));
});
afterAll(() => {
// clean up all temporary files
removeTestFile(GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME);
});
beforeEach(() => {
// create GHA arguments parser
parser = new GHAArgsParser();
@ -242,4 +269,43 @@ describe("gha runner", () => {
}
);
});
test("using config file with overrides", async () => {
spyGetInput({
"config-file": GITLAB_MERGED_PR_COMPLEX_CONFIG_FILE_CONTENT_PATHNAME,
});
await runner.execute();
const cwd = process.cwd() + "/bp";
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "prod");
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-prod-ebb1eca696c42fd067658bd9b5267709f78ef38e");
// 0 occurrences as the mr is already merged and the owner is the same for
// both source and target repositories
expect(GitCLIService.prototype.fetch).toBeCalledTimes(0);
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "ebb1eca696c42fd067658bd9b5267709f78ef38e");
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-prod-ebb1eca696c42fd067658bd9b5267709f78ef38e");
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca696c42fd067658bd9b5267709f78ef38e",
base: "prod",
title: "New Title",
body: expect.stringContaining("**This is a backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
reviewers: [],
assignees: ["user3", "user4"],
}
);
});
});

View file

@ -1,4 +1,5 @@
import * as core from "@actions/core";
import * as fs from "fs";
export const addProcessArgs = (args: string[]) => {
process.argv = [...process.argv, ...args];
@ -24,4 +25,21 @@ export const spyGetInput = (obj: any) => {
*/
export const expectArrayEqual = (actual: unknown[], expected: unknown[]) => {
expect(actual.sort()).toEqual(expected.sort());
};
/**
* Create a test file given the full pathname
* @param pathname full file pathname e.g, /tmp/dir/filename.json
* @param content what must be written in the file
*/
export const createTestFile = (pathname: string, content: string) => {
fs.writeFileSync(pathname, content);
};
/**
* Remove a file located at pathname
* @param pathname full file pathname e.g, /tmp/dir/filename.json
*/
export const removeTestFile = (pathname: string) => {
fs.rmSync(pathname);
};