2023-01-05 11:41:14 +01:00
|
|
|
import ArgsParser from "@bp/service/args/args-parser";
|
|
|
|
import Runner from "@bp/service/runner/runner";
|
|
|
|
import GitCLIService from "@bp/service/git/git-cli";
|
2023-07-02 00:05:17 +02:00
|
|
|
import GitHubClient from "@bp/service/git/github/github-client";
|
2023-01-05 11:41:14 +01:00
|
|
|
import GHAArgsParser from "@bp/service/args/gha/gha-args-parser";
|
|
|
|
import { spyGetInput } from "../../support/utils";
|
2023-07-02 00:05:17 +02:00
|
|
|
import { mockGitHubClient } from "../../support/mock/git-client-mock-support";
|
2023-01-05 11:41:14 +01:00
|
|
|
|
|
|
|
jest.mock("@bp/service/git/git-cli");
|
2023-07-02 00:05:17 +02:00
|
|
|
jest.spyOn(GitHubClient.prototype, "createPullRequest");
|
2023-01-05 11:41:14 +01:00
|
|
|
|
|
|
|
let parser: ArgsParser;
|
|
|
|
let runner: Runner;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-07-02 00:05:17 +02:00
|
|
|
mockGitHubClient();
|
2023-01-05 11:41:14 +01:00
|
|
|
|
|
|
|
// create GHA arguments parser
|
|
|
|
parser = new GHAArgsParser();
|
|
|
|
|
|
|
|
// create runner
|
|
|
|
runner = new Runner(parser);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("gha runner", () => {
|
|
|
|
test("with dry run", async () => {
|
|
|
|
spyGetInput({
|
|
|
|
"dry-run": "true",
|
|
|
|
"target-branch": "target",
|
|
|
|
"pull-request": "https://github.com/owner/reponame/pull/2368"
|
|
|
|
});
|
|
|
|
|
|
|
|
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-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");
|
|
|
|
|
|
|
|
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(0);
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(0);
|
2023-01-05 11:41:14 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
test("without dry run", async () => {
|
|
|
|
spyGetInput({
|
|
|
|
"target-branch": "target",
|
|
|
|
"pull-request": "https://github.com/owner/reponame/pull/2368"
|
|
|
|
});
|
|
|
|
|
|
|
|
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-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");
|
|
|
|
|
|
|
|
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-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
2023-01-05 11:41:14 +01:00
|
|
|
owner: "owner",
|
|
|
|
repo: "reponame",
|
|
|
|
head: "bp-target-28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc",
|
|
|
|
base: "target",
|
|
|
|
title: "[target] PR Title",
|
|
|
|
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/2368"),
|
2023-06-22 17:44:14 +02:00
|
|
|
reviewers: ["gh-user", "that-s-a-user"],
|
|
|
|
assignees: [],
|
2023-01-05 11:41:14 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("closed and not merged pull request", async () => {
|
|
|
|
spyGetInput({
|
|
|
|
"target-branch": "target",
|
|
|
|
"pull-request": "https://github.com/owner/reponame/pull/6666"
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(async () => await runner.execute()).rejects.toThrow("Provided pull request is closed and not merged!");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("open pull request", async () => {
|
|
|
|
spyGetInput({
|
|
|
|
"target-branch": "target",
|
|
|
|
"pull-request": "https://github.com/owner/reponame/pull/4444"
|
|
|
|
});
|
|
|
|
|
|
|
|
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-target-91748965051fae1330ad58d15cf694e103267c87");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/4444/head:pr/4444");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "91748965051fae1330ad58d15cf694e103267c87");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-91748965051fae1330ad58d15cf694e103267c87");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
2023-01-05 11:41:14 +01:00
|
|
|
owner: "owner",
|
|
|
|
repo: "reponame",
|
|
|
|
head: "bp-target-91748965051fae1330ad58d15cf694e103267c87",
|
|
|
|
base: "target",
|
|
|
|
title: "[target] PR Title",
|
|
|
|
body: expect.stringContaining("**Backport:** https://github.com/owner/reponame/pull/4444"),
|
2023-07-02 00:05:17 +02:00
|
|
|
reviewers: ["gh-user"],
|
2023-06-22 17:44:14 +02:00
|
|
|
assignees: [],
|
2023-01-05 11:41:14 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2023-06-20 22:29:52 +02:00
|
|
|
|
|
|
|
test("override backporting pr data", async () => {
|
|
|
|
spyGetInput({
|
|
|
|
"target-branch": "target",
|
|
|
|
"pull-request": "https://github.com/owner/reponame/pull/2368",
|
|
|
|
"title": "New Title",
|
|
|
|
"body": "New Body",
|
|
|
|
"body-prefix": "New Body Prefix - ",
|
|
|
|
"bp-branch-name": "bp_branch_name",
|
2023-06-22 17:44:14 +02:00
|
|
|
"reviewers": "user1, user2",
|
|
|
|
"assignees": "user3, user4",
|
2023-06-20 22:29:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
2023-06-20 22:29:52 +02:00
|
|
|
owner: "owner",
|
|
|
|
repo: "reponame",
|
|
|
|
head: "bp_branch_name",
|
|
|
|
base: "target",
|
|
|
|
title: "New Title",
|
|
|
|
body: "New Body Prefix - New Body",
|
2023-06-22 17:44:14 +02:00
|
|
|
reviewers: ["user1", "user2"],
|
|
|
|
assignees: ["user3", "user4"],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("set empty reviewers", async () => {
|
|
|
|
spyGetInput({
|
|
|
|
"target-branch": "target",
|
|
|
|
"pull-request": "https://github.com/owner/reponame/pull/2368",
|
|
|
|
"title": "New Title",
|
|
|
|
"body": "New Body",
|
|
|
|
"body-prefix": "New Body Prefix - ",
|
|
|
|
"bp-branch-name": "bp_branch_name",
|
|
|
|
"reviewers": "",
|
|
|
|
"assignees": "user3, user4",
|
|
|
|
"no-inherit-reviewers": "true",
|
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
2023-06-22 17:44:14 +02:00
|
|
|
owner: "owner",
|
|
|
|
repo: "reponame",
|
|
|
|
head: "bp_branch_name",
|
|
|
|
base: "target",
|
|
|
|
title: "New Title",
|
|
|
|
body: "New Body Prefix - New Body",
|
|
|
|
reviewers: [],
|
|
|
|
assignees: ["user3", "user4"],
|
2023-06-20 22:29:52 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2023-01-05 11:41:14 +01:00
|
|
|
});
|