feat: update the default comment on error

This commit is contained in:
Andrea Lamparelli 2024-04-10 22:34:29 +02:00
parent 7c420531c8
commit 8b0412b111
10 changed files with 75 additions and 29 deletions

25
dist/cli/index.js vendored
View file

@ -303,8 +303,9 @@ exports["default"] = ConfigsParser;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.AuthTokenId = exports.MESSAGE_ERROR_PLACEHOLDER = void 0; exports.AuthTokenId = exports.MESSAGE_TARGET_BRANCH_PLACEHOLDER = exports.MESSAGE_ERROR_PLACEHOLDER = void 0;
exports.MESSAGE_ERROR_PLACEHOLDER = "{{error}}"; exports.MESSAGE_ERROR_PLACEHOLDER = "{{error}}";
exports.MESSAGE_TARGET_BRANCH_PLACEHOLDER = "{{target-branch}}";
var AuthTokenId; var AuthTokenId;
(function (AuthTokenId) { (function (AuthTokenId) {
// github specific token // github specific token
@ -391,7 +392,7 @@ class PullRequestConfigsParser extends configs_parser_1.default {
} }
getDefaultErrorComment() { getDefaultErrorComment() {
// TODO: fetch from arg or set default with placeholder {{error}} // TODO: fetch from arg or set default with placeholder {{error}}
return `Backporting failed: ${configs_types_1.MESSAGE_ERROR_PLACEHOLDER}`; return `The backport to \`${configs_types_1.MESSAGE_TARGET_BRANCH_PLACEHOLDER}\` failed. Check the latest run for more details.`;
} }
/** /**
* Parse the provided labels and return a list of target branches * Parse the provided labels and return a list of target branches
@ -1461,11 +1462,11 @@ exports["default"] = Logger;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.injectError = void 0; exports.injectTargetBranch = exports.injectError = void 0;
const configs_types_1 = __nccwpck_require__(4753); const configs_types_1 = __nccwpck_require__(4753);
/** /**
* Inject the error message in the provided `message`. * Inject the error message in the provided `message`.
* This is inject in place of the MESSAGE_ERROR_PLACEHOLDER placeholder * This is injected in place of the MESSAGE_ERROR_PLACEHOLDER placeholder
* @param message string that needs to be updated * @param message string that needs to be updated
* @param errMsg the error message that needs to be injected * @param errMsg the error message that needs to be injected
*/ */
@ -1473,6 +1474,17 @@ const injectError = (message, errMsg) => {
return message.replace(configs_types_1.MESSAGE_ERROR_PLACEHOLDER, errMsg); return message.replace(configs_types_1.MESSAGE_ERROR_PLACEHOLDER, errMsg);
}; };
exports.injectError = injectError; exports.injectError = injectError;
/**
* Inject the target branch into the provided `message`.
* This is injected in place of the MESSAGE_TARGET_BRANCH_PLACEHOLDER placeholder
* @param message string that needs to be updated
* @param targetBranch the target branch to inject
* @returns
*/
const injectTargetBranch = (message, targetBranch) => {
return message.replace(configs_types_1.MESSAGE_TARGET_BRANCH_PLACEHOLDER, targetBranch);
};
exports.injectTargetBranch = injectTargetBranch;
/***/ }), /***/ }),
@ -1559,8 +1571,9 @@ class Runner {
this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`); this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`);
if (!configs.dryRun && configs.errorNotification.enabled && configs.errorNotification.message.length > 0) { if (!configs.dryRun && configs.errorNotification.enabled && configs.errorNotification.message.length > 0) {
// notify the failure as comment in the original pull request // notify the failure as comment in the original pull request
const comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error); let comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error);
gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); comment = (0, runner_util_1.injectTargetBranch)(comment, pr.base);
await gitApi.createPullRequestComment(configs.originalPullRequest.url, comment);
} }
failures.push(error); failures.push(error);
} }

25
dist/gha/index.js vendored
View file

