mirror of
https://code.forgejo.org/actions/git-backporting.git
synced 2025-05-21 04:59: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
|
@ -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"],
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
|
@ -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"],
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
|
@ -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"],
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
|
@ -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"],
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue