Skip to content

Commit

Permalink
feat: disable comment if dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
lampajr committed Apr 10, 2024
1 parent 1f09f3d commit 7c42053
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/service/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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}`);
}
Expand All @@ -171,4 +170,4 @@ export default class Runner {

this.logger.clearContext();
}
}
}
49 changes: 48 additions & 1 deletion test/service/runner/cli-github-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

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);
});
});

0 comments on commit 7c42053

Please sign in to comment.