UNPKG

backport

Version:

A CLI tool that automates the process of backporting commits

114 lines 4.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchCommitsByAuthor = void 0; const lodash_1 = require("lodash"); const generated_1 = require("../../../../graphql/generated"); const filterEmpty_1 = require("../../../../utils/filterEmpty"); const filterUnmergedCommits_1 = require("../../../../utils/filterUnmergedCommits"); const BackportError_1 = require("../../../BackportError"); const remoteConfig_1 = require("../../../remoteConfig"); const parseSourceCommit_1 = require("../../../sourceCommit/parseSourceCommit"); const graphqlClient_1 = require("../client/graphqlClient"); const fetchAuthorId_1 = require("../fetchAuthorId"); async function fetchByCommitPath({ options, authorId, commitPath, }) { const { accessToken, dateSince, dateUntil, githubApiBaseUrlV4 = 'https://api.github.com/graphql', maxNumber = 10, repoName, repoOwner, sourceBranch, } = options; const query = (0, generated_1.graphql)(` query CommitsByAuthor( $authorId: ID $commitPath: String $dateSince: GitTimestamp $dateUntil: GitTimestamp $maxNumber: Int! $repoName: String! $repoOwner: String! $sourceBranch: String! ) { repository(owner: $repoOwner, name: $repoName) { ref(qualifiedName: $sourceBranch) { target { ... on Commit { __typename history( first: $maxNumber author: { id: $authorId } path: $commitPath since: $dateSince until: $dateUntil ) { edges { node { __typename ...SourceCommitWithTargetPullRequestFragment } } } } } } } } `); const variables = { repoOwner, repoName, sourceBranch, maxNumber, authorId, commitPath, dateSince, dateUntil, }; const client = (0, graphqlClient_1.getGraphQLClient)({ accessToken, githubApiBaseUrlV4 }); const result = await client.query(query, variables); if (result.error) { if (result.statusCode === 502 && maxNumber > 50) { throw new BackportError_1.BackportError(`The GitHub API returned a 502 error. Try reducing the number of commits to display: "--max-number 20"`); } if (!(0, remoteConfig_1.isMissingConfigFileException)(result)) { throw new graphqlClient_1.GithubV4Exception(result); } } return result.data; } async function fetchCommitsByAuthor(options) { const { sourceBranch, commitPaths = [] } = options; const authorId = await (0, fetchAuthorId_1.fetchAuthorId)(options); const responses = (await Promise.all((0, lodash_1.isEmpty)(commitPaths) ? [fetchByCommitPath({ options, authorId, commitPath: null })] : commitPaths.map((commitPath) => fetchByCommitPath({ options, authorId, commitPath })))).filter(filterEmpty_1.filterNil); // we only need to check if the first item is `null` (if the first is `null` they all are) if ((0, lodash_1.first)(responses)?.repository?.ref === null) { throw new BackportError_1.BackportError(`The upstream branch "${sourceBranch}" does not exist. Try specifying a different branch with "--source-branch <your-branch>"`); } const commits = responses .flatMap((res) => { const repoRefTarget = res.repository?.ref?.target; if (repoRefTarget?.__typename !== 'Commit') { return; } return repoRefTarget.history.edges?.map((edge) => { const sourceCommit = edge?.node; if (sourceCommit) { return (0, parseSourceCommit_1.parseSourceCommit)({ options, sourceCommit }); } }); }) .filter(filterEmpty_1.filterNil); // terminate if not commits were found if ((0, lodash_1.isEmpty)(commits)) { const pathText = commitPaths.length > 0 ? ` touching files in path: "${commitPaths}"` : ''; const errorText = options.author ? `There are no commits by "${options.author}" in this repository${pathText}. Try with \`--all\` for commits by all users or \`--author=<username>\` for commits from a specific user` : `There are no commits in this repository${pathText}`; throw new BackportError_1.BackportError(errorText); } const commitsUnique = (0, lodash_1.uniqBy)(commits, (c) => c.sourceCommit.sha); const commitsSorted = (0, lodash_1.orderBy)(commitsUnique, (c) => c.sourceCommit.committedDate, 'desc'); if (options.onlyMissing) { return commitsSorted.filter(filterUnmergedCommits_1.filterUnmergedCommits); } return commitsSorted; } exports.fetchCommitsByAuthor = fetchCommitsByAuthor; //# sourceMappingURL=fetchCommitsByAuthor.js.map