feat(issue-77): handle multiple target branches (#78)

fix: https://github.com/kiegroup/git-backporting/issues/77

This enhancement allow users to backport the same change to multiple
branches with one single tool invocation
This commit is contained in:
Andrea Lamparelli 2023-08-03 21:57:11 +02:00 committed by GitHub
parent c19a56a9ad
commit 5fc72e127b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 1774 additions and 234 deletions

View file

@ -0,0 +1,422 @@
import { Args } from "@bp/service/args/args.types";
import { Configs } from "@bp/service/configs/configs.types";
import PullRequestConfigsParser from "@bp/service/configs/pullrequest/pr-configs-parser";
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 { resetProcessArgs } from "../../../support/utils";
import { MERGED_PR_FIXTURE, REPO, TARGET_OWNER, MULT_COMMITS_PR_FIXTURE } from "../../../support/mock/github-data";
import GitHubMapper from "@bp/service/git/github/github-mapper";
import GitHubClient from "@bp/service/git/github/github-client";
jest.spyOn(GitHubMapper.prototype, "mapPullRequest");
jest.spyOn(GitHubClient.prototype, "getPullRequest");
describe("github pull request config parser", () => {
const mergedPRUrl = `https://github.com/${TARGET_OWNER}/${REPO}/pull/${MERGED_PR_FIXTURE.number}`;
const multipleCommitsPRUrl = `https://github.com/${TARGET_OWNER}/${REPO}/pull/${MULT_COMMITS_PR_FIXTURE.number}`;
let configParser: PullRequestConfigsParser;
beforeAll(() => {
GitClientFactory.reset();
GitClientFactory.getOrCreate(GitClientType.GITHUB, "whatever", "http://localhost/api/v3");
});
beforeEach(() => {
// reset process.env variables
resetProcessArgs();
// mock octokit
mockGitHubClient("http://localhost/api/v3");
// create a fresh new instance every time
configParser = new PullRequestConfigsParser();
});
test("multiple backports", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitHubClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.getPullRequest).toBeCalledWith("owner", "reponame", 2368, true);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "owner",
repo: "reponame",
head: "bp-v1-28f63db",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "bp-v2-28f63db",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "bp-v3-28f63db",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
])
);
});
test("multiple backports ignore duplicates", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitHubClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.getPullRequest).toBeCalledWith("owner", "reponame", 2368, true);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "owner",
repo: "reponame",
head: "bp-v1-28f63db",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "bp-v2-28f63db",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "bp-v3-28f63db",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
])
);
});
test("multiple backports with custom branch name", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
bpBranchName: "custom-branch",
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitHubClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.getPullRequest).toBeCalledWith("owner", "reponame", 2368, true);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "owner",
repo: "reponame",
head: "custom-branch-v1",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "custom-branch-v2",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "custom-branch-v3",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
])
);
});
test("multiple backports with multiple custom branch names", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
bpBranchName: "custom-branch1, custom-branch2, custom-branch3",
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitHubClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.getPullRequest).toBeCalledWith("owner", "reponame", 2368, true);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "owner",
repo: "reponame",
head: "custom-branch1",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "custom-branch2",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "custom-branch3",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
])
);
});
test("multiple backports with incorrect number of bp branch names", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
bpBranchName: "custom-branch1, custom-branch2",
};
await expect(() => configParser.parseAndValidate(args)).rejects.toThrow("The number of backport branch names, if provided, must match the number of target branches or just one, provided 2 branch names instead");
});
test("multiple backports and multiple commits", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: multipleCommitsPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "GitHub",
gitEmail: "noreply@github.com",
reviewers: [],
assignees: [],
inheritReviewers: true,
squash: false,
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitHubClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.getPullRequest).toBeCalledWith("owner", "reponame", 8632, false);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), ["0404fb922ab75c3a8aecad5c97d9af388df04695", "11da4e38aa3e577ffde6d546f1c52e53b04d3151"]);
expect(configs.dryRun).toEqual(false);
expect(configs.git).toEqual({
user: "GitHub",
email: "noreply@github.com"
});
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "owner",
repo: "reponame",
head: "bp-v1-0404fb9-11da4e3",
base: "v1",
title: "[v1] PR Title",
body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "bp-v2-0404fb9-11da4e3",
base: "v2",
title: "[v2] PR Title",
body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: [],
comments: [],
},
{
owner: "owner",
repo: "reponame",
head: "bp-v3-0404fb9-11da4e3",
base: "v3",
title: "[v3] PR Title",
body: "**Backport:** https://github.com/owner/reponame/pull/8632\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: [],
comments: [],
},
])
);
});
});

View file

