Skip to content

Commit

Permalink
ci: add PR co-authors to contributors section of release notes
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Dec 7, 2024
1 parent f09ee0b commit e236c28
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 11 deletions.
12 changes: 8 additions & 4 deletions scripts/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,21 @@ async function main() {

// Preview/Update changelog
header(`${logPrefix}📝 Updating changelog...`)
const { changelog: changelogContent, releaseUrl } = await generateReleaseNotes({
const {
changelog: changelogContent,
releaseUrl,
releaseNotes,
} = await generateReleaseNotes({
bump,
dryRun,
toVersion: 'HEAD',
fromVersion,
openReleaseUrl: true,
})

console.log(chalk.green('\nChangelog Preview:\n'))
console.log(chalk.gray(changelogContent) + '\n\n')
console.log(`Release URL: ${chalk.dim(releaseUrl)}`)
console.log(chalk.green('\nFull Release Notes:\n\n'))
console.log(chalk.gray(releaseNotes) + '\n\n')
console.log(`\n\nRelease URL: ${chalk.dim(releaseUrl)}`)

let packageDetails = await getPackageDetails(packagePublishList)

Expand Down
79 changes: 72 additions & 7 deletions scripts/utils/generateReleaseNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const generateReleaseNotes = async (args: Args = {}): Promise<ChangelogRe
// Helper functions

async function createContributorSection(commits: GitCommit[]): Promise<string> {
console.log('Fetching contributors...')
const contributors = await getContributors(commits)
if (!contributors.length) {
return ''
Expand All @@ -179,18 +180,21 @@ async function getContributors(commits: GitCommit[]): Promise<Contributor[]> {
const contributors: Contributor[] = []
const emails = new Set<string>()

const headers = {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${process.env.GITHUB_TOKEN}`,
}

for (const commit of commits) {
if (emails.has(commit.author.email) || commit.author.name === 'dependabot[bot]') {
console.log(`Fetching details for ${commit.message} - ${commit.shortHash}`)
if (emails.has(commit.author.email) || commit.author.name.includes('[bot]')) {
continue
}

const res = await fetch(
`https://api.github.com/repos/payloadcms/payload/commits/${commit.shortHash}`,
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
headers,
},
)

Expand All @@ -202,12 +206,73 @@ async function getContributors(commits: GitCommit[]): Promise<Contributor[]> {

const { author } = (await res.json()) as { author: { login: string; email: string } }

// TODO: Handle co-authors

if (!contributors.some((c) => c.username === author.login)) {
contributors.push({ name: commit.author.name, username: author.login })
}
emails.add(author.email)

// Check git commit for 'Co-authored-by:' lines
const coAuthorPattern = /Co-authored-by: (?<name>[^<]+) <(?<email>[^>]+)>/g
const coAuthors = Array.from(
commit.body.matchAll(coAuthorPattern),
(match) => match.groups,
).filter((e) => !e.email.includes('[bot]'))

if (!coAuthors.length) {
continue
}

console.log(
`Fetching co-author details for hash: ${commit.shortHash}. Co-authors:`,
coAuthors.map((c) => c.email).join(', '),
)

// Attempt to co-authors by email
await Promise.all(
(coAuthors || [])
.map(async ({ name, email }) => {
// Check if this co-author has already been added
if (emails.has(email)) {
return null
}

// Get co-author's GitHub username by email
try {
const response = await fetch(
`https://api.github.com/search/users?q=${encodeURIComponent(email)}+in:email`,
{
headers,
},
)

if (!response.ok) {
console.log('Bad response from GitHub API fetching co-author by email')
console.error(response.status)
return null
}

const data = (await response.json()) as { items?: { login: string }[] }
const user = data.items?.[0]

if (!user) {
return null
}

console.log(`Found co-author by email: ${user.login}`)

if (!contributors.some((c) => c.username === user.login)) {
contributors.push({ name, username: user.login })
}
emails.add(email)
return user.login
} catch (error) {
console.log(`ERROR: Failed to fetch co-author by email`)
console.error(error)
return null
}
})
.filter(Boolean),
)
}
return contributors
}
Expand Down

0 comments on commit e236c28

Please sign in to comment.