Skip to content

Commit

Permalink
dist: build new artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
korthout committed Dec 4, 2023
1 parent 13b24fe commit 32e3a3b
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 7 deletions.
226 changes: 220 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.findTargetBranches = exports.Backport = void 0;
exports.findTargetBranches = exports.Backport = exports.experimentalDefaults = void 0;
const core = __importStar(__nccwpck_require__(2186));
const dedent_1 = __importDefault(__nccwpck_require__(5281));
const github_1 = __nccwpck_require__(5928);
const git_1 = __nccwpck_require__(3374);
const utils = __importStar(__nccwpck_require__(918));
const experimentalDefaults = {
detect_merge_method: false,
};
exports.experimentalDefaults = experimentalDefaults;
var Output;
(function (Output) {
Output["wasSuccessful"] = "was_successful";
Expand Down Expand Up @@ -85,9 +90,46 @@ class Backport {
console.log(`Fetching all the commits from the pull request: ${mainpr.commits + 1}`);
yield this.git.fetch(`refs/pull/${pull_number}/head`, this.config.pwd, mainpr.commits + 1);
const commitShas = yield this.github.getCommits(mainpr);
console.log(`Found commits: ${commitShas}`);
let commitShasToCherryPick;
if (this.config.experimental.detect_merge_method) {
const merge_commit_sha = yield this.github.getMergeCommitSha(mainpr);
// switch case to check if it is a squash, rebase, or merge commit
switch (yield this.github.mergeStrategy(mainpr, merge_commit_sha)) {
case github_1.MergeStrategy.SQUASHED:
// If merged via a squash merge_commit_sha represents the SHA of the squashed commit on
// the base branch. We must fetch it and its parent in case of a shallowly cloned repo
// To store the fetched commits indefinitely we save them to a remote ref using the sha
yield this.git.fetch(`+${merge_commit_sha}:refs/remotes/origin/${merge_commit_sha}`, this.config.pwd, 2);
commitShasToCherryPick = [merge_commit_sha];
break;
case github_1.MergeStrategy.REBASED:
// If rebased merge_commit_sha represents the commit that the base branch was updated to
// We must fetch it, its parents, and one extra parent in case of a shallowly cloned repo
// To store the fetched commits indefinitely we save them to a remote ref using the sha
yield this.git.fetch(`+${merge_commit_sha}:refs/remotes/origin/${merge_commit_sha}`, this.config.pwd, mainpr.commits + 1);
const range = `${merge_commit_sha}~${mainpr.commits}..${merge_commit_sha}`;
commitShasToCherryPick = yield this.git.findCommitsInRange(range, this.config.pwd);
break;
case github_1.MergeStrategy.MERGECOMMIT:
commitShasToCherryPick = commitShas;
break;
case github_1.MergeStrategy.UNKNOWN:
console.log("Could not detect merge strategy. Using commits from the Pull Request.");
commitShasToCherryPick = commitShas;
break;
default:
console.log("Could not detect merge strategy. Using commits from the Pull Request.");
commitShasToCherryPick = commitShas;
break;
}
}
else {
console.log("Not detecting merge strategy. Using commits from the Pull Request.");
commitShasToCherryPick = commitShas;
}
console.log(`Found commits to backport: ${commitShasToCherryPick}`);
console.log("Checking the merged pull request for merge commits");
const mergeCommitShas = yield this.git.findMergeCommits(commitShas, this.config.pwd);
const mergeCommitShas = yield this.git.findMergeCommits(commitShasToCherryPick, this.config.pwd);
console.log(`Encountered ${(_d = mergeCommitShas.length) !== null && _d !== void 0 ? _d : "no"} merge commits`);
if (mergeCommitShas.length > 0 &&
this.config.commits.merge_commits == "fail") {
Expand All @@ -102,11 +144,10 @@ class Backport {
});
return;
}
let commitShasToCherryPick = commitShas;
if (mergeCommitShas.length > 0 &&
this.config.commits.merge_commits == "skip") {
console.log("Skipping merge commits: " + mergeCommitShas);
const nonMergeCommitShas = commitShas.filter((sha) => !mergeCommitShas.includes(sha));
const nonMergeCommitShas = commitShasToCherryPick.filter((sha) => !mergeCommitShas.includes(sha));
commitShasToCherryPick = nonMergeCommitShas;
}
console.log("Will cherry-pick the following commits: " + commitShasToCherryPick);
Expand Down Expand Up @@ -456,6 +497,19 @@ class Git {
}
});
}
findCommitsInRange(range, pwd) {
return __awaiter(this, void 0, void 0, function* () {
const { exitCode, stdout } = yield this.git("log", ['--pretty=format:"%H"', "--reverse", range], pwd);
if (exitCode !== 0) {
throw new Error(`'git log --pretty=format:"%H" ${range}' failed with exit code ${exitCode}`);
}
const commitShas = stdout
.split("\n")
.map((sha) => sha.replace(/"/g, ""))
.filter((sha) => sha.trim() !== "");
return commitShas;
});
}
findMergeCommits(commitShas, pwd) {
return __awaiter(this, void 0, void 0, function* () {
const range = `${commitShas[0]}^..${commitShas[commitShas.length - 1]}`;
Expand Down Expand Up @@ -556,7 +610,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
};
var _Github_octokit, _Github_context;
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.Github = void 0;
exports.MergeStrategy = exports.Github = void 0;
const github = __importStar(__nccwpck_require__(5438));
class Github {
constructor(token) {
Expand Down Expand Up @@ -660,9 +714,156 @@ class Github {
return __classPrivateFieldGet(this, _Github_octokit, "f").rest.issues.update(Object.assign(Object.assign({}, this.getRepo()), { issue_number: pr, milestone: milestone }));
});
}
/**
* Retrieves the SHA of the merge commit for a given pull request.
*
* After merging a pull request, the `merge_commit_sha` attribute changes depending on how you merged the pull request:
*
* - If merged as a merge commit, `merge_commit_sha` represents the SHA of the merge commit.
* - If merged via a squash, `merge_commit_sha` represents the SHA of the squashed commit on the base branch.
* - If rebased, `merge_commit_sha` represents the commit that the base branch was updated to.
*
* See: https://docs.github.com/en/free-pro-team@latest/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request
*
* @param pull - The pull request object.
* @returns The SHA of the merge commit.
*/
getMergeCommitSha(pull) {
return __awaiter(this, void 0, void 0, function* () {
return pull.merge_commit_sha;
});
}
/**
* Retrieves a commit from the repository.
* @param sha - The SHA of the commit to retrieve.
* @returns A promise that resolves to the retrieved commit.
*/
getCommit(sha) {
return __awaiter(this, void 0, void 0, function* () {
const commit = __classPrivateFieldGet(this, _Github_octokit, "f").rest.repos.getCommit(Object.assign(Object.assign({}, this.getRepo()), { ref: sha }));
return commit;
});
}
/**
* Retrieves the parents of a commit.
* @param sha - The SHA of the commit.
* @returns A promise that resolves to the parents of the commit.
*/
getParents(sha) {
return __awaiter(this, void 0, void 0, function* () {
const commit = yield this.getCommit(sha);
return commit.data.parents;
});
}
/**
* Retrieves the pull requests associated with a specific commit.
* @param sha The SHA of the commit.
* @returns A promise that resolves to the pull requests associated with the commit.
*/
getPullRequestsAssociatedWithCommit(sha) {
return __awaiter(this, void 0, void 0, function* () {
const pr = __classPrivateFieldGet(this, _Github_octokit, "f").rest.repos.listPullRequestsAssociatedWithCommit(Object.assign(Object.assign({}, this.getRepo()), { commit_sha: sha }));
return pr;
});
}
/**
* Checks if a given SHA is associated with a specific pull request.
* @param sha - The SHA of the commit.
* @param pull - The pull request to check against.
* @returns A boolean indicating whether the SHA is associated with the pull request.
*/
isShaAssociatedWithPullRequest(sha, pull) {
return __awaiter(this, void 0, void 0, function* () {
const assoc_pr = yield this.getPullRequestsAssociatedWithCommit(sha);
const assoc_pr_data = assoc_pr.data;
// commits can be associated with multiple PRs
// checks if any of the assoc_prs is the same as the pull
return assoc_pr_data.some((pr) => pr.number == pull.number);
});
}
/**
* Checks if a commit is a merge commit.
* @param parents - An array of parent commit hashes.
* @returns A promise that resolves to a boolean indicating whether the commit is a merge commit.
*/
isMergeCommit(parents) {
return __awaiter(this, void 0, void 0, function* () {
return parents.length > 1;
});
}
/**
* Checks if a pull request is rebased.
* @param first_parent_belongs_to_pr - Indicates if the parent belongs to a pull request.
* @param merge_belongs_to_pr - Indicates if the merge belongs to a pull request.
* @param pull - The pull request object.
* @returns A boolean value indicating if the pull request is rebased.
*/
isRebased(first_parent_belongs_to_pr, merge_belongs_to_pr, pull) {
return __awaiter(this, void 0, void 0, function* () {
return first_parent_belongs_to_pr && merge_belongs_to_pr;
});
}
/**
* Checks if a merge commit is squashed.
* @param first_parent_belongs_to_pr - Indicates if the parent commit belongs to a pull request.
* @param merge_belongs_to_pr - Indicates if the merge commit belongs to a pull request.
* @returns A boolean value indicating if the merge commit is squashed.
*/
isSquashed(first_parent_belongs_to_pr, merge_belongs_to_pr) {
return __awaiter(this, void 0, void 0, function* () {
return !first_parent_belongs_to_pr && merge_belongs_to_pr;
});
}
/**
* Determines the merge strategy used for a given pull request.
*
* @param pull - The pull request to analyze.
* @returns The merge strategy used for the pull request.
*/
mergeStrategy(pull, merge_commit_sha) {
return __awaiter(this, void 0, void 0, function* () {
if (merge_commit_sha == null) {
console.log("PR was merged without merge_commit_sha unable to detect merge method");
return MergeStrategy.UNKNOWN;
}
const parents = yield this.getParents(merge_commit_sha);
if (yield this.isMergeCommit(parents)) {
console.log("PR was merged using a merge commit");
return MergeStrategy.MERGECOMMIT;
}
// if there is only one commit, it is a rebase OR a squash but we treat it
// as a squash.
if (pull.commits == 1) {
console.log("PR was merged using a squash or a rebase. Choosing squash strategy.");
return MergeStrategy.SQUASHED;
}
// Prepare the data for the rebase and squash checks.
const first_parent_sha = parents[0].sha;
const first_parent_belonts_to_pr = yield this.isShaAssociatedWithPullRequest(first_parent_sha, pull);
const merge_belongs_to_pr = yield this.isShaAssociatedWithPullRequest(merge_commit_sha, pull);
// This is the case when the PR is merged using a rebase.
// and has multiple commits.
if (yield this.isRebased(first_parent_belonts_to_pr, merge_belongs_to_pr, pull)) {
console.log("PR was merged using a rebase");
return MergeStrategy.REBASED;
}
if (yield this.isSquashed(first_parent_belonts_to_pr, merge_belongs_to_pr)) {
console.log("PR was merged using a squash");
return MergeStrategy.SQUASHED;
}
return MergeStrategy.UNKNOWN;
});
}
}
exports.Github = Github;
_Github_octokit = new WeakMap(), _Github_context = new WeakMap();
var MergeStrategy;
(function (MergeStrategy) {
MergeStrategy["SQUASHED"] = "squashed";
MergeStrategy["REBASED"] = "rebased";
MergeStrategy["MERGECOMMIT"] = "mergecommit";
MergeStrategy["UNKNOWN"] = "unknown";
})(MergeStrategy = exports.MergeStrategy || (exports.MergeStrategy = {}));


/***/ }),
Expand Down Expand Up @@ -704,12 +905,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const core = __importStar(__nccwpck_require__(2186));
const backport_1 = __nccwpck_require__(5859);
const github_1 = __nccwpck_require__(5928);
const git_1 = __nccwpck_require__(3374);
const execa_1 = __nccwpck_require__(7845);
const dedent_1 = __importDefault(__nccwpck_require__(5281));
/**
* Called from the action.yml.
*
Expand All @@ -728,12 +933,20 @@ function run() {
const copy_assignees = core.getInput("copy_assignees");
const copy_milestone = core.getInput("copy_milestone");
const copy_requested_reviewers = core.getInput("copy_requested_reviewers");
const experimental = JSON.parse(core.getInput("experimental"));
if (merge_commits != "fail" && merge_commits != "skip") {
const message = `Expected input 'merge_commits' to be either 'fail' or 'skip', but was '${merge_commits}'`;
console.error(message);
core.setFailed(message);
return;
}
for (const key in experimental) {
if (!(key in backport_1.experimentalDefaults)) {
console.warn((0, dedent_1.default) `Encountered unexpected key in input 'experimental'.\
No experimental config options known for key '${key}'.\
Please check the documentation for details about experimental features.`);
}
}
const github = new github_1.Github(token);
const git = new git_1.Git(execa_1.execa);
const config = {
Expand All @@ -746,6 +959,7 @@ function run() {
copy_assignees: copy_assignees === "true",
copy_milestone: copy_milestone === "true",
copy_requested_reviewers: copy_requested_reviewers === "true",
experimental: Object.assign(Object.assign({}, backport_1.experimentalDefaults), experimental),
};
const backport = new backport_1.Backport(github, config, git);
return backport.run();
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

0 comments on commit 32e3a3b

Please sign in to comment.