|  | 
|  | 1 | +import { strict as assert } from 'assert'; | 
|  | 2 | +import execa from 'execa'; | 
|  | 3 | + | 
|  | 4 | +export type AddNewCommitsOptions = { | 
|  | 5 | +  mostRecentTag: string | null; | 
|  | 6 | +  repoUrl: string; | 
|  | 7 | +  loggedPrNumbers: string[]; | 
|  | 8 | +  projectRootDirectory?: string; | 
|  | 9 | +}; | 
|  | 10 | + | 
|  | 11 | +/** | 
|  | 12 | + * Get all commit hashes included in the given commit range. | 
|  | 13 | + * | 
|  | 14 | + * @param commitRange - The commit range. | 
|  | 15 | + * @param rootDirectory - The project root directory. | 
|  | 16 | + * @returns A list of commit hashes for the given range. | 
|  | 17 | + */ | 
|  | 18 | +async function getCommitHashesInRange( | 
|  | 19 | +  commitRange: string, | 
|  | 20 | +  rootDirectory?: string, | 
|  | 21 | +) { | 
|  | 22 | +  const revListArgs = ['rev-list', commitRange]; | 
|  | 23 | +  if (rootDirectory) { | 
|  | 24 | +    revListArgs.push(rootDirectory); | 
|  | 25 | +  } | 
|  | 26 | +  return await runCommand('git', revListArgs); | 
|  | 27 | +} | 
|  | 28 | + | 
|  | 29 | +/** | 
|  | 30 | + * Get commit details for each given commit hash. | 
|  | 31 | + * | 
|  | 32 | + * @param commitHashes - The list of commit hashes. | 
|  | 33 | + * @returns Commit details for each commit, including description and PR number (if present). | 
|  | 34 | + */ | 
|  | 35 | +async function getCommits(commitHashes: string[]) { | 
|  | 36 | +  const commits: { prNumber?: string; description: string }[] = []; | 
|  | 37 | +  for (const commitHash of commitHashes) { | 
|  | 38 | +    const [subject] = await runCommand('git', [ | 
|  | 39 | +      'show', | 
|  | 40 | +      '-s', | 
|  | 41 | +      '--format=%s', | 
|  | 42 | +      commitHash, | 
|  | 43 | +    ]); | 
|  | 44 | +    assert.ok( | 
|  | 45 | +      Boolean(subject), | 
|  | 46 | +      `"git show" returned empty subject for commit "${commitHash}".`, | 
|  | 47 | +    ); | 
|  | 48 | + | 
|  | 49 | +    let matchResults = subject.match(/\(#(\d+)\)/u); | 
|  | 50 | +    let prNumber: string | undefined; | 
|  | 51 | +    let description = subject; | 
|  | 52 | + | 
|  | 53 | +    if (matchResults) { | 
|  | 54 | +      // Squash & Merge: the commit subject is parsed as `<description> (#<PR ID>)` | 
|  | 55 | +      prNumber = matchResults[1]; | 
|  | 56 | +      description = subject.match(/^(.+)\s\(#\d+\)/u)?.[1] ?? ''; | 
|  | 57 | +    } else { | 
|  | 58 | +      // Merge: the PR ID is parsed from the git subject (which is of the form `Merge pull request | 
|  | 59 | +      // #<PR ID> from <branch>`, and the description is assumed to be the first line of the body. | 
|  | 60 | +      // If no body is found, the description is set to the commit subject | 
|  | 61 | +      matchResults = subject.match(/#(\d+)\sfrom/u); | 
|  | 62 | +      if (matchResults) { | 
|  | 63 | +        prNumber = matchResults[1]; | 
|  | 64 | +        const [firstLineOfBody] = await runCommand('git', [ | 
|  | 65 | +          'show', | 
|  | 66 | +          '-s', | 
|  | 67 | +          '--format=%b', | 
|  | 68 | +          commitHash, | 
|  | 69 | +        ]); | 
|  | 70 | +        description = firstLineOfBody || subject; | 
|  | 71 | +      } | 
|  | 72 | +    } | 
|  | 73 | +    // Otherwise: | 
|  | 74 | +    // Normal commits: The commit subject is the description, and the PR ID is omitted. | 
|  | 75 | + | 
|  | 76 | +    commits.push({ prNumber, description }); | 
|  | 77 | +  } | 
|  | 78 | +  return commits; | 
|  | 79 | +} | 
|  | 80 | + | 
|  | 81 | +/** | 
|  | 82 | + * Get the list of new change entries to add to a changelog. | 
|  | 83 | + * | 
|  | 84 | + * @param options - Options. | 
|  | 85 | + * @param options.mostRecentTag - The most recent tag. | 
|  | 86 | + * @param options.repoUrl - The GitHub repository URL for the current project. | 
|  | 87 | + * @param options.loggedPrNumbers - A list of all pull request numbers included in the relevant parsed changelog. | 
|  | 88 | + * @param options.projectRootDirectory - The root project directory, used to | 
|  | 89 | + * filter results from various git commands. This path is assumed to be either | 
|  | 90 | + * absolute, or relative to the current directory. Defaults to the root of the | 
|  | 91 | + * current git repository. | 
|  | 92 | + * @returns A list of new change entries to add to the changelog, based on commits made since the last release. | 
|  | 93 | + */ | 
|  | 94 | +export async function getNewChangeEntries({ | 
|  | 95 | +  mostRecentTag, | 
|  | 96 | +  repoUrl, | 
|  | 97 | +  loggedPrNumbers, | 
|  | 98 | +  projectRootDirectory, | 
|  | 99 | +}: AddNewCommitsOptions) { | 
|  | 100 | +  const commitRange = | 
|  | 101 | +    mostRecentTag === null ? 'HEAD' : `${mostRecentTag}..HEAD`; | 
|  | 102 | +  const commitsHashesSinceLastRelease = await getCommitHashesInRange( | 
|  | 103 | +    commitRange, | 
|  | 104 | +    projectRootDirectory, | 
|  | 105 | +  ); | 
|  | 106 | +  const commits = await getCommits(commitsHashesSinceLastRelease); | 
|  | 107 | + | 
|  | 108 | +  const newCommits = commits.filter( | 
|  | 109 | +    ({ prNumber }) => !prNumber || !loggedPrNumbers.includes(prNumber), | 
|  | 110 | +  ); | 
|  | 111 | + | 
|  | 112 | +  return newCommits.map(({ prNumber, description }) => { | 
|  | 113 | +    if (prNumber) { | 
|  | 114 | +      const suffix = `([#${prNumber}](${repoUrl}/pull/${prNumber}))`; | 
|  | 115 | +      return `${description} ${suffix}`; | 
|  | 116 | +    } | 
|  | 117 | +    return description; | 
|  | 118 | +  }); | 
|  | 119 | +} | 
|  | 120 | + | 
|  | 121 | +/** | 
|  | 122 | + * Executes a shell command in a child process and returns what it wrote to | 
|  | 123 | + * stdout, or rejects if the process exited with an error. | 
|  | 124 | + * | 
|  | 125 | + * @param command - The command to run, e.g. "git". | 
|  | 126 | + * @param args - The arguments to the command. | 
|  | 127 | + * @returns An array of the non-empty lines returned by the command. | 
|  | 128 | + */ | 
|  | 129 | +async function runCommand(command: string, args: string[]): Promise<string[]> { | 
|  | 130 | +  return (await execa(command, [...args])).stdout | 
|  | 131 | +    .trim() | 
|  | 132 | +    .split('\n') | 
|  | 133 | +    .filter((line) => line !== ''); | 
|  | 134 | +} | 
0 commit comments