@ -268,8 +268,9 @@ exports["default"] = ConfigsParser;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.AuthTokenId = exports.MESSAGE_ERROR_PLACEHOLDER = void 0; exports.AuthTokenId = exports.MESSAGE_TARGET_BRANCH_PLACEHOLDER = exports.MESSAGE_ERROR_PLACEHOLDER = void 0;
exports.MESSAGE_ERROR_PLACEHOLDER = "{{error}}"; exports.MESSAGE_ERROR_PLACEHOLDER = "{{error}}";
exports.MESSAGE_TARGET_BRANCH_PLACEHOLDER = "{{target-branch}}";
var AuthTokenId; var AuthTokenId;
(function (AuthTokenId) { (function (AuthTokenId) {
// github specific token // github specific token
@ -356,7 +357,7 @@ class PullRequestConfigsParser extends configs_parser_1.default {
} }
getDefaultErrorComment() { getDefaultErrorComment() {
// TODO: fetch from arg or set default with placeholder {{error}} // TODO: fetch from arg or set default with placeholder {{error}}
return `Backporting failed: ${configs_types_1.MESSAGE_ERROR_PLACEHOLDER}`; return `The backport to \`${configs_types_1.MESSAGE_TARGET_BRANCH_PLACEHOLDER}\` failed. Check the latest run for more details.`;
} }
/** /**
* Parse the provided labels and return a list of target branches * Parse the provided labels and return a list of target branches
@ -1426,11 +1427,11 @@ exports["default"] = Logger;
"use strict"; "use strict";
Object.defineProperty(exports, "__esModule", ({ value: true })); Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.injectError = void 0; exports.injectTargetBranch = exports.injectError = void 0;
const configs_types_1 = __nccwpck_require__(4753); const configs_types_1 = __nccwpck_require__(4753);
/** /**
* Inject the error message in the provided `message`. * Inject the error message in the provided `message`.
* This is inject in place of the MESSAGE_ERROR_PLACEHOLDER placeholder * This is injected in place of the MESSAGE_ERROR_PLACEHOLDER placeholder
* @param message string that needs to be updated * @param message string that needs to be updated
* @param errMsg the error message that needs to be injected * @param errMsg the error message that needs to be injected
*/ */
@ -1438,6 +1439,17 @@ const injectError = (message, errMsg) => {
return message.replace(configs_types_1.MESSAGE_ERROR_PLACEHOLDER, errMsg); return message.replace(configs_types_1.MESSAGE_ERROR_PLACEHOLDER, errMsg);
}; };
exports.injectError = injectError; exports.injectError = injectError;
/**
* Inject the target branch into the provided `message`.
* This is injected in place of the MESSAGE_TARGET_BRANCH_PLACEHOLDER placeholder
* @param message string that needs to be updated
* @param targetBranch the target branch to inject
* @returns
*/
const injectTargetBranch = (message, targetBranch) => {
return message.replace(configs_types_1.MESSAGE_TARGET_BRANCH_PLACEHOLDER, targetBranch);
};
exports.injectTargetBranch = injectTargetBranch;
/***/ }), /***/ }),
@ -1524,8 +1536,9 @@ class Runner {
this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`); this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`);
if (!configs.dryRun && configs.errorNotification.enabled && configs.errorNotification.message.length > 0) { if (!configs.dryRun && configs.errorNotification.enabled && configs.errorNotification.message.length > 0) {
// notify the failure as comment in the original pull request // notify the failure as comment in the original pull request
const comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error); let comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error);
gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); comment = (0, runner_util_1.injectTargetBranch)(comment, pr.base);
await gitApi.createPullRequestComment(configs.originalPullRequest.url, comment);
} }
failures.push(error); failures.push(error);
} }

View file

@ -3,6 +3,7 @@
import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types"; import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types";
export const MESSAGE_ERROR_PLACEHOLDER = "{{error}}"; export const MESSAGE_ERROR_PLACEHOLDER = "{{error}}";
export const MESSAGE_TARGET_BRANCH_PLACEHOLDER = "{{target-branch}}";
export interface LocalGit { export interface LocalGit {
user: string, // local git user user: string, // local git user

View file

@ -1,7 +1,7 @@
import { getAsCleanedCommaSeparatedList, getAsCommaSeparatedList } from "@bp/service/args/args-utils"; import { getAsCleanedCommaSeparatedList, getAsCommaSeparatedList } from "@bp/service/args/args-utils";
import { Args } from "@bp/service/args/args.types"; import { Args } from "@bp/service/args/args.types";
import ConfigsParser from "@bp/service/configs/configs-parser"; import ConfigsParser from "@bp/service/configs/configs-parser";
import { Configs, MESSAGE_ERROR_PLACEHOLDER } from "@bp/service/configs/configs.types"; import { Configs, MESSAGE_TARGET_BRANCH_PLACEHOLDER } from "@bp/service/configs/configs.types";
import GitClient from "@bp/service/git/git-client"; import GitClient from "@bp/service/git/git-client";
import GitClientFactory from "@bp/service/git/git-client-factory"; import GitClientFactory from "@bp/service/git/git-client-factory";
import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types"; import { BackportPullRequest, GitPullRequest } from "@bp/service/git/git.types";
@ -72,7 +72,7 @@ export default class PullRequestConfigsParser extends ConfigsParser {
private getDefaultErrorComment(): string { private getDefaultErrorComment(): string {
// TODO: fetch from arg or set default with placeholder {{error}} // TODO: fetch from arg or set default with placeholder {{error}}
return `Backporting failed: ${MESSAGE_ERROR_PLACEHOLDER}`; return `The backport to \`${MESSAGE_TARGET_BRANCH_PLACEHOLDER}\` failed. Check the latest run for more details.`;
} }
/** /**

View file

@ -1,11 +1,22 @@
import { MESSAGE_ERROR_PLACEHOLDER } from "@bp/service/configs/configs.types"; import { MESSAGE_ERROR_PLACEHOLDER, MESSAGE_TARGET_BRANCH_PLACEHOLDER } from "@bp/service/configs/configs.types";
/** /**
* Inject the error message in the provided `message`. * Inject the error message in the provided `message`.
* This is inject in place of the MESSAGE_ERROR_PLACEHOLDER placeholder * This is injected in place of the MESSAGE_ERROR_PLACEHOLDER placeholder
* @param message string that needs to be updated * @param message string that needs to be updated
* @param errMsg the error message that needs to be injected * @param errMsg the error message that needs to be injected
*/ */
export const injectError = (message: string, errMsg: string): string => { export const injectError = (message: string, errMsg: string): string => {
return message.replace(MESSAGE_ERROR_PLACEHOLDER, errMsg); return message.replace(MESSAGE_ERROR_PLACEHOLDER, errMsg);
};
/**
* Inject the target branch into the provided `message`.
* This is injected in place of the MESSAGE_TARGET_BRANCH_PLACEHOLDER placeholder
* @param message string that needs to be updated
* @param targetBranch the target branch to inject
* @returns
*/
export const injectTargetBranch = (message: string, targetBranch: string): string => {
return message.replace(MESSAGE_TARGET_BRANCH_PLACEHOLDER, targetBranch);
}; };

View file

@ -9,7 +9,7 @@ import { BackportPullRequest, GitClientType, GitPullRequest } from "@bp/service/
import LoggerService from "@bp/service/logger/logger-service"; import LoggerService from "@bp/service/logger/logger-service";
import LoggerServiceFactory from "@bp/service/logger/logger-service-factory"; import LoggerServiceFactory from "@bp/service/logger/logger-service-factory";
import { inferGitClient, inferGitApiUrl, getGitTokenFromEnv } from "@bp/service/git/git-util"; import { inferGitClient, inferGitApiUrl, getGitTokenFromEnv } from "@bp/service/git/git-util";
import { injectError } from "./runner-util"; import { injectError, injectTargetBranch } from "./runner-util";
interface Git { interface Git {
gitClientType: GitClientType; gitClientType: GitClientType;
@ -95,8 +95,9 @@ export default class Runner {
this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`); this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`);
if (!configs.dryRun && configs.errorNotification.enabled && configs.errorNotification.message.length > 0) { if (!configs.dryRun && configs.errorNotification.enabled && configs.errorNotification.message.length > 0) {
// notify the failure as comment in the original pull request // notify the failure as comment in the original pull request
const comment = injectError(configs.errorNotification.message, error as string); let comment = injectError(configs.errorNotification.message, error as string);
gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); comment = injectTargetBranch(comment, pr.base);
await gitApi.createPullRequestComment(configs.originalPullRequest.url, comment);
} }
failures.push(error as string); failures.push(error as string);
} }

View file

@ -141,7 +141,7 @@ describe("github pull request config parser", () => {
}); });
expect(configs.errorNotification).toEqual({ expect(configs.errorNotification).toEqual({
enabled: false, enabled: false,
message: "Backporting failed: {{error}}" message: "The backport to `{{target-branch}}` failed. Check the latest run for more details."
}); });
}); });
@ -962,7 +962,7 @@ describe("github pull request config parser", () => {
expect(configs.errorNotification).toEqual({ expect(configs.errorNotification).toEqual({
"enabled": true, "enabled": true,
"message": "Backporting failed: {{error}}" "message": "The backport to `{{target-branch}}` failed. Check the latest run for more details."
}); });
}); });
}); });

View file

@ -146,7 +146,7 @@ describe("gitlab merge request config parser", () => {
}); });
expect(configs.errorNotification).toEqual({ expect(configs.errorNotification).toEqual({
"enabled": false, "enabled": false,
"message": "Backporting failed: {{error}}" "message": "The backport to `{{target-branch}}` failed. Check the latest run for more details."
}); });
}); });
@ -905,7 +905,7 @@ describe("gitlab merge request config parser", () => {
expect(configs.errorNotification).toEqual({ expect(configs.errorNotification).toEqual({
"enabled": true, "enabled": true,
"message": "Backporting failed: {{error}}", "message": "The backport to `{{target-branch}}` failed. Check the latest run for more details.",
}); });
}); });
}); });

View file

@ -1238,7 +1238,6 @@ describe("cli runner", () => {
test("with multiple target branches, one failure and error notification enabled", async () => { test("with multiple target branches, one failure and error notification enabled", async () => {
jest.spyOn(GitHubClient.prototype, "createPullRequest").mockImplementation((backport: BackportPullRequest) => { jest.spyOn(GitHubClient.prototype, "createPullRequest").mockImplementation((backport: BackportPullRequest) => {
throw new Error(`Mocked error: ${backport.base}`); throw new Error(`Mocked error: ${backport.base}`);
}); });
@ -1323,9 +1322,9 @@ describe("cli runner", () => {
}); });
expect(GitHubClient.prototype.createPullRequest).toThrowError(); expect(GitHubClient.prototype.createPullRequest).toThrowError();
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(3); expect(GitHubClient.prototype.createPullRequestComment).toBeCalledTimes(3);
expect(GitHubClient.prototype.createPullRequestComment).toBeCalledWith("https://api.github.com/repos/owner/reponame/pulls/2368", "Backporting failed: Error: Mocked error: v1"); 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", "Backporting failed: Error: Mocked error: v2"); 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", "Backporting failed: Error: Mocked error: v3"); 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 () => { test("with some failures and dry run enabled", async () => {

View file

@ -1,11 +1,19 @@
import { injectError } from "@bp/service/runner/runner-util"; import { injectError, injectTargetBranch } from "@bp/service/runner/runner-util";
describe("check runner utilities", () => { describe("check runner utilities", () => {
test("properly inject error message", () => { test("properly inject error message", () => {
expect(injectError("Original message: {{error}}", "to inject")).toStrictEqual("Original message: to inject"); expect(injectError("Original message: {{error}}", "to inject")).toStrictEqual("Original message: to inject");
}); });
test("missing placeholder in the original message", () => { test("missing error placeholder in the original message", () => {
expect(injectError("Original message: {{wrong}}", "to inject")).toStrictEqual("Original message: {{wrong}}"); 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}}");
});
}); });