2023-07-02 00:05:17 +02:00
|
|
|
import ArgsParser from "@bp/service/args/args-parser";
|
|
|
|
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";
|
2023-07-05 22:11:23 +02:00
|
|
|
import { addProcessArgs, createTestFile, removeTestFile, resetProcessArgs } from "../../support/utils";
|
2023-07-02 00:05:17 +02:00
|
|
|
import { getAxiosMocked } from "../../support/mock/git-client-mock-support";
|
2023-07-05 22:11:23 +02:00
|
|
|
import { MERGED_SQUASHED_MR } from "../../support/mock/gitlab-data";
|
2023-07-10 15:18:51 +02:00
|
|
|
import GitClientFactory from "@bp/service/git/git-client-factory";
|
|
|
|
import { GitClientType } from "@bp/service/git/git.types";
|
2023-07-05 22:11:23 +02:00
|
|
|
|
|
|
|
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,
|
2023-07-10 08:49:11 +02:00
|
|
|
"labels": ["cli gitlab cherry pick :cherries:"],
|
|
|
|
"inheritLabels": true,
|
2023-07-05 22:11:23 +02:00
|
|
|
};
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
jest.mock("axios", () => {
|
|
|
|
return {
|
|
|
|
create: () => ({
|
|
|
|
get: getAxiosMocked,
|
|
|
|
post: () => ({
|
|
|
|
data: {
|
|
|
|
iid: 1, // FIXME: I am not testing this atm
|
|
|
|
}
|
|
|
|
}),
|
2023-07-11 16:15:52 -04:00
|
|
|
put: async () => undefined,
|
2023-07-02 00:05:17 +02:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock("@bp/service/git/git-cli");
|
|
|
|
jest.spyOn(GitLabClient.prototype, "createPullRequest");
|
2023-07-10 15:18:51 +02:00
|
|
|
jest.spyOn(GitClientFactory, "getOrCreate");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
let parser: ArgsParser;
|
|
|
|
let runner: Runner;
|
|
|
|
|
2023-07-05 22:11:23 +02:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
beforeEach(() => {
|
2023-07-11 22:43:22 +02:00
|
|
|
// reset process.env variables
|
|
|
|
resetProcessArgs();
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
// create CLI arguments parser
|
|
|
|
parser = new CLIArgsParser();
|
|
|
|
|
|
|
|
// create runner
|
|
|
|
runner = new Runner(parser);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("cli runner", () => {
|
|
|
|
test("with dry run", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-d",
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2"
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-9e15674");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "merge-requests/2/head:pr/2");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "9e15674ebd48e05c6e428a1fa31dbb60a778d644", undefined, undefined);
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("dry run with relative folder", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-d",
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2",
|
|
|
|
"-f",
|
|
|
|
"folder"
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/folder";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-9e15674");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "merge-requests/2/head:pr/2");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "9e15674ebd48e05c6e428a1fa31dbb60a778d644", undefined, undefined);
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.addRemote).toBeCalledTimes(0);
|
|
|
|
expect(GitCLIService.prototype.addRemote).toBeCalledTimes(0);
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("without dry run", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2"
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-9e15674");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "merge-requests/2/head:pr/2");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "9e15674ebd48e05c6e428a1fa31dbb60a778d644", undefined, undefined);
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-9e15674");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
2023-07-11 22:46:21 +02:00
|
|
|
head: "bp-target-9e15674",
|
2023-07-02 00:05:17 +02:00
|
|
|
base: "target",
|
|
|
|
title: "[target] Update test.txt opened",
|
|
|
|
body: expect.stringContaining("**Backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2"),
|
|
|
|
reviewers: ["superuser"],
|
|
|
|
assignees: [],
|
2023-07-10 08:49:11 +02:00
|
|
|
labels: [],
|
2023-07-02 00:05:17 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("closed and not merged pull request", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/3"
|
|
|
|
]);
|
|
|
|
|
2023-07-20 10:05:07 +02:00
|
|
|
await expect(() => runner.execute()).rejects.toThrow("Provided pull request is closed and not merged");
|
2023-07-02 00:05:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("merged pull request", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-ebb1eca");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
// 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);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "ebb1eca696c42fd067658bd9b5267709f78ef38e", undefined, undefined);
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-ebb1eca");
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
2023-07-11 22:46:21 +02:00
|
|
|
head: "bp-target-ebb1eca",
|
2023-07-02 00:05:17 +02:00
|
|
|
base: "target",
|
|
|
|
title: "[target] Update test.txt",
|
|
|
|
body: expect.stringContaining("**Backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
|
|
|
|
reviewers: ["superuser"],
|
|
|
|
assignees: [],
|
2023-07-10 08:49:11 +02:00
|
|
|
labels: [],
|
2023-07-02 00:05:17 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test("override backporting pr data", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2",
|
|
|
|
"--title",
|
|
|
|
"New Title",
|
|
|
|
"--body",
|
|
|
|
"New Body",
|
|
|
|
"--body-prefix",
|
|
|
|
"New Body Prefix - ",
|
|
|
|
"--bp-branch-name",
|
|
|
|
"bp_branch_name",
|
|
|
|
"--reviewers",
|
|
|
|
"user1,user2",
|
|
|
|
"--assignees",
|
|
|
|
"user3,user4"
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.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, "merge-requests/2/head:pr/2");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "9e15674ebd48e05c6e428a1fa31dbb60a778d644", undefined, undefined);
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp_branch_name");
|
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
|
|
|
head: "bp_branch_name",
|
|
|
|
base: "target",
|
|
|
|
title: "New Title",
|
|
|
|
body: "New Body Prefix - New Body",
|
|
|
|
reviewers: ["user1", "user2"],
|
|
|
|
assignees: ["user3", "user4"],
|
2023-07-10 08:49:11 +02:00
|
|
|
labels: [],
|
2023-07-02 00:05:17 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("set empty reviewers", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2",
|
|
|
|
"--title",
|
|
|
|
"New Title",
|
|
|
|
"--body",
|
|
|
|
"New Body",
|
|
|
|
"--body-prefix",
|
|
|
|
"New Body Prefix - ",
|
|
|
|
"--bp-branch-name",
|
|
|
|
"bp_branch_name",
|
|
|
|
"--no-inherit-reviewers",
|
|
|
|
"--assignees",
|
|
|
|
"user3,user4",
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-02 00:05:17 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.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, "merge-requests/2/head:pr/2");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(1);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "9e15674ebd48e05c6e428a1fa31dbb60a778d644", undefined, undefined);
|
2023-07-02 00:05:17 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp_branch_name");
|
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
|
|
|
head: "bp_branch_name",
|
|
|
|
base: "target",
|
|
|
|
title: "New Title",
|
|
|
|
body: "New Body Prefix - New Body",
|
|
|
|
reviewers: [],
|
|
|
|
assignees: ["user3", "user4"],
|
2023-07-10 08:49:11 +02:00
|
|
|
labels: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("set custom labels with inheritance", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1",
|
|
|
|
"--labels",
|
|
|
|
"cherry-pick :cherries:, another-label",
|
|
|
|
"--inherit-labels",
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-10 08:49:11 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-ebb1eca");
|
2023-07-10 08:49:11 +02:00
|
|
|
|
|
|
|
// 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);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "ebb1eca696c42fd067658bd9b5267709f78ef38e", undefined, undefined);
|
2023-07-10 08:49:11 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-ebb1eca");
|
2023-07-10 08:49:11 +02:00
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
2023-07-11 22:46:21 +02:00
|
|
|
head: "bp-target-ebb1eca",
|
2023-07-10 08:49:11 +02:00
|
|
|
base: "target",
|
|
|
|
title: "[target] Update test.txt",
|
|
|
|
body: expect.stringContaining("**Backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
|
|
|
|
reviewers: ["superuser"],
|
|
|
|
assignees: [],
|
|
|
|
labels: ["cherry-pick :cherries:", "another-label", "gitlab-original-label"],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("set custom labels without inheritance", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1",
|
|
|
|
"--labels",
|
|
|
|
"cherry-pick :cherries:, another-label",
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-10 08:49:11 +02:00
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-ebb1eca");
|
2023-07-10 08:49:11 +02:00
|
|
|
|
|
|
|
// 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);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "ebb1eca696c42fd067658bd9b5267709f78ef38e", undefined, undefined);
|
2023-07-10 08:49:11 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-ebb1eca");
|
2023-07-10 08:49:11 +02:00
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
2023-07-11 22:46:21 +02:00
|
|
|
head: "bp-target-ebb1eca",
|
2023-07-10 08:49:11 +02:00
|
|
|
base: "target",
|
|
|
|
title: "[target] Update test.txt",
|
|
|
|
body: expect.stringContaining("**Backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
|
|
|
|
reviewers: ["superuser"],
|
|
|
|
assignees: [],
|
|
|
|
labels: ["cherry-pick :cherries:", "another-label"],
|
2023-07-02 00:05:17 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2023-07-05 22:11:23 +02:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
2023-07-10 15:18:51 +02:00
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, "my-token", "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
2023-07-05 22:11:23 +02:00
|
|
|
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);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-prod-ebb1eca");
|
2023-07-05 22:11:23 +02:00
|
|
|
|
|
|
|
// 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);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "ebb1eca696c42fd067658bd9b5267709f78ef38e", undefined, undefined);
|
2023-07-05 22:11:23 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-prod-ebb1eca");
|
2023-07-05 22:11:23 +02:00
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
2023-07-11 22:46:21 +02:00
|
|
|
head: "bp-prod-ebb1eca",
|
2023-07-05 22:11:23 +02:00
|
|
|
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"],
|
2023-07-10 08:49:11 +02:00
|
|
|
labels: ["cli gitlab cherry pick :cherries:", "gitlab-original-label"],
|
2023-07-05 22:11:23 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2023-07-11 11:22:01 +02:00
|
|
|
|
|
|
|
test("multiple commits without squash", async () => {
|
|
|
|
addProcessArgs([
|
|
|
|
"-tb",
|
|
|
|
"target",
|
|
|
|
"-pr",
|
|
|
|
"https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2",
|
|
|
|
"--no-squash",
|
|
|
|
]);
|
|
|
|
|
|
|
|
await runner.execute();
|
|
|
|
|
|
|
|
const cwd = process.cwd() + "/bp";
|
|
|
|
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
|
|
|
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITLAB, undefined, "https://my.gitlab.host.com/api/v4");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.clone).toBeCalledWith("https://my.gitlab.host.com/superuser/backporting-example.git", cwd, "target");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "bp-target-e4dd336-974519f");
|
2023-07-11 11:22:01 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledTimes(1);
|
|
|
|
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "merge-requests/2/head:pr/2");
|
|
|
|
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2);
|
2023-07-12 13:50:59 +02:00
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "e4dd336a4a20f394df6665994df382fb1d193a11", undefined, undefined);
|
|
|
|
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "974519f65c9e0ed65277cd71026657a09fca05e7", undefined, undefined);
|
2023-07-11 11:22:01 +02:00
|
|
|
|
|
|
|
expect(GitCLIService.prototype.push).toBeCalledTimes(1);
|
2023-07-11 22:46:21 +02:00
|
|
|
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-e4dd336-974519f");
|
2023-07-11 11:22:01 +02:00
|
|
|
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(1);
|
|
|
|
expect(GitLabClient.prototype.createPullRequest).toBeCalledWith({
|
|
|
|
owner: "superuser",
|
|
|
|
repo: "backporting-example",
|
2023-07-11 22:46:21 +02:00
|
|
|
head: "bp-target-e4dd336-974519f",
|
2023-07-11 11:22:01 +02:00
|
|
|
base: "target",
|
|
|
|
title: "[target] Update test.txt opened",
|
|
|
|
body: expect.stringContaining("**Backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/2"),
|
|
|
|
reviewers: ["superuser"],
|
|
|
|
assignees: [],
|
|
|
|
labels: [],
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2023-07-02 00:05:17 +02:00
|
|
|
});
|