diff --git a/dist/cli/index.js b/dist/cli/index.js index a68d73c..b19ef88 100755 --- a/dist/cli/index.js +++ b/dist/cli/index.js @@ -1557,7 +1557,7 @@ class Runner { } catch (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 const comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error); gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); diff --git a/dist/gha/index.js b/dist/gha/index.js index 2cfc0b1..e0541dd 100755 --- a/dist/gha/index.js +++ b/dist/gha/index.js @@ -1522,7 +1522,7 @@ class Runner { } catch (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 const comment = (0, runner_util_1.injectError)(configs.errorNotification.message, error); gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); diff --git a/src/service/runner/runner.ts b/src/service/runner/runner.ts index a536fa7..a93e810 100644 --- a/src/service/runner/runner.ts +++ b/src/service/runner/runner.ts @@ -93,7 +93,7 @@ export default class Runner { }); } catch(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 const comment = injectError(configs.errorNotification.message, error as string); gitApi.createPullRequestComment(configs.originalPullRequest.url, comment); @@ -139,13 +139,12 @@ export default class Runner { // 5. create new branch from target one and checkout this.logger.debug("Creating local branch.."); - await git.gitCli.createLocalBranch(configs.folder, backportPR.head); // 6. fetch pull request remote if source owner != target owner or pull request still open if (configs.originalPullRequest.sourceRepo.owner !== configs.originalPullRequest.targetRepo.owner || configs.originalPullRequest.state === "open") { - this.logger.debug("Fetching pull request remote.."); + this.logger.debug("Fetching pull request remote.."); const prefix = git.gitClientType === GitClientType.GITLAB ? "merge-requests" : "pull" ; // default is for gitlab await git.gitCli.fetch(configs.folder, `${prefix}/${configs.originalPullRequest.number}/head:pr/${configs.originalPullRequest.number}`); } @@ -171,4 +170,4 @@ export default class Runner { this.logger.clearContext(); } -} \ No newline at end of file +} diff --git a/test/service/runner/cli-github-runner.test.ts b/test/service/runner/cli-github-runner.test.ts index 153a232..72bc133 100644 --- a/test/service/runner/cli-github-runner.test.ts +++ b/test/service/runner/cli-github-runner.test.ts @@ -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: v3"); }); -}); \ No newline at end of file + + 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); + }); +});