feat(gh75): extract target branched from pr labels (#112)

This commit is contained in:
Andrea Lamparelli 2024-03-30 19:19:17 +01:00 committed by GitHub
parent b2e2e271b9
commit 53cc505f17
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 523 additions and 83 deletions

View file

@ -489,7 +489,7 @@ describe("cli args parser", () => {
"https://localhost/whatever/pulls/1"
]);
expect(() => parser.parse()).toThrowError("Missing option: pull request and target branches must be provided");
expect(() => parser.parse()).toThrowError("Missing option: target branch(es) or target regular expression must be provided");
});
test("invalid execution with missing mandatory target branch", () => {
@ -498,15 +498,15 @@ describe("cli args parser", () => {
"https://localhost/whatever/pulls/1"
]);
expect(() => parser.parse()).toThrowError("Missing option: pull request and target branches must be provided");
expect(() => parser.parse()).toThrowError("Missing option: target branch(es) or target regular expression must be provided");
});
test("invalid execution with missin mandatory pull request", () => {
test("invalid execution with missing mandatory pull request", () => {
addProcessArgs([
"-tb",
"target",
]);
expect(() => parser.parse()).toThrowError("Missing option: pull request and target branches must be provided");
expect(() => parser.parse()).toThrowError("Missing option: pull request must be provided");
});
});

View file

@ -296,7 +296,7 @@ describe("gha args parser", () => {
"pull-request": "https://localhost/whatever/pulls/1"
});
expect(() => parser.parse()).toThrowError("Missing option: pull request and target branches must be provided");
expect(() => parser.parse()).toThrowError("Missing option: target branch(es) or target regular expression must be provided");
});
test("invalid execution with missing mandatory target branch", () => {
@ -304,7 +304,7 @@ describe("gha args parser", () => {
"pull-request": "https://localhost/whatever/pulls/1"
});
expect(() => parser.parse()).toThrowError("Missing option: pull request and target branches must be provided");
expect(() => parser.parse()).toThrowError("Missing option: target branch(es) or target regular expression must be provided");
});
test("invalid execution with missin mandatory pull request", () => {
@ -312,6 +312,6 @@ describe("gha args parser", () => {
"target-branch": "target,old",
});
expect(() => parser.parse()).toThrowError("Missing option: pull request and target branches must be provided");
expect(() => parser.parse()).toThrowError("Missing option: pull request must be provided");
});
});

View file

