feat: disable comment if dry-run

This commit is contained in:
Andrea Lamparelli 2024-04-10 21:48:29 +02:00
parent 1f09f3d7ea
commit 7c420531c8
4 changed files with 53 additions and 7 deletions

2
dist/cli/index.js vendored
View file

@ -1557,7 +1557,7 @@ class Runner {
} }
catch (error) { catch (error) {
this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`); this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`);
if (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); const comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error);
gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); gitApi.createPullRequestComment(configs.originalPullRequest.url, comment);

2
dist/gha/index.js vendored
View file

@ -1522,7 +1522,7 @@ class Runner {
} }
catch (error) { catch (error) {
this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`); this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`);
if (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); const comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error);
gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); gitApi.createPullRequestComment(configs.originalPullRequest.url, comment);

View file

@ -93,7 +93,7 @@ export default class Runner {
}); });
} catch(error) { } catch(error) {
this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`); this.logger.error(`Something went wrong backporting to ${pr.base}: ${error}`);
if (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); const comment = injectError(configs.errorNotification.message, error as string);
gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); gitApi.createPullRequestComment(configs.originalPullRequest.url, comment);
@ -139,7 +139,6 @@ export default class Runner {
// 5. create new branch from target one and checkout // 5. create new branch from target one and checkout
this.logger.debug("Creating local branch.."); this.logger.debug("Creating local branch..");
await git.gitCli.createLocalBranch(configs.folder, backportPR.head); await git.gitCli.createLocalBranch(configs.folder, backportPR.head);
// 6. fetch pull request remote if source owner != target owner or pull request still open // 6. fetch pull request remote if source owner != target owner or pull request still open

View file

@ -1327,4 +1327,51 @@ describe("cli runner", () => {
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", "Backporting failed: Error: Mocked error: v2");
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", "Backporting failed: Error: Mocked error: v3");
}); });
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);
});
}); });