mirror of
https://github.com/kiegroup/git-backporting.git
synced 2025-06-28 05:33:47 +00:00
feat: implement error notification as pr comment (#124)
* feat: implement error notification as pr comment * Update action.yml Co-authored-by: Earl Warren <109468362+earl-warren@users.noreply.github.com> * feat: implement gitlab client and surround with try catch * docs: add error notification enablment in the doc * feat: disable comment if dry-run * feat: update the default comment on error --------- Co-authored-by: Earl Warren <109468362+earl-warren@users.noreply.github.com>
This commit is contained in:
parent
6042bcc40b
commit
2bb7f73112
28 changed files with 594 additions and 39 deletions
|
@ -30,6 +30,7 @@ const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT = {
|
|||
|
||||
jest.mock("@bp/service/git/git-cli");
|
||||
jest.spyOn(GitHubClient.prototype, "createPullRequest");
|
||||
jest.spyOn(GitHubClient.prototype, "createPullRequestComment");
|
||||
jest.spyOn(GitClientFactory, "getOrCreate");
|
||||
|
||||
let parser: ArgsParser;
|
||||
|
@ -94,6 +95,7 @@ describe("cli runner", () => {
|
|||
|
||||
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(0);
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("overriding author", async () => {
|
||||
|
@ -287,6 +289,7 @@ describe("cli runner", () => {
|
|||
}
|
||||
);
|
||||
expect(GitHubClient.prototype.createPullRequest).toReturnTimes(1);
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("closed and not merged pull request", async () => {
|
||||
|
@ -1156,6 +1159,7 @@ describe("cli runner", () => {
|
|||
comments: [],
|
||||
});
|
||||
expect(GitHubClient.prototype.createPullRequest).toThrowError();
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("auth using GITHUB_TOKEN takes precedence over GIT_TOKEN env variable", async () => {
|
||||
|
@ -1231,4 +1235,142 @@ describe("cli runner", () => {
|
|||
expect(GitCLIService.prototype.clone).toBeCalledTimes(1);
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "prod");
|
||||
});
|
||||
});
|
||||
|
||||
test("with multiple target branches, one failure and error notification enabled", async () => {
|
||||
jest.spyOn(GitHubClient.prototype, "createPullRequest").mockImplementation((backport: BackportPullRequest) => {
|
||||
throw new Error(`Mocked error: ${backport.base}`);
|
||||
});
|
||||
|
||||
addProcessArgs([
|
||||
"-tb",
|
||||
"v1, v2, v3",
|
||||
"-pr",
|
||||
"https://github.com/owner/reponame/pull/2368",
|
||||
"-f",
|
||||
"/tmp/folder",
|
||||
"--bp-branch-name",
|
||||
"custom-failure-head",
|
||||
"--enable-err-notification",
|
||||
]);
|
||||
|
||||
await expect(() => runner.execute()).rejects.toThrowError("Failure occurred during one of the backports: [Error: Mocked error: v1 ; Error: Mocked error: v2 ; Error: Mocked error: v3]");
|
||||
|
||||
const cwd = "/tmp/folder";
|
||||
|
||||
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
||||
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");
|
||||
|
||||
expect(GitCLIService.prototype.clone).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "v1");
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "v2");
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "v3");
|
||||
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "custom-failure-head-v1");
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "custom-failure-head-v2");
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "custom-failure-head-v3");
|
||||
|
||||
expect(GitCLIService.prototype.fetch).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/2368/head:pr/2368");
|
||||
|
||||
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc", undefined, undefined, undefined);
|
||||
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc", undefined, undefined, undefined);
|
||||
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc", undefined, undefined, undefined);
|
||||
|
||||
expect(GitCLIService.prototype.push).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "custom-failure-head-v1");
|
||||
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "custom-failure-head-v2");
|
||||
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "custom-failure-head-v3");
|
||||
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(3);
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
||||
owner: "owner",
|
||||
repo: "reponame",
|
||||
head: "custom-failure-head-v1",
|
||||
base: "v1",
|
||||
title: "[v1] PR Title",
|
||||
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: [],
|
||||
comments: [],
|
||||
});
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
||||
owner: "owner",
|
||||
repo: "reponame",
|
||||
head: "custom-failure-head-v2",
|
||||
base: "v2",
|
||||
title: "[v2] PR Title",
|
||||
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: [],
|
||||
comments: [],
|
||||
});
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledWith({
|
||||
owner: "owner",
|
||||
repo: "reponame",
|
||||
head: "custom-failure-head-v3",
|
||||
base: "v3",
|
||||
title: "[v3] PR Title",
|
||||
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: [],
|
||||
comments: [],
|
||||
});
|
||||
expect(GitHubClient.prototype.createPullRequest).toThrowError();
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(3);
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledWith("https://api.github.com/repos/owner/reponame/pulls/2368", "The backport to `v1` failed. Check the latest run for more details.");
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledWith("https://api.github.com/repos/owner/reponame/pulls/2368", "The backport to `v2` failed. Check the latest run for more details.");
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledWith("https://api.github.com/repos/owner/reponame/pulls/2368", "The backport to `v3` failed. Check the latest run for more details.");
|
||||
});
|
||||
|
||||
test("with some failures and dry run enabled", async () => {
|
||||
jest.spyOn(GitCLIService.prototype, "cherryPick").mockImplementation((cwd: string, sha: string) => {
|
||||
throw new Error(`Forced error: ${sha}`);
|
||||
});
|
||||
|
||||
addProcessArgs([
|
||||
"-tb",
|
||||
"v1, v2, v3",
|
||||
"-pr",
|
||||
"https://github.com/owner/reponame/pull/2368",
|
||||
"-f",
|
||||
"/tmp/folder",
|
||||
"--bp-branch-name",
|
||||
"custom-failure-head",
|
||||
"--enable-err-notification",
|
||||
"--dry-run",
|
||||
]);
|
||||
|
||||
await expect(() => runner.execute()).rejects.toThrowError("Failure occurred during one of the backports: [Error: Forced error: 28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc ; Error: Forced error: 28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc ; Error: Forced error: 28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc]");
|
||||
|
||||
const cwd = "/tmp/folder";
|
||||
|
||||
expect(GitClientFactory.getOrCreate).toBeCalledTimes(1);
|
||||
expect(GitClientFactory.getOrCreate).toBeCalledWith(GitClientType.GITHUB, undefined, "https://api.github.com");
|
||||
|
||||
expect(GitCLIService.prototype.clone).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "v1");
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "v2");
|
||||
expect(GitCLIService.prototype.clone).toBeCalledWith("https://github.com/owner/reponame.git", cwd, "v3");
|
||||
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "custom-failure-head-v1");
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "custom-failure-head-v2");
|
||||
expect(GitCLIService.prototype.createLocalBranch).toBeCalledWith(cwd, "custom-failure-head-v3");
|
||||
|
||||
expect(GitCLIService.prototype.fetch).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/2368/head:pr/2368");
|
||||
|
||||
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(3);
|
||||
expect(GitCLIService.prototype.cherryPick).toThrowError();
|
||||
|
||||
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
||||
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(0);
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -44,6 +44,7 @@ jest.mock("axios", () => {
|
|||
|
||||
jest.mock("@bp/service/git/git-cli");
|
||||
jest.spyOn(GitLabClient.prototype, "createPullRequest");
|
||||
jest.spyOn(GitLabClient.prototype, "createPullRequestComment");
|
||||
jest.spyOn(GitClientFactory, "getOrCreate");
|
||||
|
||||
|
||||
|
@ -105,6 +106,7 @@ describe("cli runner", () => {
|
|||
|
||||
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
||||
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(0);
|
||||
expect(GitLabClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("dry run with relative folder", async () => {
|
||||
|
@ -199,6 +201,7 @@ describe("cli runner", () => {
|
|||
]);
|
||||
|
||||
await expect(() => runner.execute()).rejects.toThrow("Provided pull request is closed and not merged");
|
||||
expect(GitLabClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("merged pull request", async () => {
|
||||
|
@ -246,6 +249,7 @@ describe("cli runner", () => {
|
|||
comments: [],
|
||||
}
|
||||
);
|
||||
expect(GitLabClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ const GITHUB_MERGED_PR_W_OVERRIDES_CONFIG_FILE_CONTENT = {
|
|||
|
||||
jest.mock("@bp/service/git/git-cli");
|
||||
jest.spyOn(GitHubClient.prototype, "createPullRequest");
|
||||
jest.spyOn(GitHubClient.prototype, "createPullRequestComment");
|
||||
jest.spyOn(GitClientFactory, "getOrCreate");
|
||||
|
||||
let parser: ArgsParser;
|
||||
|
@ -87,6 +88,7 @@ describe("gha runner", () => {
|
|||
|
||||
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
||||
expect(GitHubClient.prototype.createPullRequest).toBeCalledTimes(0);
|
||||
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("without dry run", async () => {
|
||||
|
|
|
@ -43,6 +43,7 @@ jest.mock("axios", () => {
|
|||
|
||||
jest.mock("@bp/service/git/git-cli");
|
||||
jest.spyOn(GitLabClient.prototype, "createPullRequest");
|
||||
jest.spyOn(GitLabClient.prototype, "createPullRequestComment");
|
||||
jest.spyOn(GitClientFactory, "getOrCreate");
|
||||
|
||||
let parser: ArgsParser;
|
||||
|
@ -98,6 +99,7 @@ describe("gha runner", () => {
|
|||
|
||||
expect(GitCLIService.prototype.push).toBeCalledTimes(0);
|
||||
expect(GitLabClient.prototype.createPullRequest).toBeCalledTimes(0);
|
||||
expect(GitLabClient.prototype.createPullRequestComment).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
test("without dry run", async () => {
|
||||
|
|
19
test/service/runner/runner-util.test.ts
Normal file
19
test/service/runner/runner-util.test.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { injectError, injectTargetBranch } from "@bp/service/runner/runner-util";
|
||||
|
||||
describe("check runner utilities", () => {
|
||||
test("properly inject error message", () => {
|
||||
expect(injectError("Original message: {{error}}", "to inject")).toStrictEqual("Original message: to inject");
|
||||
});
|
||||
|
||||
test("missing error placeholder in the original message", () => {
|
||||
expect(injectError("Original message: {{wrong}}", "to inject")).toStrictEqual("Original message: {{wrong}}");
|
||||
});
|
||||
|
||||
test("properly inject target branch into message", () => {
|
||||
expect(injectTargetBranch("Original message: {{target-branch}}", "to inject")).toStrictEqual("Original message: to inject");
|
||||
});
|
||||
|
||||
test("missing target branch placeholder in the original message", () => {
|
||||
expect(injectTargetBranch("Original message: {{wrong}}", "to inject")).toStrictEqual("Original message: {{wrong}}");
|
||||
});
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue