1
0
Fork 0
mirror of https://code.forgejo.org/actions/git-backporting synced 2025-03-15 06:36:59 +01:00

fix(gh130): apply commits in the correct order on github (#131)

This commit is contained in:
Andrea Lamparelli 2024-07-16 17:49:23 +02:00 committed by GitHub
parent da6431b114
commit cb3473d7c9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 3562 additions and 11 deletions

10
dist/cli/index.js vendored
View file

@ -1619,7 +1619,15 @@ class Runner {
} }
// 7. apply all changes to the new branch // 7. apply all changes to the new branch
this.logger.debug("Cherry picking commits.."); this.logger.debug("Cherry picking commits..");
for (const sha of originalPR.commits.reverse()) { // Commits might be ordered in different ways based on the git service, e.g., considering top to bottom
// GITHUB --> oldest to newest
// CODEBERG --> newest to oldest
// GITLAB --> newest to oldest
if (git.gitClientType === git_types_1.GitClientType.CODEBERG || git.gitClientType === git_types_1.GitClientType.GITLAB) {
// reverse the order as we always need to process from older to newest
originalPR.commits.reverse();
}
for (const sha of originalPR.commits) {
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions); await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions);
} }
if (!configs.dryRun) { if (!configs.dryRun) {

10
dist/gha/index.js vendored
View file

@ -1584,7 +1584,15 @@ class Runner {
} }
// 7. apply all changes to the new branch // 7. apply all changes to the new branch
this.logger.debug("Cherry picking commits.."); this.logger.debug("Cherry picking commits..");
for (const sha of originalPR.commits.reverse()) { // Commits might be ordered in different ways based on the git service, e.g., considering top to bottom
// GITHUB --> oldest to newest
// CODEBERG --> newest to oldest
// GITLAB --> newest to oldest
if (git.gitClientType === git_types_1.GitClientType.CODEBERG || git.gitClientType === git_types_1.GitClientType.GITLAB) {
// reverse the order as we always need to process from older to newest
originalPR.commits.reverse();
}
for (const sha of originalPR.commits) {
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions); await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions);
} }
if (!configs.dryRun) { if (!configs.dryRun) {

View file

@ -152,7 +152,15 @@ export default class Runner {
// 7. apply all changes to the new branch // 7. apply all changes to the new branch
this.logger.debug("Cherry picking commits.."); this.logger.debug("Cherry picking commits..");
for (const sha of originalPR.commits.reverse()!) { // Commits might be ordered in different ways based on the git service, e.g., considering top to bottom
// GITHUB --> oldest to newest
// CODEBERG --> newest to oldest
// GITLAB --> newest to oldest
if (git.gitClientType === GitClientType.CODEBERG || git.gitClientType === GitClientType.GITLAB) {
// reverse the order as we always need to process from older to newest
originalPR.commits.reverse();
}
for (const sha of originalPR.commits) {
await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions); await git.gitCli.cherryPick(configs.folder, sha, configs.mergeStrategy, configs.mergeStrategyOption, configs.cherryPickOptions);
} }

File diff suppressed because it is too large Load diff

View file

@ -376,8 +376,8 @@ describe("cli runner", () => {
expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/4444/head:pr/4444"); expect(GitCLIService.prototype.fetch).toBeCalledWith(cwd, "pull/4444/head:pr/4444");
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2); expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2);
expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", undefined, undefined, undefined); expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", undefined, undefined, undefined);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", undefined, undefined, undefined); expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", undefined, undefined, undefined);
expect(GitCLIService.prototype.push).toBeCalledTimes(1); expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3"); expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3");
@ -733,8 +733,8 @@ describe("cli runner", () => {
expect(GitCLIService.prototype.fetch).toBeCalledTimes(0); expect(GitCLIService.prototype.fetch).toBeCalledTimes(0);
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2); expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2);
expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", undefined, undefined, undefined); expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", undefined, undefined, undefined);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", undefined, undefined, undefined); expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", undefined, undefined, undefined);
expect(GitCLIService.prototype.push).toBeCalledTimes(1); expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3"); expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3");
@ -839,8 +839,8 @@ describe("cli runner", () => {
expect(GitCLIService.prototype.fetch).toBeCalledTimes(0); expect(GitCLIService.prototype.fetch).toBeCalledTimes(0);
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2); expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2);
expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", "ort", "find-renames", undefined); expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", "ort", "find-renames", undefined);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", "ort", "find-renames", undefined); expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", "ort", "find-renames", undefined);
expect(GitCLIService.prototype.push).toBeCalledTimes(1); expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3"); expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3");

View file

@ -500,8 +500,8 @@ describe("gha runner", () => {
expect(GitCLIService.prototype.fetch).toBeCalledTimes(0); expect(GitCLIService.prototype.fetch).toBeCalledTimes(0);
expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2); expect(GitCLIService.prototype.cherryPick).toBeCalledTimes(2);
expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", undefined, undefined, undefined); expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "0404fb922ab75c3a8aecad5c97d9af388df04695", undefined, undefined, undefined);
expect(GitCLIService.prototype.cherryPick).toBeCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", undefined, undefined, undefined); expect(GitCLIService.prototype.cherryPick).toHaveBeenLastCalledWith(cwd, "11da4e38aa3e577ffde6d546f1c52e53b04d3151", undefined, undefined, undefined);
expect(GitCLIService.prototype.push).toBeCalledTimes(1); expect(GitCLIService.prototype.push).toBeCalledTimes(1);
expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3"); expect(GitCLIService.prototype.push).toBeCalledWith(cwd, "bp-target-0404fb9-11da4e3");

File diff suppressed because it is too large Load diff

View file

@ -2,6 +2,7 @@ import LoggerServiceFactory from "@bp/service/logger/logger-service-factory";
import { Moctokit } from "@kie/mock-github"; import { Moctokit } from "@kie/mock-github";
import { TARGET_OWNER, REPO, MERGED_PR_FIXTURE, OPEN_PR_FIXTURE, NOT_MERGED_PR_FIXTURE, NOT_FOUND_PR_NUMBER, MULT_COMMITS_PR_FIXTURE, MULT_COMMITS_PR_COMMITS, NEW_PR_URL, NEW_PR_NUMBER, GITHUB_GET_COMMIT } from "./github-data"; import { TARGET_OWNER, REPO, MERGED_PR_FIXTURE, OPEN_PR_FIXTURE, NOT_MERGED_PR_FIXTURE, NOT_FOUND_PR_NUMBER, MULT_COMMITS_PR_FIXTURE, MULT_COMMITS_PR_COMMITS, NEW_PR_URL, NEW_PR_NUMBER, GITHUB_GET_COMMIT } from "./github-data";
import { CLOSED_NOT_MERGED_MR, MERGED_SQUASHED_MR, NESTED_NAMESPACE_MR, OPEN_MR, OPEN_PR_COMMITS, PROJECT_EXAMPLE, NESTED_PROJECT_EXAMPLE, SUPERUSER, MERGED_SQUASHED_MR_COMMITS } from "./gitlab-data"; import { CLOSED_NOT_MERGED_MR, MERGED_SQUASHED_MR, NESTED_NAMESPACE_MR, OPEN_MR, OPEN_PR_COMMITS, PROJECT_EXAMPLE, NESTED_PROJECT_EXAMPLE, SUPERUSER, MERGED_SQUASHED_MR_COMMITS } from "./gitlab-data";
import { CB_TARGET_OWNER, CB_REPO, CB_MERGED_PR_FIXTURE, CB_OPEN_PR_FIXTURE, CB_NOT_MERGED_PR_FIXTURE, CB_NOT_FOUND_PR_NUMBER, CB_MULT_COMMITS_PR_FIXTURE, CB_MULT_COMMITS_PR_COMMITS, CB_NEW_PR_URL, CB_NEW_PR_NUMBER, CODEBERG_GET_COMMIT } from "./codeberg-data";
// high number, for each test we are not expecting // high number, for each test we are not expecting
// to send more than 3 reqs per api endpoint // to send more than 3 reqs per api endpoint
@ -239,3 +240,151 @@ export const mockGitHubClient = (apiUrl = "https://api.github.com"): Moctokit =>
return mock; return mock;
}; };
// CODEBERG - OCTOKIT
export const mockCodebergClient = (apiUrl = "https://codeberg.org/api/v1"): Moctokit => {
logger.debug("Setting up moctokit..");
const mock = new Moctokit(apiUrl);
// setup the mock requests here
// valid requests
mock.rest.pulls
.get({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_MERGED_PR_FIXTURE.number
})
.reply({
status: 200,
data: CB_MERGED_PR_FIXTURE
});
mock.rest.pulls
.get({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_MULT_COMMITS_PR_FIXTURE.number
})
.reply({
status: 200,
data: CB_MULT_COMMITS_PR_FIXTURE
});
mock.rest.pulls
.get({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_OPEN_PR_FIXTURE.number
})
.reply({
status: 200,
data: CB_OPEN_PR_FIXTURE
});
mock.rest.pulls
.get({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_NOT_MERGED_PR_FIXTURE.number
})
.reply({
status: 200,
data: CB_NOT_MERGED_PR_FIXTURE
});
mock.rest.pulls
.listCommits({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_MULT_COMMITS_PR_FIXTURE.number
})
.reply({
status: 200,
data: CB_MULT_COMMITS_PR_COMMITS
});
mock.rest.pulls
.listCommits({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_OPEN_PR_FIXTURE.number
})
.reply({
status: 200,
data: CB_MULT_COMMITS_PR_COMMITS
});
mock.rest.pulls
.create()
.reply({
repeat: REPEAT,
status: 201,
data: {
number: CB_NEW_PR_NUMBER,
html_url: CB_NEW_PR_URL,
}
});
mock.rest.pulls
.requestReviewers()
.reply({
repeat: REPEAT,
status: 201,
data: CB_MERGED_PR_FIXTURE
});
mock.rest.issues
.addAssignees()
.reply({
repeat: REPEAT,
status: 201,
data: {}
});
mock.rest.issues
.addLabels()
.reply({
repeat: REPEAT,
status: 200,
data: {}
});
mock.rest.issues
.createComment()
.reply({
repeat: REPEAT,
status: 201,
data: {}
});
mock.rest.git
.getCommit({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
commit_sha: "28f63db774185f4ec4b57cd9aaeb12dbfb4c9ecc",
})
.reply({
status: 200,
data: CODEBERG_GET_COMMIT,
});
// invalid requests
mock.rest.pulls
.get({
owner: CB_TARGET_OWNER,
repo: CB_REPO,
pull_number: CB_NOT_FOUND_PR_NUMBER
})
.reply({
repeat: REPEAT,
status: 404,
data: {
message: "Not found"
}
});
return mock;
};