- identify merged PRs given the mergeCommitSha

- do not solely go via merge commits as not all merges will have these
This commit is contained in:
Mike Penz
2020-10-29 12:06:32 +01:00
parent eea7b77d97
commit a1b4e555e4
3 changed files with 15 additions and 42 deletions
-1
View File
@@ -8,7 +8,6 @@ export interface CommitInfo {
message: string message: string
author: string author: string
date: moment.Moment date: moment.Moment
prNumber: number | undefined
} }
export class Commits { export class Commits {
+6 -7
View File
@@ -9,6 +9,7 @@ export interface PullRequestInfo {
title: string title: string
htmlURL: string htmlURL: string
mergedAt: moment.Moment mergedAt: moment.Moment
mergeCommitSha: string
author: string author: string
repoName: string repoName: string
labels: string[] labels: string[]
@@ -38,6 +39,7 @@ export class PullRequests {
title: pr.data.title, title: pr.data.title,
htmlURL: pr.data.html_url, htmlURL: pr.data.html_url,
mergedAt: moment(pr.data.merged_at), mergedAt: moment(pr.data.merged_at),
mergeCommitSha: pr.data.merge_commit_sha,
author: pr.data.user.login, author: pr.data.user.login,
repoName: pr.data.base.repo.full_name, repoName: pr.data.base.repo.full_name,
labels: pr.data.labels.map(function (label) { labels: pr.data.labels.map(function (label) {
@@ -88,6 +90,7 @@ export class PullRequests {
title: pr.title, title: pr.title,
htmlURL: pr.html_url, htmlURL: pr.html_url,
mergedAt: moment(pr.merged_at), mergedAt: moment(pr.merged_at),
mergeCommitSha: pr.merge_commit_sha,
author: pr.user.login, author: pr.user.login,
repoName: pr.base.repo.full_name, repoName: pr.base.repo.full_name,
labels: pr.labels?.map(function (label) { labels: pr.labels?.map(function (label) {
@@ -121,11 +124,13 @@ export class PullRequests {
return sortPullRequests(mergedPRs, true) return sortPullRequests(mergedPRs, true)
} }
/**
* Filters out all commits which match the exclude pattern
*/
filterCommits( filterCommits(
commits: CommitInfo[], commits: CommitInfo[],
excludeMergeBranches: string[] excludeMergeBranches: string[]
): CommitInfo[] { ): CommitInfo[] {
const prRegex = /Merge pull request #(\d+)/
const filteredCommits = [] const filteredCommits = []
for (const commit of commits) { for (const commit of commits) {
@@ -141,12 +146,6 @@ export class PullRequests {
continue continue
} }
} }
const match = commit.summary.match(prRegex)
if (!match) {
continue
}
commit.prNumber = Number.parseInt(match[1], 10)
filteredCommits.push(commit) filteredCommits.push(commit)
} }
+9 -34
View File
@@ -107,42 +107,17 @@ export class ReleaseNotes {
) )
core.info( core.info(
`️ Retrieved ${prCommits.length} PR merge commits for ${owner}/${repo}` `️ Retrieved ${prCommits.length} release commits for ${owner}/${repo}`
) )
const filteredPullRequests = [] // create array of commits for this release
const pullRequestsByNumber: {[key: number]: PullRequestInfo} = {} const releaseCommitHashes = prCommits.map(commmit => {
return commmit.sha
})
for (const pr of pullRequests) { // return only the pull requests associated with this release
pullRequestsByNumber[pr.number] = pr return pullRequests.filter(pr => {
} return releaseCommitHashes.includes(pr.mergeCommitSha)
})
for (const commit of prCommits) {
if (!commit.prNumber) {
continue
}
const prRef = `${owner}/${repo}#${commit.prNumber}`
if (pullRequestsByNumber[commit.prNumber]) {
filteredPullRequests.push(pullRequestsByNumber[commit.prNumber])
} else if (fromDate.toISOString() === toDate.toISOString()) {
const pullRequest = await pullRequestsApi.getSingle(
owner,
repo,
commit.prNumber
)
if (pullRequest) {
filteredPullRequests.push(pullRequest)
} else {
core.warning(`⚠️ ${prRef} not found! Commit text: ${commit.summary}`)
}
} else {
core.info(`${prRef} not in date range, excluding from changelog`)
}
}
return filteredPullRequests
} }
} }