Skip to content

Commit

Permalink
update optic link in the issue comment (#129)
Browse files Browse the repository at this point in the history
* update optic link in the issue comment

* increase threshold for coverage

* omit npm link from comment if not relevant
  • Loading branch information
denes-fekeshazy authored Jun 24, 2022
1 parent ba838a0 commit 6e687ad
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .taprc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
statements: 80
lines: 80
functions: 75
branches: 80
statements: 90
lines: 90
functions: 78
branches: 90
53 changes: 46 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20007,7 +20007,9 @@ module.exports = async function ({ github, context, inputs }) {
try {
// post a comment about release on npm to any linked issues in the
// any of the PRs in this release
await notifyIssues(github, owner, repo, release)
const shouldPostNpmLink = Boolean(inputs['npm-token'])

await notifyIssues(github, shouldPostNpmLink, owner, repo, release)
} catch (err) {
logWarning('Failed to notify any/all issues')
logError(err)
Expand Down Expand Up @@ -20124,7 +20126,43 @@ async function getLinkedIssueNumbers(github, prNumber, repoOwner, repoName) {
return linkedIssues.map(issue => issue.number)
}

async function notifyIssues(githubClient, owner, repo, release) {
function createCommentBody(
shouldPostNpmLink,
packageName,
packageVersion,
releaseUrl
) {
const npmUrl = `https://www.npmjs.com/package/${packageName}/v/${packageVersion}`

if (shouldPostNpmLink) {
return `🎉 This issue has been resolved in version ${packageVersion} 🎉


The release is available on:
* [npm package](${npmUrl})
* [GitHub release](${releaseUrl})


Your **[optic](https://github.com/nearform/optic-release-automation-action)** bot 📦🚀`
}

return `🎉 This issue has been resolved in version ${packageVersion} 🎉


The release is available on:
* [GitHub release](${releaseUrl})


Your **[optic](https://github.com/nearform/optic-release-automation-action)** bot 📦🚀`
}

async function notifyIssues(
githubClient,
shouldPostNpmLink,
owner,
repo,
release
) {
const packageJsonFile = fs.readFileSync('./package.json', 'utf8')
const packageJson = JSON.parse(packageJsonFile)

Expand All @@ -20139,11 +20177,12 @@ async function notifyIssues(githubClient, owner, repo, release) {
)
).flat()

const npmUrl = `https://www.npmjs.com/package/${packageName}/v/${packageVersion}`

const body = `🎉 This issue has been resolved in version ${packageVersion} 🎉 \n\n
The release is available on: \n * [npm package](${npmUrl}) \n
* [GitHub release](${releaseUrl}) \n\n Your **[optic](https://github.com/nearform/optic)** bot 📦🚀`
const body = createCommentBody(
shouldPostNpmLink,
packageName,
packageVersion,
releaseUrl
)

await pMap(
issueNumbersToNotify,
Expand Down
4 changes: 3 additions & 1 deletion src/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ module.exports = async function ({ github, context, inputs }) {
try {
// post a comment about release on npm to any linked issues in the
// any of the PRs in this release
await notifyIssues(github, owner, repo, release)
const shouldPostNpmLink = Boolean(inputs['npm-token'])

await notifyIssues(github, shouldPostNpmLink, owner, repo, release)
} catch (err) {
logWarning('Failed to notify any/all issues')
logError(err)
Expand Down
49 changes: 43 additions & 6 deletions src/utils/notifyIssues.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,43 @@ async function getLinkedIssueNumbers(github, prNumber, repoOwner, repoName) {
return linkedIssues.map(issue => issue.number)
}

async function notifyIssues(githubClient, owner, repo, release) {
function createCommentBody(
shouldPostNpmLink,
packageName,
packageVersion,
releaseUrl
) {
const npmUrl = `https://www.npmjs.com/package/${packageName}/v/${packageVersion}`

if (shouldPostNpmLink) {
return `🎉 This issue has been resolved in version ${packageVersion} 🎉
The release is available on:
* [npm package](${npmUrl})
* [GitHub release](${releaseUrl})
Your **[optic](https://github.com/nearform/optic-release-automation-action)** bot 📦🚀`
}

return `🎉 This issue has been resolved in version ${packageVersion} 🎉
The release is available on:
* [GitHub release](${releaseUrl})
Your **[optic](https://github.com/nearform/optic-release-automation-action)** bot 📦🚀`
}

async function notifyIssues(
githubClient,
shouldPostNpmLink,
owner,
repo,
release
) {
const packageJsonFile = fs.readFileSync('./package.json', 'utf8')
const packageJson = JSON.parse(packageJsonFile)

Expand All @@ -54,11 +90,12 @@ async function notifyIssues(githubClient, owner, repo, release) {
)
).flat()

const npmUrl = `https://www.npmjs.com/package/${packageName}/v/${packageVersion}`

const body = `🎉 This issue has been resolved in version ${packageVersion} 🎉 \n\n
The release is available on: \n * [npm package](${npmUrl}) \n
* [GitHub release](${releaseUrl}) \n\n Your **[optic](https://github.com/nearform/optic)** bot 📦🚀`
const body = createCommentBody(
shouldPostNpmLink,
packageName,
packageVersion,
releaseUrl
)

await pMap(
issueNumbersToNotify,
Expand Down
75 changes: 70 additions & 5 deletions test/notifyIssues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ tap.test('Should not call createComment if no linked issues', async () => {

const release = { body: releaseNotes, html_url: 'some_url' }

await notifyIssues(DEFAULT_GITHUB_CLIENT, 'owner', 'repo', release)
await notifyIssues(DEFAULT_GITHUB_CLIENT, false, 'owner', 'repo', release)

sinon.assert.notCalled(createCommentStub)
})

tap.test(
'Should call createComment with correct arguments for linked issues',
'Should call createComment with correct arguments for linked issues with npm link',
async () => {
const { notifyIssues } = setup()

Expand All @@ -86,14 +86,79 @@ tap.test(

await notifyIssues(
{ ...DEFAULT_GITHUB_CLIENT, graphql: graphqlStub },
true,
'owner',
'repo',
release
)

const expectedCommentBody = `🎉 This issue has been resolved in version 1.0.0 🎉 \n\n
The release is available on: \n * [npm package](https://www.npmjs.com/package/packageName/v/1.0.0) \n
* [GitHub release](some_url) \n\n Your **[optic](https://github.com/nearform/optic)** bot 📦🚀`
const expectedCommentBody = `🎉 This issue has been resolved in version 1.0.0 🎉
The release is available on:
* [npm package](https://www.npmjs.com/package/packageName/v/1.0.0)
* [GitHub release](some_url)
Your **[optic](https://github.com/nearform/optic-release-automation-action)** bot 📦🚀`

sinon.assert.calledWith(createCommentStub, {
owner: 'owner',
repo: 'repo',
issue_number: '10',
body: expectedCommentBody,
})

sinon.assert.calledWith(createCommentStub, {
owner: 'owner',
repo: 'repo',
issue_number: '15',
body: expectedCommentBody,
})
}
)

tap.test(
'Should call createComment with correct arguments for linked issues without npm link',
async () => {
const { notifyIssues } = setup()

const releaseNotes = `
## What's Changed\n +
* chore 15 by @people in https://github.com/owner/repo/pull/13\n
\n
\n
**Full Changelog**: https://github.com/owner/repo/compare/v1.0.20...v1.1.0
`

const release = { body: releaseNotes, html_url: 'some_url' }

const graphqlStub = sinon.stub().resolves({
repository: {
pullRequest: {
closingIssuesReferences: {
nodes: [{ number: '10' }, { number: '15' }],
},
},
},
})

await notifyIssues(
{ ...DEFAULT_GITHUB_CLIENT, graphql: graphqlStub },
false,
'owner',
'repo',
release
)

const expectedCommentBody = `🎉 This issue has been resolved in version 1.0.0 🎉
The release is available on:
* [GitHub release](some_url)
Your **[optic](https://github.com/nearform/optic-release-automation-action)** bot 📦🚀`

sinon.assert.calledWith(createCommentStub, {
owner: 'owner',
Expand Down
1 change: 1 addition & 0 deletions test/release.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ tap.test(
sinon.assert.calledWith(
stubs.notifyIssuesStub,
DEFAULT_ACTION_DATA.github,
true,
'test',
'repo',
{ body: 'test_body', html_url: 'test_url' }
Expand Down

0 comments on commit 6e687ad

Please sign in to comment.