@ -5,7 +5,7 @@ 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, multipleCommitsPullRequestFixture } from "../../../support/mock/github-data";
import { MERGED_PR_FIXTURE, OPEN_PR_FIXTURE, NOT_MERGED_PR_FIXTURE, REPO, TARGET_OWNER, MULT_COMMITS_PR_FIXTURE } from "../../../support/mock/github-data";
import CLIArgsParser from "@bp/service/args/cli/cli-args-parser";
import GitHubMapper from "@bp/service/git/github/github-mapper";
import GitHubClient from "@bp/service/git/github/github-client";
@ -13,14 +13,14 @@ import GitHubClient from "@bp/service/git/github/github-client";
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}`,
"pullRequest": `https://github.com/${TARGET_OWNER}/${REPO}/pull/${MERGED_PR_FIXTURE.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}`,
"pullRequest": `https://github.com/${TARGET_OWNER}/${REPO}/pull/${MERGED_PR_FIXTURE.number}`,
"targetBranch": "prod",
"gitUser": "Me",
"gitEmail": "me@email.com",
@ -39,10 +39,10 @@ jest.spyOn(GitHubClient.prototype, "getPullRequest");
describe("github pull request config parser", () => {
const mergedPRUrl = `https://github.com/${targetOwner}/${repo}/pull/${mergedPullRequestFixture.number}`;
const openPRUrl = `https://github.com/${targetOwner}/${repo}/pull/${openPullRequestFixture.number}`;
const notMergedPRUrl = `https://github.com/${targetOwner}/${repo}/pull/${notMergedPullRequestFixture.number}`;
const multipleCommitsPRUrl = `https://github.com/${targetOwner}/${repo}/pull/${multipleCommitsPullRequestFixture.number}`;
const mergedPRUrl = `https://github.com/${TARGET_OWNER}/${REPO}/pull/${MERGED_PR_FIXTURE.number}`;
const openPRUrl = `https://github.com/${TARGET_OWNER}/${REPO}/pull/${OPEN_PR_FIXTURE.number}`;
const notMergedPRUrl = `https://github.com/${TARGET_OWNER}/${REPO}/pull/${NOT_MERGED_PR_FIXTURE.number}`;
const multipleCommitsPRUrl = `https://github.com/${TARGET_OWNER}/${REPO}/pull/${MULT_COMMITS_PR_FIXTURE.number}`;
let argsParser: CLIArgsParser;
let configParser: PullRequestConfigsParser;
@ -100,7 +100,6 @@ describe("github pull request config parser", () => {
email: "noreply@github.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
@ -128,7 +127,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
@ -160,7 +160,6 @@ describe("github pull request config parser", () => {
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: "GitHub",
@ -190,7 +189,6 @@ describe("github pull request config parser", () => {
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.targetBranch).toEqual("prod");
expect(configs.git).toEqual({
user: "GitHub",
email: "noreply@github.com"
@ -271,7 +269,6 @@ describe("github pull request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
@ -300,7 +297,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "custom-branch",
@ -314,6 +312,48 @@ describe("github pull request config parser", () => {
});
});
test("override backport with empty bp branch name", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "prod",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: [],
inheritReviewers: true,
bpBranchName: " "
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitHubClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitHubClient.prototype.getPullRequest).toBeCalledWith("owner", "reponame", 2368, true);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitHubMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
base: "prod",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: [],
comments: [],
});
});
test("override backport pr reviewers and assignees", async () => {
const args: Args = {
dryRun: false,
@ -343,7 +383,6 @@ describe("github pull request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
@ -372,7 +411,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
@ -415,7 +455,6 @@ describe("github pull request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
@ -444,7 +483,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
@ -489,7 +529,6 @@ describe("github pull request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
@ -518,7 +557,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
@ -552,7 +592,6 @@ describe("github pull request config parser", () => {
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,
@ -580,7 +619,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
@ -614,7 +654,6 @@ describe("github pull request config parser", () => {
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,
@ -643,7 +682,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",
@ -684,7 +724,6 @@ describe("github pull request config parser", () => {
email: "noreply@github.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 8632,
@ -712,7 +751,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["0404fb922ab75c3a8aecad5c97d9af388df04695", "11da4e38aa3e577ffde6d546f1c52e53b04d3151"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-0404fb9-11da4e3",
@ -758,7 +798,6 @@ describe("github pull request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 2368,
@ -787,7 +826,8 @@ describe("github pull request config parser", () => {
nCommits: 2,
commits: ["28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc"],
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "owner",
repo: "reponame",
head: "bp-prod-28f63db",

View file

@ -0,0 +1,349 @@
import { Args } from "@bp/service/args/args.types";
import { Configs } from "@bp/service/configs/configs.types";
import PullRequestConfigsParser from "@bp/service/configs/pullrequest/pr-configs-parser";
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 { MERGED_SQUASHED_MR } from "../../../support/mock/gitlab-data";
import GitLabClient from "@bp/service/git/gitlab/gitlab-client";
import GitLabMapper from "@bp/service/git/gitlab/gitlab-mapper";
jest.spyOn(GitLabMapper.prototype, "mapPullRequest");
jest.spyOn(GitLabClient.prototype, "getPullRequest");
jest.mock("axios", () => {
return {
create: jest.fn(() => ({
get: getAxiosMocked,
})),
};
});
describe("gitlab merge request config parser", () => {
const mergedPRUrl = `https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/${MERGED_SQUASHED_MR.iid}`;
let configParser: PullRequestConfigsParser;
beforeAll(() => {
GitClientFactory.reset();
GitClientFactory.getOrCreate(GitClientType.GITLAB, "whatever", "my.gitlab.host.com");
});
beforeEach(() => {
configParser = new PullRequestConfigsParser();
});
test("multiple backports", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "superuser",
repo: "backporting-example",
head: "bp-v1-ebb1eca",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "bp-v2-ebb1eca",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "bp-v3-ebb1eca",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
}
])
);
});
test("multiple backports ignore duplicates", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3, v1",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "superuser",
repo: "backporting-example",
head: "bp-v1-ebb1eca",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "bp-v2-ebb1eca",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "bp-v3-ebb1eca",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
}
])
);
});
test("multiple backports with custom branch name", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
bpBranchName: "custom-branch"
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "superuser",
repo: "backporting-example",
head: "custom-branch-v1",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "custom-branch-v2",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "custom-branch-v3",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
}
])
);
});
test("multiple backports with multiple custom branch names", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
bpBranchName: "custom1, custom2, custom3"
};
const configs: Configs = await configParser.parseAndValidate(args);
expect(GitLabClient.prototype.getPullRequest).toBeCalledTimes(1);
expect(GitLabClient.prototype.getPullRequest).toBeCalledWith("superuser", "backporting-example", 1, true);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledTimes(1);
expect(GitLabMapper.prototype.mapPullRequest).toBeCalledWith(expect.anything(), []);
expect(configs.dryRun).toEqual(false);
expect(configs.auth).toEqual("");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.backportPullRequests.length).toEqual(3);
expect(configs.backportPullRequests).toEqual(
expect.arrayContaining([
{
owner: "superuser",
repo: "backporting-example",
head: "custom1",
base: "v1",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "custom2",
base: "v2",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
},
{
owner: "superuser",
repo: "backporting-example",
head: "custom3",
base: "v3",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: [],
}
])
);
});
test("multiple backports with incorrect number of bp branch names", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranch: "v1, v2, v3",
gitUser: "Me",
gitEmail: "me@email.com",
title: "New Title",
body: "New Body",
bodyPrefix: "New Body Prefix -",
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: [],
inheritLabels: false,
comments: [],
bpBranchName: "custom-branch1, custom-branch2, custom-branch2, custom-branch3, custom-branch4",
};
await expect(() => configParser.parseAndValidate(args)).rejects.toThrow("The number of backport branch names, if provided, must match the number of target branches or just one, provided 4 branch names instead");
});
});

