diff --git a/ci/github-script/get-pr-commit-details.js b/ci/github-script/get-pr-commit-details.js index 07ac10faa44f..c9349fa7e055 100644 --- a/ci/github-script/get-pr-commit-details.js +++ b/ci/github-script/get-pr-commit-details.js @@ -23,71 +23,63 @@ async function runGit({ args, repoPath, core, quiet }) { } /** - * GitHub's API will return a maximum of 250 commits. - * We will use it if we can, but fall back to using git locally. - * * @param {{ - * context: import('@actions/github/lib/context').Context, * core: import('@actions/core'), - * github: InstanceType, * pr: Awaited["rest"]["pulls"]["get"]>>["data"] * repoPath?: string, * }} GetCommitMessagesForPRProps * * @returns {Promise<{ - * message: string, + * subject: string, * sha: string, * }[]>} */ -async function getCommitDetailsForPR({ context, core, github, pr, repoPath }) { - if (pr.commits < 250) { - return ( - await github.paginate(github.rest.pulls.listCommits, { - ...context.repo, - pull_number: pr.number, - }) - ).map((commit) => ({ message: commit.commit.message, sha: commit.sha })) - } else { +async function getCommitDetailsForPR({ core, pr, repoPath }) { + await runGit({ + args: ['fetch', `--depth=1`, 'origin', pr.base.sha], + repoPath, + core, + }) + await runGit({ + args: ['fetch', `--depth=${pr.commits + 1}`, 'origin', pr.head.sha], + repoPath, + core, + }) + + const shas = ( await runGit({ - args: ['fetch', `--depth=1`, 'origin', pr.base.sha], - repoPath, - core, - }) - await runGit({ - args: ['fetch', `--depth=${pr.commits + 1}`, 'origin', pr.head.sha], + args: [ + 'rev-list', + `--max-count=${pr.commits}`, + `${pr.base.sha}..${pr.head.sha}`, + ], repoPath, core, }) + ).stdout + .split('\n') + .map((s) => s.trim()) + .filter(Boolean) - const shas = ( - await runGit({ - args: [ - 'rev-list', - `--max-count=${pr.commits}`, - `${pr.base.sha}..${pr.head.sha}`, - ], - repoPath, - core, - }) - ).stdout - .split('\n') - .map((s) => s.trim()) - .filter(Boolean) + return Promise.all( + shas.map(async (sha) => { + const result = ( + await runGit({ + args: ['log', '--format=%s', '--numstat', '-1', sha], + repoPath, + core, + quiet: true, + }) + ).stdout.split('\n') - return Promise.all( - shas.map(async (sha) => ({ + const subject = result[0] + + return { sha, - message: ( - await runGit({ - args: ['log', '--format=%s', '-1', sha], - repoPath, - core, - quiet: true, - }) - ).stdout, - })), - ) - } + subject, + } + }), + ) } module.exports = { getCommitDetailsForPR } diff --git a/ci/github-script/lint-commits.js b/ci/github-script/lint-commits.js index ddc422f9114c..bba4cb9ec99a 100644 --- a/ci/github-script/lint-commits.js +++ b/ci/github-script/lint-commits.js @@ -47,9 +47,7 @@ async function checkCommitMessages({ github, context, core, repoPath }) { } const commits = await getCommitDetailsForPR({ - context, core, - github, pr, repoPath, }) @@ -57,21 +55,18 @@ async function checkCommitMessages({ github, context, core, repoPath }) { const failures = new Set() for (const commit of commits) { - const message = commit.message - const firstLine = message.split('\n')[0] + const logMsgStart = `Commit ${commit.sha}'s message's subject ("${commit.subject}")` - const logMsgStart = `Commit ${commit.sha}'s message's subject ("${firstLine}")` - - if (!firstLine.includes(': ')) { + if (!commit.subject.includes(': ')) { core.error( `${logMsgStart} was detected as not meeting our guidelines because ` + - 'it does not contain a colon followed by a whitespace.' + + 'it does not contain a colon followed by a whitespace. ' + 'There are likely other issues as well.', ) failures.add(commit.sha) } - if (firstLine.endsWith('.')) { + if (commit.subject.endsWith('.')) { core.error( `${logMsgStart} was detected as not meeting our guidelines because ` + 'it ends in a period. There may be other issues as well.', @@ -80,10 +75,10 @@ async function checkCommitMessages({ github, context, core, repoPath }) { } const fixups = ['amend!', 'fixup!', 'squash!'] - if (fixups.some((s) => firstLine.startsWith(s))) { + if (fixups.some((s) => commit.subject.startsWith(s))) { core.error( `${logMsgStart} was detected as not meeting our guidelines because ` + - `it begins with "${fixups.find((s) => firstLine.startsWith(s))}". ` + + `it begins with "${fixups.find((s) => commit.subject.startsWith(s))}". ` + 'Did you forget to run `git rebase -i --autosquash`?', ) failures.add(commit.sha)