backport
Version:
A CLI tool that automates the process of backporting commits
80 lines (78 loc) • 3.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchCommitsByPullNumber = void 0;
const generated_1 = require("../../../../graphql/generated");
const BackportError_1 = require("../../../BackportError");
const remoteConfig_1 = require("../../../remoteConfig");
const graphqlClient_1 = require("../client/graphqlClient");
const fetchCommitBySha_1 = require("./fetchCommitBySha");
const fetchCommitsForRebaseAndMergeStrategy_1 = require("./fetchCommitsForRebaseAndMergeStrategy");
async function fetchCommitsByPullNumber(options) {
const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', pullNumber, repoName, repoOwner, } = options;
const query = (0, generated_1.graphql)(`
query CommitByPullNumber(
$repoOwner: String!
$repoName: String!
$pullNumber: Int!
) {
repository(owner: $repoOwner, name: $repoName) {
pullRequest(number: $pullNumber) {
# used to determine if "Rebase and Merge" strategy was used
commits(last: 1) {
totalCount
edges {
node {
commit {
message
}
}
}
}
mergeCommit {
oid
# used to determine if "Rebase and Merge" strategy was used
committedDate
history(first: 2) {
edges {
node {
message
committedDate
}
}
}
}
}
}
}
`);
const variables = { repoOwner, repoName, pullNumber };
const client = (0, graphqlClient_1.getGraphQLClient)({ accessToken, githubApiBaseUrlV4 });
const result = await client.query(query, variables);
if (result.error && !(0, remoteConfig_1.isMissingConfigFileException)(result)) {
throw new graphqlClient_1.GithubV4Exception(result);
}
const { data } = result;
const pullRequestNode = data?.repository?.pullRequest;
if (!pullRequestNode) {
throw new BackportError_1.BackportError(`The PR #${pullNumber} does not exist`);
}
const { mergeCommit } = pullRequestNode;
if (mergeCommit === null) {
throw new BackportError_1.BackportError(`The PR #${pullNumber} is not merged`);
}
const lastCommitInPullRequest = pullRequestNode.commits.edges?.[0]?.node?.commit;
const firstCommitInBaseBranch = mergeCommit?.history.edges?.[0]?.node;
const isRebaseAndMergeStrategy = pullRequestNode.commits.totalCount > 0 &&
mergeCommit?.history.edges?.every((c) => c?.node?.committedDate === mergeCommit.committedDate) &&
lastCommitInPullRequest?.message === firstCommitInBaseBranch?.message;
if (isRebaseAndMergeStrategy) {
const commits = await (0, fetchCommitsForRebaseAndMergeStrategy_1.fetchCommitsForRebaseAndMergeStrategy)(options, pullRequestNode.commits.totalCount);
if (commits) {
return commits;
}
}
const commit = await (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha: mergeCommit?.oid });
return [commit];
}
exports.fetchCommitsByPullNumber = fetchCommitsByPullNumber;
//# sourceMappingURL=fetchCommitByPullNumber.js.map