Skip to content

Commit 5fdc88b

Browse files
committed
improve formatting of cloned items, add more unit tests
1 parent c205f96 commit 5fdc88b

File tree

7 files changed

+59
-34
lines changed

7 files changed

+59
-34
lines changed

app.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
280280
}
281281

282282
chrome.storage.sync.get({ preventReferences: false }, async (item) => {
283-
const newIssueBody = `From ${currentRepo} created by [${oldIssue.user.login}](${oldIssue.user.html_url}): [${organization}/${currentRepo}#${issueNumber}](https://github.com/${organization}/${currentRepo}/issues/${issueNumber}) \n\n${oldIssue.body}`
283+
const blockQuoteOldBody = addBlockQuote(oldIssue.body)
284+
const createdAt = oldIssue.created_at.split('T')[0]
285+
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}`
286+
284287
const newIssue = {
285288
title: oldIssue.title,
286289
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
@@ -297,11 +300,6 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
297300
})
298301
}
299302

300-
function preventReferences(text) {
301-
// replace "github.com" links with "www.github.com" links, which do not cause references on the original issue due to the "www" (see https://github.com/orgs/community/discussions/23123#discussioncomment-3239240)
302-
return text.replace(/https:\/\/github.com\//gi, 'https://www.github.com/')
303-
}
304-
305303
async function cloneOldIssueComments(newIssue, repo, url) {
306304
const response = await ajaxRequest('GET', '', url)
307305

@@ -321,8 +319,11 @@ async function cloneOldIssueComments(newIssue, repo, url) {
321319

322320
response.data.reduce(async (previous, current) => {
323321
await previous
322+
const blockQuoteOldBody = addBlockQuote(current.body)
323+
const createdAt = current.created_at.split('T')[0]
324+
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}`
324325
const comment = {
325-
body: item.preventReferences ? preventReferences(current.body) : current.body,
326+
body: item.preventReferences ? preventReferences(newCommentBody) : newCommentBody,
326327
}
327328
return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`)
328329
}, Promise.resolve())

batch.js

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,10 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
291291
}
292292

293293
chrome.storage.sync.get({ preventReferences: false }, async (item) => {
294-
const newIssueBody = `From ${currentRepo} created by [${oldIssue.user.login}](${oldIssue.user.html_url}): [${organization}/${currentRepo}#${issueNumber}](https://github.com/${organization}/${currentRepo}/issues/${issueNumber}) \n\n${oldIssue.body}`
294+
const blockQuoteOldBody = addBlockQuote(oldIssue.body)
295+
const createdAt = oldIssue.created_at.split('T')[0]
296+
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}`
297+
295298
const newIssue = {
296299
title: oldIssue.title,
297300
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
@@ -308,17 +311,13 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
308311
})
309312
}
310313

311-
function preventReferences(text) {
312-
// replace "github.com" links with "www.github.com" links, which do not cause references on the original issue due to the "www" (see https://github.com/orgs/community/discussions/23123#discussioncomment-3239240)
313-
return text.replace(/https:\/\/github.com\//gi, 'https://www.github.com/')
314-
}
315-
316314
async function cloneOldIssueComments(newIssue, repo, url) {
317315
const comments = await ajaxRequest('GET', '', url)
318316

319317
chrome.storage.sync.get(
320318
{
321319
cloneComments: false,
320+
preventReferences: false,
322321
},
323322
async (item) => {
324323
if (!item.cloneComments) {
@@ -329,13 +328,16 @@ async function cloneOldIssueComments(newIssue, repo, url) {
329328
return null
330329
}
331330

332-
for (var comment of comments.data) {
333-
const c = {
334-
body: comment.body,
331+
comments.data.reduce(async (previous, current) => {
332+
await previous
333+
const blockQuoteOldBody = addBlockQuote(current.body)
334+
const createdAt = current.created_at.split('T')[0]
335+
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}`
336+
const comment = {
337+
body: item.preventReferences ? preventReferences(newCommentBody) : newCommentBody,
335338
}
336-
337-
await ajaxRequest('POST', c, `https://api.github.com/repos/${repo}/issues/${newIssue}/comments`)
338-
}
339+
return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`)
340+
}, Promise.resolve())
339341
}
340342
)
341343
}
@@ -445,21 +447,6 @@ function addRepoToList(repoFullName, section) {
445447
}
446448
}
447449

448-
function populateUrlMetadata() {
449-
var url = document.location.href
450-
const urlArray = url.split('/')
451-
const currentRepo = urlArray[4]
452-
const organization = urlArray[3]
453-
454-
const urlObject = {
455-
url: url,
456-
currentRepo: currentRepo,
457-
organization: organization,
458-
}
459-
460-
return urlObject
461-
}
462-
463450
function closeBatchModal() {
464451
$('#batchModal').removeClass('in')
465452
$('#batchModal').css('display', '')

lib/addBlockQuote.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function addBlockQuote(text) {
2+
return text.replace(/^/gm, '> ')
3+
}
4+
5+
module.exports = addBlockQuote

lib/preventReferences.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function preventReferences(text) {
2+
// replace "github.com" links with "www.github.com" links, which do not cause references on the original issue due to the "www" (see https://github.com/orgs/community/discussions/23123#discussioncomment-3239240)
3+
return text.replace(/https:\/\/github.com\//gi, 'https://www.github.com/')
4+
}
5+
6+
module.exports = preventReferences

manifest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"./handlebars.runtime.min-v4.7.7.js",
1010
"./lib/populateUrlMetadata.js",
1111
"./lib/createFilters.js",
12+
"./lib/addBlockQuote.js",
13+
"./lib/preventReferences.js",
1214
"./batch.js",
1315
"./template.js"
1416
],
@@ -23,6 +25,8 @@
2325
"./handlebars.runtime.min-v4.7.7.js",
2426
"./lib/populateUrlMetadata.js",
2527
"./lib/createFilters.js",
28+
"./lib/addBlockQuote.js",
29+
"./lib/preventReferences.js",
2630
"./app.js",
2731
"./template.js"
2832
],
@@ -54,6 +58,8 @@
5458
"handlebars.runtime.min-v4.7.7.js",
5559
"lib/populateUrlMetadata.js",
5660
"lib/createFilters.js",
61+
"lib/addBlockQuote.js",
62+
"lib/preventReferences.js",
5763
"template.js",
5864
"app.js",
5965
"options.html",

tests/addBlockQuote.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const addBlockQuote = require('../lib/addBlockQuote')
2+
3+
describe('addBlockQuote', () => {
4+
it('adds block quotes to text', () => {
5+
const originalText = 'This is text'
6+
7+
const blockQuoteText = addBlockQuote(originalText)
8+
expect(blockQuoteText).toEqual(`> ${originalText}`)
9+
})
10+
})

tests/preventReferences.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const preventReferences = require('../lib/preventReferences')
2+
3+
describe('preventReferences', () => {
4+
it('updates the url for github', () => {
5+
const originalText = 'This is text with a url: https://github.com/'
6+
7+
const alteredText = preventReferences(originalText)
8+
expect(alteredText).toEqual(`This is text with a url: https://www.github.com/`)
9+
})
10+
})

0 commit comments

Comments
 (0)