Skip to content

Commit c205f96

Browse files
authored
Merge pull request #212 from gatewayapps/prevent-references
ported code from #189
2 parents 3bce6d4 + 1672576 commit c205f96

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

app.js

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -263,37 +263,43 @@ async function getGithubIssue(repo, closeOriginal) {
263263
// Otherwise Kamino will not function
264264
await ajaxRequest('PATCH', { has_issues: true, name: repoName }, `${githubApiUrl}repos/${repo}`)
265265

266-
const response = await ajaxRequest(
266+
const issue = await ajaxRequest(
267267
'GET',
268268
'',
269269
`${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}`
270270
)
271271

272-
const newIssue = {
273-
title: response.data.title,
274-
body: `From ${currentRepo} created by [${response.data.user.login}](${response.data.user.html_url}): ${organization}/${currentRepo}#${issueNumber} \n\n${response.data.body}`,
275-
labels: response.data.labels,
276-
}
277-
278-
await createGithubIssue(newIssue, repo, closeOriginal)
272+
await createGithubIssue(repo, issue.data, closeOriginal)
279273
}
280274

281-
async function createGithubIssue(newIssue, repo, closeOriginal) {
275+
async function createGithubIssue(repo, oldIssue, closeOriginal) {
282276
const { currentRepo, error, issueNumber, organization } = populateUrlMetadata(document.location.href)
283277

284278
if (error) {
285279
return
286280
}
287281

288-
const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`)
282+
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}`
284+
const newIssue = {
285+
title: oldIssue.title,
286+
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
287+
labels: oldIssue.labels,
288+
}
289+
const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`)
290+
await cloneOldIssueComments(
291+
response.data.number,
292+
repo,
293+
`${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}/comments?per_page=100`
294+
)
289295

290-
await cloneOldIssueComments(
291-
response.data.number,
292-
repo,
293-
`${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}/comments?per_page=100`
294-
)
296+
await commentOnIssue(repo, response.data, closeOriginal)
297+
})
298+
}
295299

296-
await commentOnIssue(repo, response.data, closeOriginal)
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/')
297303
}
298304

299305
async function cloneOldIssueComments(newIssue, repo, url) {
@@ -302,6 +308,7 @@ async function cloneOldIssueComments(newIssue, repo, url) {
302308
chrome.storage.sync.get(
303309
{
304310
cloneComments: false,
311+
preventReferences: false,
305312
},
306313
(item) => {
307314
if (!item.cloneComments) {
@@ -315,7 +322,7 @@ async function cloneOldIssueComments(newIssue, repo, url) {
315322
response.data.reduce(async (previous, current) => {
316323
await previous
317324
const comment = {
318-
body: current.body,
325+
body: item.preventReferences ? preventReferences(current.body) : current.body,
319326
}
320327
return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`)
321328
}, Promise.resolve())

batch.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -277,32 +277,40 @@ async function getGithubIssue(destinationRepo, issueNumber, closeOriginal) {
277277
`https://api.github.com/repos/${organization}/${currentRepo}/issues/${issueNumber}`
278278
)
279279

280-
// build new issue
281-
const newIssue = {
282-
title: issue.data.title,
283-
body: `From ${currentRepo} created by [${issue.data.user.login}](${issue.data.user.html_url}): ${urlObj.organization}/${urlObj.currentRepo}#${issueNumber} \n\n${issue.data.body}`,
284-
labels: issue.data.labels,
285-
}
286280
updateMessageText(`Creating issue #${issueNumber} at ${destinationRepo}`)
287281

288-
await createGithubIssue(newIssue, destinationRepo, issue.data, closeOriginal)
282+
await createGithubIssue(destinationRepo, issue.data, closeOriginal)
289283
}
290284

291285
// create the cloned GitHub issue
292-
async function createGithubIssue(newIssue, repo, oldIssue, closeOriginal) {
293-
const urlObj = populateUrlMetadata()
286+
async function createGithubIssue(repo, oldIssue, closeOriginal) {
287+
const { currentRepo, error, issueNumber, organization } = populateUrlMetadata()
288+
289+
if (error) {
290+
return
291+
}
294292

295-
const response = await ajaxRequest('POST', newIssue, `https://api.github.com/repos/${repo}/issues`)
293+
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}`
295+
const newIssue = {
296+
title: oldIssue.title,
297+
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
298+
labels: oldIssue.labels,
299+
}
300+
const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`)
301+
await cloneOldIssueComments(
302+
response.data.number,
303+
repo,
304+
`${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}/comments?per_page=100`
305+
)
296306

297-
// clone comments from old issue to new issue
298-
await cloneOldIssueComments(
299-
response.data.number,
300-
repo,
301-
`https://api.github.com/repos/${urlObj.organization}/${urlObj.currentRepo}/issues/${oldIssue.number}/comments?per_page=100`
302-
)
307+
await commentOnIssue(repo, response.data, closeOriginal)
308+
})
309+
}
303310

304-
// add a comment to the closed issue
305-
commentOnIssue(repo, oldIssue, response.data, closeOriginal)
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/')
306314
}
307315

308316
async function cloneOldIssueComments(newIssue, repo, url) {

options.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ <h2 id="generaloptions">General Settings</h2>
6262
on original issue</label
6363
>
6464
</div>
65+
66+
<div class="checkbox">
67+
<label
68+
><input class="me-2" type="checkbox" id="prevent-references" />Prevent references to cloned issue on
69+
original issue (using
70+
<a href="https://github.com/orgs/community/discussions/23123#discussioncomment-3239240"
71+
>this hacky method</a
72+
>)</label
73+
>
74+
</div>
6575
</div>
6676

6777
<p class="mt-2">

options.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ function save_options() {
55
const createTab = document.getElementById('create-tab').checked
66
const cloneComments = document.getElementById('clone-comments').checked
77
const disableCommentsOnOriginal = document.getElementById('disable-comment-on-original').checked
8+
const preventReferences = document.getElementById('prevent-references').checked
89

910
chrome.storage.sync.set(
1011
{
@@ -13,6 +14,7 @@ function save_options() {
1314
createTab,
1415
cloneComments,
1516
disableCommentsOnOriginal,
17+
preventReferences,
1618
},
1719
function () {
1820
// Update status to let user know options were saved.
@@ -38,13 +40,15 @@ function restore_options() {
3840
createTab: true,
3941
cloneComments: false,
4042
disableCommentsOnOriginal: false,
43+
preventReferences: false,
4144
},
4245
function (items) {
4346
document.getElementById('github-pat').value = items.githubToken
4447
document.getElementById('go-to-issue-list').checked = items.goToList
4548
document.getElementById('create-tab').checked = items.createTab
4649
document.getElementById('clone-comments').checked = items.cloneComments
4750
document.getElementById('disable-comment-on-original').checked = items.disableCommentsOnOriginal
51+
document.getElementById('prevent-references').checked = items.preventReferences
4852
}
4953
)
5054
}

0 commit comments

Comments
 (0)