View file

@ -100,7 +100,6 @@ describe("gitlab merge request config parser", () => {
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,
@ -128,7 +127,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -166,7 +166,6 @@ describe("gitlab merge request config parser", () => {
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",
@ -196,7 +195,6 @@ describe("gitlab merge request config parser", () => {
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.targetBranch).toEqual("prod");
expect(configs.git).toEqual({
user: "Gitlab",
email: "noreply@gitlab.com"
@ -276,7 +274,6 @@ describe("gitlab merge request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
@ -304,7 +301,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -347,7 +345,6 @@ describe("gitlab merge request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
@ -375,7 +372,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -418,7 +416,6 @@ describe("gitlab merge request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
@ -446,7 +443,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -491,7 +489,6 @@ describe("gitlab merge request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
@ -519,7 +516,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -552,7 +550,6 @@ describe("gitlab merge request config parser", () => {
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,
@ -580,7 +577,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -613,7 +611,6 @@ describe("gitlab merge request config parser", () => {
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,
@ -641,7 +638,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
@ -678,7 +676,6 @@ describe("gitlab merge request config parser", () => {
expect(configs.dryRun).toEqual(true);
expect(configs.auth).toEqual("whatever");
expect(configs.targetBranch).toEqual("prod");
expect(configs.git).toEqual({
user: "Gitlab",
email: "noreply@gitlab.com"
@ -710,7 +707,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 2,
commits: ["e4dd336a4a20f394df6665994df382fb1d193a11", "974519f65c9e0ed65277cd71026657a09fca05e7"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-e4dd336-974519f",
@ -756,7 +754,6 @@ describe("gitlab merge request config parser", () => {
email: "me@email.com"
});
expect(configs.auth).toEqual("");
expect(configs.targetBranch).toEqual("prod");
expect(configs.folder).toEqual(process.cwd() + "/bp");
expect(configs.originalPullRequest).toEqual({
number: 1,
@ -784,7 +781,8 @@ describe("gitlab merge request config parser", () => {
nCommits: 1,
commits: ["ebb1eca696c42fd067658bd9b5267709f78ef38e"]
});
expect(configs.backportPullRequest).toEqual({
expect(configs.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",