backport
Version:
A CLI tool that automates the process of backporting commits
155 lines (150 loc) • 5.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SourceCommitWithTargetPullRequestFragment = exports.parseSourceCommit = void 0;
const lodash_1 = require("lodash");
const generated_1 = require("../../graphql/generated");
const filterEmpty_1 = require("../../utils/filterEmpty");
const remoteConfig_1 = require("../remoteConfig");
const getPullRequestStates_1 = require("./getPullRequestStates");
const getSourcePullRequest_1 = require("./getSourcePullRequest");
function getSuggestedTargetBranches(sourceCommit, targetPullRequestStates, branchLabelMapping) {
const missingPrs = (0, getPullRequestStates_1.getPullRequestStates)({
sourceCommit,
branchLabelMapping,
}).filter((pr) => pr.state === 'NOT_CREATED' || pr.state === 'CLOSED');
const mergedPrs = targetPullRequestStates.filter((pr) => pr.state === 'MERGED');
return (0, lodash_1.differenceBy)(missingPrs, mergedPrs, (pr) => pr.label).map((pr) => pr.branch);
}
function parseSourceCommit({ sourceCommit, options, }) {
const sourcePullRequest = (0, getSourcePullRequest_1.getSourcePullRequest)(sourceCommit);
const sourceCommitBranchLabelMapping = getSourceCommitBranchLabelMapping(sourceCommit);
const branchLabelMapping = sourceCommitBranchLabelMapping ?? options.branchLabelMapping;
const targetPullRequestStates = (0, getPullRequestStates_1.getPullRequestStates)({
sourceCommit,
branchLabelMapping,
});
const suggestedTargetBranches = getSuggestedTargetBranches(sourceCommit, targetPullRequestStates, options.branchLabelMapping);
return {
author: sourceCommit.author,
sourceCommit: {
committedDate: sourceCommit.committedDate,
message: sourceCommit.message,
sha: sourceCommit.sha,
branchLabelMapping: sourceCommitBranchLabelMapping,
},
sourcePullRequest: sourcePullRequest
? {
labels: sourcePullRequest.labels?.nodes
?.map((label) => label?.name)
.filter(filterEmpty_1.filterNil) ?? [],
title: sourcePullRequest.title,
number: sourcePullRequest.number,
url: sourcePullRequest.url,
mergeCommit: sourcePullRequest.mergeCommit
? {
message: sourcePullRequest.mergeCommit.message,
sha: sourcePullRequest.mergeCommit.sha,
}
: undefined,
}
: undefined,
sourceBranch: sourcePullRequest?.baseRefName ?? options.sourceBranch,
suggestedTargetBranches,
targetPullRequestStates: targetPullRequestStates,
};
}
exports.parseSourceCommit = parseSourceCommit;
exports.SourceCommitWithTargetPullRequestFragment = (0, generated_1.graphql)(`
fragment SourceCommitWithTargetPullRequestFragment on Commit {
__typename
# Source Commit
repository {
name
owner {
login
}
}
sha: oid
message
committedDate
author {
name
email
}
# Source pull request: PR where source commit was merged in
associatedPullRequests(first: 1) {
edges {
node {
title
url
number
labels(first: 50) {
nodes {
name
}
}
baseRefName
# source merge commit (the commit that actually went into the source branch)
mergeCommit {
__typename
...RemoteConfigHistoryFragment
sha: oid
message
}
# (possible) backport pull requests referenced in the source pull request
timelineItems(last: 20, itemTypes: CROSS_REFERENCED_EVENT) {
edges {
node {
... on CrossReferencedEvent {
__typename
targetPullRequest: source {
__typename
# Target PRs (backport PRs)
... on PullRequest {
__typename
# target merge commit: the backport commit that was merged into the target branch
targetMergeCommit: mergeCommit {
sha: oid
message
}
repository {
name
owner {
login
}
}
url
title
state
baseRefName
number
commits(first: 20) {
edges {
node {
targetCommit: commit {
message
sha: oid
}
}
}
}
}
}
}
}
}
}
}
}
}
}
`);
function getSourceCommitBranchLabelMapping(sourceCommit) {
const sourcePullRequest = (0, getSourcePullRequest_1.getSourcePullRequest)(sourceCommit);
const remoteConfig = sourcePullRequest?.mergeCommit?.remoteConfigHistory.edges?.[0]
?.remoteConfig;
if (remoteConfig) {
return (0, remoteConfig_1.parseRemoteConfigFile)(remoteConfig)?.branchLabelMapping;
}
}
//# sourceMappingURL=parseSourceCommit.js.map