diff --git a/app.js b/app.js index 20fd04c..335f44c 100644 --- a/app.js +++ b/app.js @@ -279,16 +279,25 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) { return } - chrome.storage.sync.get({ preventReferences: false }, async (item) => { + chrome.storage.sync.get({ preventReferences: false, preventMentions: false }, async (item) => { const blockQuoteOldBody = addBlockQuote(oldIssue.body) const createdAt = oldIssue.created_at.split('T')[0] - const newIssueBody = `**[ @${oldIssue.user.login}](${oldIssue.user.html_url})** cloned issue [${organization}/${currentRepo}#${issueNumber}](${oldIssue.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}` + let newIssueBody = `**[ @${oldIssue.user.login}](${oldIssue.user.html_url})** cloned issue [${organization}/${currentRepo}#${issueNumber}](${oldIssue.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}` + + if (item.preventMentions) { + newIssueBody = preventMentions(newIssueBody) + } + + if (item.preventReferences) { + newIssueBody = preventReferences(newIssueBody) + } const newIssue = { title: oldIssue.title, - body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody, + body: newIssueBody, labels: oldIssue.labels, } + const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`) await cloneOldIssueComments( response.data.number, @@ -307,6 +316,7 @@ async function cloneOldIssueComments(newIssue, repo, url) { { cloneComments: false, preventReferences: false, + preventMentions: false, }, (item) => { if (!item.cloneComments) { @@ -321,9 +331,18 @@ async function cloneOldIssueComments(newIssue, repo, url) { await previous const blockQuoteOldBody = addBlockQuote(current.body) const createdAt = current.created_at.split('T')[0] - const newCommentBody = `**[ @${current.user.login}](${current.user.html_url})** [commented](${current.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}` + let newCommentBody = `**[ @${current.user.login}](${current.user.html_url})** [commented](${current.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}` + + if (item.preventMentions) { + newCommentBody = preventMentions(newCommentBody) + } + + if (item.preventReferences) { + newCommentBody = preventReferences(newCommentBody) + } + const comment = { - body: item.preventReferences ? preventReferences(newCommentBody) : newCommentBody, + body: newCommentBody, } return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`) }, Promise.resolve()) diff --git a/batch.js b/batch.js index fde3d8b..54857a8 100644 --- a/batch.js +++ b/batch.js @@ -290,14 +290,21 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) { return } - chrome.storage.sync.get({ preventReferences: false }, async (item) => { + chrome.storage.sync.get({ preventReferences: false, preventMentions: false }, async (item) => { const blockQuoteOldBody = addBlockQuote(oldIssue.body) const createdAt = oldIssue.created_at.split('T')[0] - const newIssueBody = `**[ @${oldIssue.user.login}](${oldIssue.user.html_url})** cloned issue [${organization}/${currentRepo}#${issueNumber}](${oldIssue.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}` + let newIssueBody = `**[ @${oldIssue.user.login}](${oldIssue.user.html_url})** cloned issue [${organization}/${currentRepo}#${issueNumber}](${oldIssue.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}` + + if (item.preventMentions) { + newIssueBody = preventMentions(newIssueBody) + } + if (item.preventReferences) { + newIssueBody = preventReferences(newIssueBody) + } const newIssue = { title: oldIssue.title, - body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody, + body: newIssueBody, labels: oldIssue.labels, } const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`) @@ -318,6 +325,7 @@ async function cloneOldIssueComments(newIssue, repo, url) { { cloneComments: false, preventReferences: false, + preventMentions: false, }, async (item) => { if (!item.cloneComments) { @@ -332,9 +340,17 @@ async function cloneOldIssueComments(newIssue, repo, url) { await previous const blockQuoteOldBody = addBlockQuote(current.body) const createdAt = current.created_at.split('T')[0] - const newCommentBody = `**[ @${current.user.login}](${current.user.html_url})** commented [on ${createdAt}](${current.html_url}): \n\n${blockQuoteOldBody}` + let newCommentBody = `**[ @${current.user.login}](${current.user.html_url})** commented [on ${createdAt}](${current.html_url}): \n\n${blockQuoteOldBody}` + + if (item.preventMentions) { + newCommentBody = preventMentions(newCommentBody) + } + if (item.preventReferences) { + newCommentBody = preventReferences(newCommentBody) + } + const comment = { - body: item.preventReferences ? preventReferences(newCommentBody) : newCommentBody, + body: newCommentBody, } return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`) }, Promise.resolve()) diff --git a/lib/preventMentions.js b/lib/preventMentions.js new file mode 100644 index 0000000..a5f690d --- /dev/null +++ b/lib/preventMentions.js @@ -0,0 +1,10 @@ +function preventMentions(text) { + // replace "@githubusername" with a link to the user to avoid mention notifications + // regex from https://stackoverflow.com/a/30281147 + return text.replace( + /\B@([a-z0-9](?:-(?=[a-z0-9])|[a-z0-9]){0,38}(?<=[a-z0-9]))/gi, + '[@**$1**](https://www.github.com/$1)' + ) +} + +module.exports = preventMentions diff --git a/options.html b/options.html index 2432b84..bf73e76 100644 --- a/options.html +++ b/options.html @@ -72,6 +72,14 @@

General Settings

>) + +
+ +

diff --git a/options.js b/options.js index 5b69d35..0f4b1e1 100644 --- a/options.js +++ b/options.js @@ -6,6 +6,7 @@ function save_options() { const cloneComments = document.getElementById('clone-comments').checked const disableCommentsOnOriginal = document.getElementById('disable-comment-on-original').checked const preventReferences = document.getElementById('prevent-references').checked + const preventMentions = document.getElementById('prevent-mentions').checked chrome.storage.sync.set( { @@ -15,6 +16,7 @@ function save_options() { cloneComments, disableCommentsOnOriginal, preventReferences, + preventMentions, }, function () { // Update status to let user know options were saved. @@ -41,6 +43,7 @@ function restore_options() { cloneComments: false, disableCommentsOnOriginal: false, preventReferences: false, + preventMentions: false, }, function (items) { document.getElementById('github-pat').value = items.githubToken @@ -49,6 +52,7 @@ function restore_options() { document.getElementById('clone-comments').checked = items.cloneComments document.getElementById('disable-comment-on-original').checked = items.disableCommentsOnOriginal document.getElementById('prevent-references').checked = items.preventReferences + document.getElementById('prevent-mentions').checked = items.preventMentions } ) } diff --git a/tests/preventMentions.spec.js b/tests/preventMentions.spec.js new file mode 100644 index 0000000..fd33cbe --- /dev/null +++ b/tests/preventMentions.spec.js @@ -0,0 +1,10 @@ +const preventMentions = require('../lib/preventMentions') + +describe('preventMentions', () => { + it('replaces @username with urls', () => { + const originalText = 'Hello, @johnmurphy01. How are you doing?' + + const alteredText = preventMentions(originalText) + expect(alteredText).toEqual(`Hello, [@**johnmurphy01**](https://www.github.com/johnmurphy01). How are you doing?`) + }) +})