Skip to content

Commit

Permalink
Add option to prevent references on the original issue.
Browse files Browse the repository at this point in the history
Works by replacing github.com with www.github.com
  • Loading branch information
djahandarie committed Mar 5, 2023
1 parent 8040236 commit 7f11173
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 29 deletions.
72 changes: 43 additions & 29 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function initializeExtension() {
$('.noClone').click(() => {
closeModal()
})
}
}
}

function saveAppliedFilters(urlObj) {
Expand Down Expand Up @@ -162,7 +162,7 @@ function saveAppliedFilters(urlObj) {
{
filters: item.filters,
},
() => {}
() => { }
)
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ function loadRepos() {

// create a way to go to options without using the extension context menu
$('.kamino-heading').click(() => {
chrome.runtime.sendMessage({ action: 'goToOptions' }, (response) => {})
chrome.runtime.sendMessage({ action: 'goToOptions' }, (response) => { })
})

// if there's no personal access token, disable the button
Expand All @@ -231,7 +231,7 @@ function loadRepos() {
$('.repoDropdown').append('<li class="dropdown-header dropdown-header-used">Last Used</li>')
$('.repoDropdown').append('<li class="dropdown-header dropdown-header-rest">The Rest</li>')

getRepos('https://api.github.com/user/repos?per_page=100').then(() => {})
getRepos('https://api.github.com/user/repos?per_page=100').then(() => { })
}

function compileRepositoryList(list, searchTerm) {
Expand Down Expand Up @@ -312,39 +312,53 @@ function getGithubIssue(repo, closeOriginal) {
'',
`https://api.github.com/repos/${urlObj.organization}/${urlObj.currentRepo}/issues/${urlObj.issueNumber}`
).then((issue) => {
// build new issue
const newIssue = {
title: issue.data.title,
body: `From ${urlObj.currentRepo} created by [${issue.data.user.login}](${issue.data.user.html_url}): ${urlObj.organization}/${urlObj.currentRepo}#${urlObj.issueNumber} \n\n${issue.data.body}`,
labels: issue.data.labels,
}

createGithubIssue(newIssue, repo, issue.data, closeOriginal)
createGithubIssue(repo, issue.data, closeOriginal)
})
}

// create the cloned GitHub issue
function createGithubIssue(newIssue, repo, oldIssue, closeOriginal) {
function createGithubIssue(repo, oldIssue, closeOriginal) {
const urlObj = populateUrlMetadata()

ajaxRequest('POST', newIssue, `https://api.github.com/repos/${repo}/issues`).then((response) => {
// clone comments from old issue to new issue
cloneOldIssueComments(
response.data.number,
repo,
`https://api.github.com/repos/${urlObj.organization}/${urlObj.currentRepo}/issues/${urlObj.issueNumber}/comments?per_page=100`
).then((res) => {
// add a comment to the closed issue
commentOnIssue(repo, oldIssue, response.data, closeOriginal)
chrome.storage.sync.get(
{
preventReferences: false,
},
(item) => {
const newIssueBody = `From ${urlObj.currentRepo} created by [${oldIssue.user.login}](${oldIssue.user.html_url}): [${urlObj.organization}/${urlObj.currentRepo}#${urlObj.issueNumber}](https://github.com/${urlObj.organization}/${urlObj.currentRepo}/issues/${urlObj.issueNumber}) \n\n${oldIssue.body}`;

// build new issue
const newIssue = {
title: oldIssue.title,
body: item.preventReferences ? preventReferences(newIssueBody) : newIssueBody,
labels: oldIssue.labels,
}

ajaxRequest('POST', newIssue, `https://api.github.com/repos/${repo}/issues`).then((response) => {
// clone comments from old issue to new issue
cloneOldIssueComments(
response.data.number,
repo,
`https://api.github.com/repos/${urlObj.organization}/${urlObj.currentRepo}/issues/${urlObj.issueNumber}/comments?per_page=100`
).then((res) => {
// 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/")
}

function cloneOldIssueComments(newIssue, repo, url) {
return ajaxRequest('GET', '', url).then((comments) => {
chrome.storage.sync.get(
{
cloneComments: false,
preventReferences: false,
},
(item) => {
if (!item.cloneComments) {
Expand All @@ -358,13 +372,13 @@ function cloneOldIssueComments(newIssue, repo, url) {
comments.data.reduce(
(p, comment) => p.then(_ => {
const c = {
body: comment.body,
body: item.preventReferences ? preventReferences(comment.body) : comment.body,
}
return ajaxRequest('POST', c, `https://api.github.com/repos/${repo}/issues/${newIssue}/comments`)
}),
Promise.resolve()
).then((res) => {
return Promise.resolve({})
).then((res) => {
return Promise.resolve({})
})
}
)
Expand All @@ -382,7 +396,7 @@ function closeGithubIssue(oldIssue) {
'PATCH',
issueToClose,
`https://api.github.com/repos/${urlObj.organization}/${urlObj.currentRepo}/issues/${urlObj.issueNumber}`
).then((done) => {})
).then((done) => { })
}

function commentOnIssue(repo, oldIssue, newIssue, closeOriginal) {
Expand Down Expand Up @@ -426,7 +440,7 @@ function goToIssueList(repo, issueNumber, org, oldRepo) {
// based on user settings, determines if the issues list will open after a clone or not
chrome.runtime.sendMessage(
{ repo: repo, issueNumber: issueNumber, organization: org, oldRepo: oldRepo },
(response) => {}
(response) => { }
)
}

Expand Down Expand Up @@ -538,7 +552,7 @@ function addToMostUsed(repo) {
{
mostUsed: item.mostUsed,
},
(done) => {}
(done) => { }
)
}
)
Expand Down
7 changes: 7 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ <h2 id="generaloptions">General Settings</h2>
<label><input class="me-2" type="checkbox" id="disable-comment-on-original" />Disable Kamino automatic
comments 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')
const preventReferences = document.getElementById('prevent-references')

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 7f11173

Please sign in to comment.