Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: external linked issues notification #181

Merged
merged 8 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 78 additions & 67 deletions dist/index.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1",
"adm-zip": "^0.5.9",
"lodash.isequal": "^4.5.0",
"lodash.template": "^4.5.0",
"lodash.uniqwith": "^4.5.0",
"markdown-it": "^13.0.1",
"node-fetch": "^2.6.6",
"p-map": "^4.0.0",
Expand Down
46 changes: 36 additions & 10 deletions src/utils/notifyIssues.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const fs = require('fs')
const pMap = require('p-map')
const { logWarning } = require('../log')

const { getPrNumbersFromReleaseNotes } = require('./releaseNotes')

Expand All @@ -16,6 +17,12 @@ async function getLinkedIssueNumbers(github, prNumber, repoOwner, repoName) {
nodes {
id
number
repository {
name
owner {
login
}
}
}
}
}
Expand All @@ -36,7 +43,11 @@ async function getLinkedIssueNumbers(github, prNumber, repoOwner, repoName) {
return []
}

return linkedIssues.map(issue => issue.number)
return linkedIssues.map(issue => ({
issueNumber: issue.number,
repoName: issue?.repository?.name,
repoOwner: issue?.repository?.owner?.login,
}))
}

function createCommentBody(
Expand Down Expand Up @@ -97,18 +108,33 @@ async function notifyIssues(
releaseUrl
)

await pMap(
issueNumbersToNotify,
issueNumber => {
githubClient.rest.issues.createComment({
owner,
repo,
const mapper = async ({ issueNumber, repoOwner, repoName }) => {
try {
if (repoOwner !== owner || repoName !== repo) {
logWarning(
`Skipping external issue-${issueNumber}, repoOwner-${repoOwner} , repo-${repoName}`
)
return pMap.pMapSkip
}

return await githubClient.rest.issues.createComment({
owner: repoOwner,
repo: repoName,
issue_number: issueNumber,
body,
})
},
{ concurrency: 10 }
)
} catch (error) {
logWarning(
`Failed to create comment for issue-${issueNumber}, repo-${repoName}. Error-${error.message}`
)
return pMap.pMapSkip
}
}

await pMap(issueNumbersToNotify, mapper, {
concurrency: 10,
stopOnError: false,
})
}

exports.notifyIssues = notifyIssues
2 changes: 1 addition & 1 deletion src/utils/releaseNotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const md = require('markdown-it')()

function getPrNumbersFromReleaseNotes(releaseNotes) {
const parsedReleaseNotes = md.parse(releaseNotes)
const parsedReleaseNotes = md.parse(releaseNotes, {})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was throwing the js error in #180. As per MarkdownIt docs the parse method needs 2 args
MarkdownIt.parse(src, env) --> Array

const prTokens = parsedReleaseNotes.filter(token => token.type === 'inline')

const allPrNumbers = prTokens
Expand Down
79 changes: 77 additions & 2 deletions test/notifyIssues.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,51 @@ tap.test('Should not call createComment if no linked issues', async () => {
sinon.assert.notCalled(createCommentStub)
})

tap.test(
'Should not call createComment if linked issue belongs to external repo',
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: '13',
repository: {
name: 'ext-repo',
owner: { login: 'ext-owner' },
},
},
],
},
},
},
})

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

sinon.assert.notCalled(createCommentStub)
}
)

tap.test(
'Should call createComment with correct arguments for linked issues with npm link',
async () => {
Expand All @@ -78,7 +123,22 @@ tap.test(
repository: {
pullRequest: {
closingIssuesReferences: {
nodes: [{ number: '10' }, { number: '15' }],
nodes: [
{
number: '10',
repository: {
name: 'repo',
owner: { login: 'owner' },
},
},
{
number: '15',
repository: {
name: 'repo',
owner: { login: 'owner' },
},
},
],
},
},
},
Expand Down Expand Up @@ -137,7 +197,22 @@ tap.test(
repository: {
pullRequest: {
closingIssuesReferences: {
nodes: [{ number: '10' }, { number: '15' }],
nodes: [
{
number: '10',
repository: {
name: 'repo',
owner: { login: 'owner' },
},
},
{
number: '15',
repository: {
name: 'repo',
owner: { login: 'owner' },
},
},
],
},
},
},
Expand Down