Unverified Commit d03b81d6 authored by Michael Daniels's avatar Michael Daniels
Browse files

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

parent 743ab295
Loading
Loading
Loading
Loading
+40 −48
Original line number Diff line number Diff line
@@ -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,
@@ -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 }
+6 −11
Original line number Diff line number Diff line
@@ -47,9 +47,7 @@ async function checkCommitMessages({ github, context, core, repoPath }) {
  }

  const commits = await getCommitDetailsForPR({
    context,
    core,
    github,
    pr,
    repoPath,
  })
@@ -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. ' +
@@ -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.',
@@ -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)