Skip to content

Commit

Permalink
Merge pull request #213 from gatewayapps/improve-clone-formatting
Browse files Browse the repository at this point in the history
improve formatting of cloned items, add more unit tests
  • Loading branch information
johnmurphy01 authored Jul 3, 2024
2 parents c205f96 + 5fdc88b commit 60ed264
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
15 changes: 8 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,10 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
}

chrome.storage.sync.get({ preventReferences: false }, async (item) => {
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}`
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}`

const newIssue = {
title: oldIssue.title,
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
Expand All @@ -297,11 +300,6 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
})
}

function preventReferences(text) {
// 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)
return text.replace(/https:\/\/github.com\//gi, 'https://www.github.com/')
}

async function cloneOldIssueComments(newIssue, repo, url) {
const response = await ajaxRequest('GET', '', url)

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

response.data.reduce(async (previous, current) => {
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}`
const comment = {
body: item.preventReferences ? preventReferences(current.body) : current.body,
body: item.preventReferences ? preventReferences(newCommentBody) : newCommentBody,
}
return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`)
}, Promise.resolve())
Expand Down
41 changes: 14 additions & 27 deletions batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
}

chrome.storage.sync.get({ preventReferences: false }, async (item) => {
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}`
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}`

const newIssue = {
title: oldIssue.title,
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
Expand All @@ -308,17 +311,13 @@ async function createGithubIssue(repo, oldIssue, closeOriginal) {
})
}

function preventReferences(text) {
// 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)
return text.replace(/https:\/\/github.com\//gi, 'https://www.github.com/')
}

async function cloneOldIssueComments(newIssue, repo, url) {
const comments = await ajaxRequest('GET', '', url)

chrome.storage.sync.get(
{
cloneComments: false,
preventReferences: false,
},
async (item) => {
if (!item.cloneComments) {
Expand All @@ -329,13 +328,16 @@ async function cloneOldIssueComments(newIssue, repo, url) {
return null
}

for (var comment of comments.data) {
const c = {
body: comment.body,
comments.data.reduce(async (previous, current) => {
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}`
const comment = {
body: item.preventReferences ? preventReferences(newCommentBody) : newCommentBody,
}

await ajaxRequest('POST', c, `https://api.github.com/repos/${repo}/issues/${newIssue}/comments`)
}
return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`)
}, Promise.resolve())
}
)
}
Expand Down Expand Up @@ -445,21 +447,6 @@ function addRepoToList(repoFullName, section) {
}
}

function populateUrlMetadata() {
var url = document.location.href
const urlArray = url.split('/')
const currentRepo = urlArray[4]
const organization = urlArray[3]

const urlObject = {
url: url,
currentRepo: currentRepo,
organization: organization,
}

return urlObject
}

function closeBatchModal() {
$('#batchModal').removeClass('in')
$('#batchModal').css('display', '')
Expand Down
5 changes: 5 additions & 0 deletions lib/addBlockQuote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function addBlockQuote(text) {
return text.replace(/^/gm, '> ')
}

module.exports = addBlockQuote
6 changes: 6 additions & 0 deletions lib/preventReferences.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function preventReferences(text) {
// 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)
return text.replace(/https:\/\/github.com\//gi, 'https://www.github.com/')
}

module.exports = preventReferences
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"./handlebars.runtime.min-v4.7.7.js",
"./lib/populateUrlMetadata.js",
"./lib/createFilters.js",
"./lib/addBlockQuote.js",
"./lib/preventReferences.js",
"./batch.js",
"./template.js"
],
Expand All @@ -23,6 +25,8 @@
"./handlebars.runtime.min-v4.7.7.js",
"./lib/populateUrlMetadata.js",
"./lib/createFilters.js",
"./lib/addBlockQuote.js",
"./lib/preventReferences.js",
"./app.js",
"./template.js"
],
Expand Down Expand Up @@ -54,6 +58,8 @@
"handlebars.runtime.min-v4.7.7.js",
"lib/populateUrlMetadata.js",
"lib/createFilters.js",
"lib/addBlockQuote.js",
"lib/preventReferences.js",
"template.js",
"app.js",
"options.html",
Expand Down
10 changes: 10 additions & 0 deletions tests/addBlockQuote.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const addBlockQuote = require('../lib/addBlockQuote')

describe('addBlockQuote', () => {
it('adds block quotes to text', () => {
const originalText = 'This is text'

const blockQuoteText = addBlockQuote(originalText)
expect(blockQuoteText).toEqual(`> ${originalText}`)
})
})
10 changes: 10 additions & 0 deletions tests/preventReferences.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const preventReferences = require('../lib/preventReferences')

describe('preventReferences', () => {
it('updates the url for github', () => {
const originalText = 'This is text with a url: https://github.com/'

const alteredText = preventReferences(originalText)
expect(alteredText).toEqual(`This is text with a url: https://www.github.com/`)
})
})

0 comments on commit 60ed264

Please sign in to comment.