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

Prevent mentions #216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 24 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `**[<img src="https://avatars.githubusercontent.com/u/${oldIssue.user.id}?s=17&v=4" width="17" height="17"> @${oldIssue.user.login}](${oldIssue.user.html_url})** cloned issue [${organization}/${currentRepo}#${issueNumber}](${oldIssue.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}`
let newIssueBody = `**[<img src="https://avatars.githubusercontent.com/u/${oldIssue.user.id}?s=17&v=4" width="17" height="17"> @${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,
Expand All @@ -307,6 +316,7 @@ async function cloneOldIssueComments(newIssue, repo, url) {
{
cloneComments: false,
preventReferences: false,
preventMentions: false,
},
(item) => {
if (!item.cloneComments) {
Expand All @@ -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 = `**[<img src="https://avatars.githubusercontent.com/u/${current.user.id}?s=17&v=4" width="17" height="17"> @${current.user.login}](${current.user.html_url})** [commented](${current.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}`
let newCommentBody = `**[<img src="https://avatars.githubusercontent.com/u/${current.user.id}?s=17&v=4" width="17" height="17"> @${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())
Expand Down
26 changes: 21 additions & 5 deletions batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `**[<img src="https://avatars.githubusercontent.com/u/${oldIssue.user.id}?s=17&v=4" width="17" height="17"> @${oldIssue.user.login}](${oldIssue.user.html_url})** cloned issue [${organization}/${currentRepo}#${issueNumber}](${oldIssue.html_url}) on ${createdAt}: \n\n${blockQuoteOldBody}`
let newIssueBody = `**[<img src="https://avatars.githubusercontent.com/u/${oldIssue.user.id}?s=17&v=4" width="17" height="17"> @${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`)
Expand All @@ -318,6 +325,7 @@ async function cloneOldIssueComments(newIssue, repo, url) {
{
cloneComments: false,
preventReferences: false,
preventMentions: false,
},
async (item) => {
if (!item.cloneComments) {
Expand All @@ -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 = `**[<img src="https://avatars.githubusercontent.com/u/${current.user.id}?s=17&v=4" width="17" height="17"> @${current.user.login}](${current.user.html_url})** commented [on ${createdAt}](${current.html_url}): \n\n${blockQuoteOldBody}`
let newCommentBody = `**[<img src="https://avatars.githubusercontent.com/u/${current.user.id}?s=17&v=4" width="17" height="17"> @${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())
Expand Down
10 changes: 10 additions & 0 deletions lib/preventMentions.js
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ <h2 id="generaloptions">General Settings</h2>
>)</label
>
</div>

<div class="checkbox">
<label>
<input class="me-2" type="checkbox" id="prevent-mentions" />Prevent notifications to users mentioned in
the cloned issues/comments (by replacing all occurences of @username with a link;
<strong>be warned this will do the replacement even inside of code blocks</strong>)
</label>
</div>
</div>

<p class="mt-2">
Expand Down
4 changes: 4 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -15,6 +16,7 @@ function save_options() {
cloneComments,
disableCommentsOnOriginal,
preventReferences,
preventMentions,
},
function () {
// Update status to let user know options were saved.
Expand All @@ -41,6 +43,7 @@ function restore_options() {
cloneComments: false,
disableCommentsOnOriginal: false,
preventReferences: false,
preventMentions: false,
},
function (items) {
document.getElementById('github-pat').value = items.githubToken
Expand All @@ -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
}
)
}
Expand Down
10 changes: 10 additions & 0 deletions tests/preventMentions.spec.js
Original file line number Diff line number Diff line change
@@ -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?`)
})
})
Loading