UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

82 lines (81 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchCommitsForRebaseAndMergeStrategy = void 0; const lodash_1 = require("lodash"); const generated_1 = require("../../../../graphql/generated"); const graphqlClient_1 = require("../client/graphqlClient"); const fetchCommitBySha_1 = require("./fetchCommitBySha"); async function fetchCommitsForRebaseAndMergeStrategy(options, commitsTotalCount) { const { accessToken, githubApiBaseUrlV4 = 'https://api.github.com/graphql', pullNumber, repoName, repoOwner, } = options; const query = (0, generated_1.graphql)(` query CommitsForRebaseAndMergeStrategy( $repoOwner: String! $repoName: String! $pullNumber: Int! $commitsTotalCount: Int! ) { repository(owner: $repoOwner, name: $repoName) { pullRequest(number: $pullNumber) { number commits(first: $commitsTotalCount) { totalCount edges { node { commit { message } } } } mergeCommit { committedDate history(first: $commitsTotalCount) { edges { node { oid message committedDate associatedPullRequests(first: 1) { edges { node { number } } } } } } } } } } `); const variables = { repoOwner, repoName, pullNumber, commitsTotalCount }; const client = (0, graphqlClient_1.getGraphQLClient)({ accessToken, githubApiBaseUrlV4 }); const result = await client.query(query, variables); if (result.error) { throw new graphqlClient_1.GithubV4Exception(result); } const pullRequestNode = result.data?.repository?.pullRequest; if (!pullRequestNode?.mergeCommit) { throw new Error('Pull request is not merged'); } if (pullRequestNode.commits.totalCount !== commitsTotalCount) { throw new Error(`Specified number of commits is ${commitsTotalCount} whereas the actual number is ${pullRequestNode.commits.totalCount}`); } const commitsInPullRequest = pullRequestNode.commits.edges ?? []; const commitsInBaseBranch = pullRequestNode.mergeCommit.history.edges?.reverse() ?? []; const didUseRebaseAndMergeStrategy = commitsInBaseBranch.every((c, i) => { const hasSameCommittedDate = c?.node?.committedDate === pullRequestNode.mergeCommit?.committedDate; const hasSameCommitMessages = c?.node?.message === commitsInPullRequest[i]?.node?.commit.message; const hasSamePullNumber = (0, lodash_1.first)(c?.node?.associatedPullRequests?.edges)?.node?.number === pullRequestNode.number; return hasSameCommittedDate && hasSameCommitMessages && hasSamePullNumber; }); if (didUseRebaseAndMergeStrategy) { const commits = await Promise.all(commitsInBaseBranch.map((c) => (0, fetchCommitBySha_1.fetchCommitBySha)({ ...options, sha: c?.node?.oid }))); return commits; } } exports.fetchCommitsForRebaseAndMergeStrategy = fetchCommitsForRebaseAndMergeStrategy; //# sourceMappingURL=fetchCommitsForRebaseAndMergeStrategy.js.map