Loading ci/github-script/get-pr-commit-details.js +40 −48 Original line number Diff line number Diff line Loading @@ -23,31 +23,18 @@ 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<import('@actions/github/lib/utils').GitHub>, * pr: Awaited<ReturnType<InstanceType<import('@actions/github/lib/utils').GitHub>["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, Loading Loading @@ -75,19 +62,24 @@ async function getCommitDetailsForPR({ context, core, github, pr, repoPath }) { .filter(Boolean) return Promise.all( shas.map(async (sha) => ({ sha, message: ( shas.map(async (sha) => { const result = ( await runGit({ args: ['log', '--format=%s', '-1', sha], args: ['log', '--format=%s', '--numstat', '-1', sha], repoPath, core, quiet: true, }) ).stdout, })), ) ).stdout.split('\n') const subject = result[0] return { sha, subject, } }), ) } module.exports = { getCommitDetailsForPR } ci/github-script/lint-commits.js +6 −11 Original line number Diff line number Diff line Loading @@ -47,9 +47,7 @@ async function checkCommitMessages({ github, context, core, repoPath }) { } const commits = await getCommitDetailsForPR({ context, core, github, pr, repoPath, }) Loading @@ -57,12 +55,9 @@ 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. ' + Loading @@ -71,7 +66,7 @@ async function checkCommitMessages({ github, context, core, repoPath }) { 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.', Loading @@ -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) Loading Loading
ci/github-script/get-pr-commit-details.js +40 −48 Original line number Diff line number Diff line Loading @@ -23,31 +23,18 @@ 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<import('@actions/github/lib/utils').GitHub>, * pr: Awaited<ReturnType<InstanceType<import('@actions/github/lib/utils').GitHub>["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, Loading Loading @@ -75,19 +62,24 @@ async function getCommitDetailsForPR({ context, core, github, pr, repoPath }) { .filter(Boolean) return Promise.all( shas.map(async (sha) => ({ sha, message: ( shas.map(async (sha) => { const result = ( await runGit({ args: ['log', '--format=%s', '-1', sha], args: ['log', '--format=%s', '--numstat', '-1', sha], repoPath, core, quiet: true, }) ).stdout, })), ) ).stdout.split('\n') const subject = result[0] return { sha, subject, } }), ) } module.exports = { getCommitDetailsForPR }
ci/github-script/lint-commits.js +6 −11 Original line number Diff line number Diff line Loading @@ -47,9 +47,7 @@ async function checkCommitMessages({ github, context, core, repoPath }) { } const commits = await getCommitDetailsForPR({ context, core, github, pr, repoPath, }) Loading @@ -57,12 +55,9 @@ 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. ' + Loading @@ -71,7 +66,7 @@ async function checkCommitMessages({ github, context, core, repoPath }) { 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.', Loading @@ -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) Loading