diff --git a/app.js b/app.js index 9ef9f2c..b233719 100644 --- a/app.js +++ b/app.js @@ -263,37 +263,43 @@ async function getGithubIssue(repo, closeOriginal) { // Otherwise Kamino will not function await ajaxRequest('PATCH', { has_issues: true, name: repoName }, `${githubApiUrl}repos/${repo}`) - const response = await ajaxRequest( + const issue = await ajaxRequest( 'GET', '', `${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}` ) - const newIssue = { - title: response.data.title, - body: `From ${currentRepo} created by [${response.data.user.login}](${response.data.user.html_url}): ${organization}/${currentRepo}#${issueNumber} \n\n${response.data.body}`, - labels: response.data.labels, - } - - await createGithubIssue(newIssue, repo, closeOriginal) + await createGithubIssue(repo, issue.data, closeOriginal) } -async function createGithubIssue(newIssue, repo, closeOriginal) { +async function createGithubIssue(repo, oldIssue, closeOriginal) { const { currentRepo, error, issueNumber, organization } = populateUrlMetadata(document.location.href) if (error) { return } - const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`) + 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 newIssue = { + title: oldIssue.title, + body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody, + labels: oldIssue.labels, + } + const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`) + await cloneOldIssueComments( + response.data.number, + repo, + `${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}/comments?per_page=100` + ) - await cloneOldIssueComments( - response.data.number, - repo, - `${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}/comments?per_page=100` - ) + await commentOnIssue(repo, response.data, closeOriginal) + }) +} - await commentOnIssue(repo, response.data, 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) { @@ -302,6 +308,7 @@ async function cloneOldIssueComments(newIssue, repo, url) { chrome.storage.sync.get( { cloneComments: false, + preventReferences: false, }, (item) => { if (!item.cloneComments) { @@ -315,7 +322,7 @@ async function cloneOldIssueComments(newIssue, repo, url) { response.data.reduce(async (previous, current) => { await previous const comment = { - body: current.body, + body: item.preventReferences ? preventReferences(current.body) : current.body, } return ajaxRequest('POST', comment, `${githubApiUrl}repos/${repo}/issues/${newIssue}/comments`) }, Promise.resolve()) diff --git a/batch.js b/batch.js index 024a996..ea9c024 100644 --- a/batch.js +++ b/batch.js @@ -277,32 +277,40 @@ async function getGithubIssue(destinationRepo, issueNumber, closeOriginal) { `https://api.github.com/repos/${organization}/${currentRepo}/issues/${issueNumber}` ) - // build new issue - const newIssue = { - title: issue.data.title, - body: `From ${currentRepo} created by [${issue.data.user.login}](${issue.data.user.html_url}): ${urlObj.organization}/${urlObj.currentRepo}#${issueNumber} \n\n${issue.data.body}`, - labels: issue.data.labels, - } updateMessageText(`Creating issue #${issueNumber} at ${destinationRepo}`) - await createGithubIssue(newIssue, destinationRepo, issue.data, closeOriginal) + await createGithubIssue(destinationRepo, issue.data, closeOriginal) } // create the cloned GitHub issue -async function createGithubIssue(newIssue, repo, oldIssue, closeOriginal) { - const urlObj = populateUrlMetadata() +async function createGithubIssue(repo, oldIssue, closeOriginal) { + const { currentRepo, error, issueNumber, organization } = populateUrlMetadata() + + if (error) { + return + } - const response = await ajaxRequest('POST', newIssue, `https://api.github.com/repos/${repo}/issues`) + 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 newIssue = { + title: oldIssue.title, + body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody, + labels: oldIssue.labels, + } + const response = await ajaxRequest('POST', newIssue, `${githubApiUrl}repos/${repo}/issues`) + await cloneOldIssueComments( + response.data.number, + repo, + `${githubApiUrl}repos/${organization}/${currentRepo}/issues/${issueNumber}/comments?per_page=100` + ) - // clone comments from old issue to new issue - await cloneOldIssueComments( - response.data.number, - repo, - `https://api.github.com/repos/${urlObj.organization}/${urlObj.currentRepo}/issues/${oldIssue.number}/comments?per_page=100` - ) + await commentOnIssue(repo, response.data, closeOriginal) + }) +} - // add a comment to the closed issue - commentOnIssue(repo, oldIssue, response.data, 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) { diff --git a/options.html b/options.html index 216b112..2432b84 100644 --- a/options.html +++ b/options.html @@ -62,6 +62,16 @@

General Settings

on original issue + +
+ +

diff --git a/options.js b/options.js index e3f595e..5b69d35 100644 --- a/options.js +++ b/options.js @@ -5,6 +5,7 @@ function save_options() { const createTab = document.getElementById('create-tab').checked const cloneComments = document.getElementById('clone-comments').checked const disableCommentsOnOriginal = document.getElementById('disable-comment-on-original').checked + const preventReferences = document.getElementById('prevent-references').checked chrome.storage.sync.set( { @@ -13,6 +14,7 @@ function save_options() { createTab, cloneComments, disableCommentsOnOriginal, + preventReferences, }, function () { // Update status to let user know options were saved. @@ -38,6 +40,7 @@ function restore_options() { createTab: true, cloneComments: false, disableCommentsOnOriginal: false, + preventReferences: false, }, function (items) { document.getElementById('github-pat').value = items.githubToken @@ -45,6 +48,7 @@ function restore_options() { document.getElementById('create-tab').checked = items.createTab document.getElementById('clone-comments').checked = items.cloneComments document.getElementById('disable-comment-on-original').checked = items.disableCommentsOnOriginal + document.getElementById('prevent-references').checked = items.preventReferences } ) }