Skip to content

Commit

Permalink
Merge pull request #212 from gatewayapps/prevent-references
Browse files Browse the repository at this point in the history
ported code from #189
  • Loading branch information
johnmurphy01 authored Jul 3, 2024
2 parents 3bce6d4 + 1672576 commit c205f96
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 35 deletions.
41 changes: 24 additions & 17 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -302,6 +308,7 @@ async function cloneOldIssueComments(newIssue, repo, url) {
chrome.storage.sync.get(
{
cloneComments: false,
preventReferences: false,
},
(item) => {
if (!item.cloneComments) {
Expand All @@ -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())
Expand Down
44 changes: 26 additions & 18 deletions batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ <h2 id="generaloptions">General Settings</h2>
on original issue</label
>
</div>

<div class="checkbox">
<label
><input class="me-2" type="checkbox" id="prevent-references" />Prevent references to cloned issue on
original issue (using
<a href="https://github.com/orgs/community/discussions/23123#discussioncomment-3239240"
>this hacky method</a
>)</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 @@ -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(
{
Expand All @@ -13,6 +14,7 @@ function save_options() {
createTab,
cloneComments,
disableCommentsOnOriginal,
preventReferences,
},
function () {
// Update status to let user know options were saved.
Expand All @@ -38,13 +40,15 @@ function restore_options() {
createTab: true,
cloneComments: false,
disableCommentsOnOriginal: false,
preventReferences: false,
},
function (items) {
document.getElementById('github-pat').value = items.githubToken
document.getElementById('go-to-issue-list').checked = items.goToList
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
}
)
}
Expand Down

0 comments on commit c205f96

Please sign in to comment.