ci/github-script: don't use GH API when getting commit info, return only subject

This commit is contained in:
Michael Daniels
2026-03-12 18:04:32 -04:00
parent 743ab29528
commit d03b81d689
2 changed files with 46 additions and 59 deletions
+40 -48
View File
@@ -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<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,
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 }
+6 -11
View File
@@ -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)