diff --git a/.taprc b/.taprc index ea5b65dd..d020e617 100644 --- a/.taprc +++ b/.taprc @@ -1,4 +1,4 @@ -statements: 80 -lines: 80 -functions: 75 -branches: 80 +statements: 90 +lines: 90 +functions: 78 +branches: 90 diff --git a/dist/index.js b/dist/index.js index 576e4b0c..cea8397a 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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) @@ -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) @@ -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, diff --git a/src/release.js b/src/release.js index c7e96927..3adef8f0 100644 --- a/src/release.js +++ b/src/release.js @@ -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) diff --git a/src/utils/notifyIssues.js b/src/utils/notifyIssues.js index 713605a2..51234d0c 100644 --- a/src/utils/notifyIssues.js +++ b/src/utils/notifyIssues.js @@ -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) @@ -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, diff --git a/test/notifyIssues.test.js b/test/notifyIssues.test.js index fd6de5d8..1b634ff6 100644 --- a/test/notifyIssues.test.js +++ b/test/notifyIssues.test.js @@ -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() @@ -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', diff --git a/test/release.test.js b/test/release.test.js index 6c8c7ba5..38334ac2 100644 --- a/test/release.test.js +++ b/test/release.test.js @@ -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' }