Skip to content

Commit

Permalink
fix: add pull request numbers to all rebased commits (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Feb 7, 2023
1 parent 1c2e3da commit f283d4a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/release-please/changelog.js
Expand Up @@ -19,7 +19,7 @@ module.exports = class ChangelogNotes {
}

// A link to the pull request if the commit has one
const prNumber = commit.pullRequest && commit.pullRequest.number
const prNumber = commit.pullRequest?.number
if (prNumber) {
entry.push(link(`#${prNumber}`, this.gh.pull(prNumber)))
}
Expand Down Expand Up @@ -63,7 +63,16 @@ module.exports = class ChangelogNotes {

// Group commits by type
for (const commit of commits) {
const { entry, breaking } = this.buildEntry(commit, authorsByCommit[commit.sha])
// when rebase merging multiple commits with a single PR, only the first commit
// will have a pr number when coming from release-please. this check will manually
// lookup commits without a pr number and find one if it exists
if (!commit.pullRequest?.number) {
commit.pullRequest = { number: await this.gh.commitPrNumber(commit) }
}
const { entry, breaking } = this.buildEntry(
commit,
authorsByCommit[commit.sha]
)

// Collect commits by type
changelog[commit.type].entries.push(entry)
Expand Down
15 changes: 15 additions & 0 deletions lib/release-please/github.js
Expand Up @@ -45,10 +45,25 @@ module.exports = (gh) => {
}
}

const commitPrNumber = async (commit) => {
try {
const res = await gh.octokit.rest.repos.listPullRequestsAssociatedWithCommit({
owner,
repo,
commit_sha: commit.sha,
per_page: 1,
})
return res.data[0].number
} catch {
return null
}
}

const url = (...p) => `https://github.com/${owner}/${repo}/${p.join('/')}`

return {
authors,
commitPrNumber,
pull: (number) => url('pull', number),
commit: (sha) => url('commit', sha),
compare: (a, b) => a ? url('compare', `${a.toString()}...${b.toString()}`) : null,
Expand Down
19 changes: 17 additions & 2 deletions test/release-please/changelog.js
Expand Up @@ -46,7 +46,21 @@ const mockChangelog = async ({ shas = true, authors = true, previousTag = true }
return acc
}, {}),
}),

octokit: {
rest: {
repos: {
listPullRequestsAssociatedWithCommit: async (commit) => {
if (commit.commit_sha === 'd') {
return {
data: [{
number: 50,
}],
}
}
},
},
},
},
}

const changelog = new ChangelogNotes({ github })
Expand Down Expand Up @@ -75,7 +89,8 @@ t.test('changelog', async t => {
// eslint-disable-next-line max-len
'* [`b`](https://github.com/npm/cli/commit/b) [#100](https://github.com/npm/cli/pull/100) b (@username)',
'### Bug Fixes',
'* [`d`](https://github.com/npm/cli/commit/d) this fixes it',
// eslint-disable-next-line max-len
'* [`d`](https://github.com/npm/cli/commit/d) [#50](https://github.com/npm/cli/pull/50) this fixes it',
'### Dependencies',
'* [`c`](https://github.com/npm/cli/commit/c) `test@1.2.3`',
])
Expand Down

0 comments on commit f283d4a

Please sign in to comment.