@ -353,7 +353,78 @@ describe("github pull request config parser", () => {
dryRun: false,
auth: "",
pullRequest: multipleCommitsPRUrl,
targetBranch: "v1, v2, v3",
targetBranch: "v4, v5, v6",
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-v4-0404fb9-11da4e3",
base: "v4",
title: "[v4] 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-v5-0404fb9-11da4e3",
base: "v5",
title: "[v5] 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-v6-0404fb9-11da4e3",
base: "v6",
title: "[v6] 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: [],
},
])
);
});
test("multiple extracted branches and multiple commits", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: multipleCommitsPRUrl,
targetBranchPattern: "^backport (?<target>([^ ]+))$",
gitUser: "GitHub",
gitEmail: "noreply@github.com",
reviewers: [],

View file

@ -112,7 +112,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -281,7 +281,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -395,7 +395,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -467,7 +467,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -511,7 +511,7 @@ describe("github pull request config parser", () => {
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: ["custom-label", "original-label"], // also include the one inherited
labels: ["custom-label", "backport prod"], // also include the one inherited
inheritLabels: true,
};
@ -541,7 +541,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -566,7 +566,7 @@ describe("github pull request config parser", () => {
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: ["custom-label", "original-label"],
labels: ["custom-label", "backport prod"],
comments: [],
});
});
@ -604,7 +604,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -666,7 +666,7 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",
@ -691,7 +691,7 @@ describe("github pull request config parser", () => {
body: "New Body Prefix -New Body",
reviewers: ["user1", "user2"],
assignees: ["user3", "user4"],
labels: ["cherry-pick :cherries:", "original-label"],
labels: ["cherry-pick :cherries:", "backport prod"],
comments: [],
});
});
@ -736,7 +736,11 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: [],
labels: [
"backport v1",
"backport v2",
"backport v3",
],
targetRepo: {
owner: "owner",
project: "reponame",
@ -810,7 +814,104 @@ describe("github pull request config parser", () => {
body: "Please review and merge",
reviewers: ["requested-gh-user", "gh-user"],
assignees: [],
labels: ["original-label"],
labels: ["backport prod"],
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.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: [],
assignees: ["user3", "user4"],
labels: [],
comments: ["First comment", "Second comment"],
});
});
test("no extracted target branches from pr labels due to wrong group name", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranchPattern: "^backport (?<wrong>([^ ]+))$",
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: ["First comment", "Second comment"],
};
await expect(() => configParser.parseAndValidate(args)).rejects.toThrow("Unable to extract target branches with regular expression");
});
test("extract target branches from pr labels", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranchPattern: "^backport (?<target>([^ ]+))$",
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: ["First comment", "Second comment"],
};
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.git).toEqual({
user: "Me",
email: "me@email.com"
});
expect(configs.auth).toEqual("");
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: [],
labels: ["backport prod"],
targetRepo: {
owner: "owner",
project: "reponame",

View file

@ -116,7 +116,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -290,7 +290,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -361,7 +361,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -432,7 +432,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -475,7 +475,7 @@ describe("gitlab merge request config parser", () => {
reviewers: [],
assignees: ["user3", "user4"],
inheritReviewers: false,
labels: ["custom-label", "gitlab-original-label"], // also include the one inherited
labels: ["custom-label", "backport-prod"], // also include the one inherited
inheritLabels: true,
};
@ -505,7 +505,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -529,7 +529,7 @@ describe("gitlab merge request config parser", () => {
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: ["custom-label", "gitlab-original-label"],
labels: ["custom-label", "backport-prod"],
comments: [],
});
});
@ -566,7 +566,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -627,7 +627,7 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",
@ -651,7 +651,7 @@ describe("gitlab merge request config parser", () => {
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: ["cherry-pick :cherries:", "gitlab-original-label"],
labels: ["cherry-pick :cherries:", "backport-prod"],
comments: [],
});
});
@ -770,7 +770,81 @@ describe("gitlab merge request config parser", () => {
body: "This is the body",
reviewers: ["superuser1", "superuser2"],
assignees: ["superuser"],
labels: ["gitlab-original-label"],
labels: ["backport-prod"],
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.backportPullRequests.length).toEqual(1);
expect(configs.backportPullRequests[0]).toEqual({
owner: "superuser",
repo: "backporting-example",
head: "bp-prod-ebb1eca",
base: "prod",
title: "New Title",
body: "New Body Prefix -New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: [],
comments: ["First comment", "Second comment"],
});
});
test("extract target branches from pr labels", async () => {
const args: Args = {
dryRun: false,
auth: "",
pullRequest: mergedPRUrl,
targetBranchPattern: "^backport-(?<target>([^ ]+))$",
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: ["First comment", "Second comment"],
};
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.git).toEqual({
user: "Me",
email: "me@email.com"
});
expect(configs.auth).toEqual("");
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"],
labels: ["backport-prod"],
targetRepo: {
owner: "superuser",
project: "backporting-example",

View file

@ -471,7 +471,7 @@ describe("cli runner", () => {
"-pr",
"https://github.com/owner/reponame/pull/2368",
"--labels",
"cherry-pick :cherries:, original-label",
"cherry-pick :cherries:, backport prod",
"--inherit-labels",
]);
@ -507,7 +507,7 @@ describe("cli runner", () => {
body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: ["cherry-pick :cherries:", "original-label"],
labels: ["cherry-pick :cherries:", "backport prod"],
comments: [],
}
);
@ -601,7 +601,7 @@ describe("cli runner", () => {
body: "New Body Prefix - New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: ["cli github cherry pick :cherries:", "original-label"],
labels: ["cli github cherry pick :cherries:", "backport prod"],
comments: [],
}
);
@ -1163,4 +1163,23 @@ describe("cli runner", () => {
// Not interested in all subsequent calls, already tested in other test cases
});
test("extract target branch from label", async () => {
addProcessArgs([
"--target-branch-pattern",
"^backport (?<target>([^ ]+))$",
"-pr",
"https://github.com/owner/reponame/pull/2368"
]);
await runner.execute();
const cwd = process.cwd() + "/bp";
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "prod");
});
});

View file

@ -408,7 +408,7 @@ describe("cli runner", () => {
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"],
labels: ["cherry-pick :cherries:", "another-label", "backport-prod"],
comments: [],
}
);
@ -502,7 +502,7 @@ describe("cli runner", () => {
body: expect.stringContaining("**This is a backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
reviewers: [],
assignees: ["user3", "user4"],
labels: ["cli gitlab cherry pick :cherries:", "gitlab-original-label"],
labels: ["cli gitlab cherry pick :cherries:", "backport-prod"],
comments: [],
}
);

View file

@ -331,7 +331,7 @@ describe("gha runner", () => {
body: "**Backport:** https://github.com/owner/reponame/pull/2368\r\n\r\nPlease review and merge",
reviewers: ["gh-user", "that-s-a-user"],
assignees: [],
labels: ["cherry-pick :cherries:", "another-label", "original-label"],
labels: ["cherry-pick :cherries:", "another-label", "backport prod"],
comments: [],
}
);
@ -422,7 +422,7 @@ describe("gha runner", () => {
body: "New Body Prefix - New Body",
reviewers: [],
assignees: ["user3", "user4"],
labels: ["gha github cherry pick :cherries:", "original-label"],
labels: ["gha github cherry pick :cherries:", "backport prod"],
comments: [],
}
);

View file

@ -336,7 +336,7 @@ describe("gha runner", () => {
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"],
labels: ["cherry-pick :cherries:", "another-label", "backport-prod"],
comments: [],
}
);
@ -424,7 +424,7 @@ describe("gha runner", () => {
body: expect.stringContaining("**This is a backport:** https://my.gitlab.host.com/superuser/backporting-example/-/merge_requests/1"),
reviewers: [],
assignees: ["user3", "user4"],
labels: ["gha gitlab cherry pick :cherries:", "gitlab-original-label"],
labels: ["gha gitlab cherry pick :cherries:", "backport-prod"],
comments: [],
}
);

View file

@ -96,8 +96,8 @@ export const MERGED_PR_FIXTURE = {
{
"id": 4901021057,
"node_id": "LA_kwDOImgs2354988AAAABJB-lgQ",
"url": "https://api.github.com/repos/owner/reponame/labels/original-label",
"name": "original-label",
"url": "https://api.github.com/repos/owner/reponame/labels/backport-prod",
"name": "backport prod",
"color": "AB975B",
"default": false,
"description": ""
@ -1431,7 +1431,33 @@ export const MULT_COMMITS_PR_FIXTURE = {
],
"labels": [
{
"id": 4901021057,
"node_id": "LA_kwDOImgs2354988AAAABJB-lgQ",
"url": "https://api.github.com/repos/owner/reponame/labels/backport-v1",
"name": "backport v1",
"color": "AB975B",
"default": false,
"description": ""
},
{
"id": 4901021057,
"node_id": "LA_kwDOImgs2354988AAAABJB-lgQ",
"url": "https://api.github.com/repos/owner/reponame/labels/backport-v2",
"name": "backport v2",
"color": "AB975B",
"default": false,
"description": ""
},
{
"id": 4901021057,
"node_id": "LA_kwDOImgs2354988AAAABJB-lgQ",
"url": "https://api.github.com/repos/owner/reponame/labels/backport-v3",
"name": "backport v3",
"color": "AB975B",
"default": false,
"description": ""
}
],
"milestone": null,
"draft": false,

View file

@ -401,7 +401,7 @@ export const MERGED_SQUASHED_MR = {
"source_project_id":76316,
"target_project_id":76316,
"labels":[
"gitlab-original-label"
"backport-prod"
],
"draft":false,
"work_in_progress":false,