From 6f62d4a00faaa5e0242b3c8f5515c1a8c61df24e Mon Sep 17 00:00:00 2001 From: John Murphy Date: Sun, 16 Apr 2017 22:26:49 -0500 Subject: [PATCH] Refactored code, commented, cleaned up --- app.js | 300 ++++++++---------- ...large.png => kamino-promotional-large.png} | Bin ...uee.png => kamino-promotional-marquee.png} | Bin ...small.png => kamino-promotional-small.png} | Bin background.js | 2 + css/options.css | 2 +- options.html | 2 +- options.js | 5 +- 8 files changed, 140 insertions(+), 171 deletions(-) rename assets/{kamin-promotional-large.png => kamino-promotional-large.png} (100%) rename assets/{kamin-promotional-marquee.png => kamino-promotional-marquee.png} (100%) rename assets/{kamin-promotional-small.png => kamino-promotional-small.png} (100%) diff --git a/app.js b/app.js index 7742dbb..71d0454 100644 --- a/app.js +++ b/app.js @@ -4,253 +4,221 @@ var token = '' if (token === '') { // load jquery via JS $.getScript('https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js', () => { - setInterval( - () => { initializeExtension() } - , 1000); - }); + setInterval(() => { initializeExtension() }, 1000) + }) } function initializeExtension() { + // if there's already a button on the screen, exit if ($('.kaminoButton').length > 0) { - return; + return } // the button - var btn = $(''); - var popup = $('') + const btn = $('') + // the modal + const popup = $('') // get url - var url = document.location.href; + const urlObj = populateUrlMetadata() - if (url.indexOf('/pull/') < 0 && $('.kaminoButton').length === 0) { - // append button to DOM - $('.gh-header-actions').append(btn); - $('.gh-header-actions').append(popup); + // if the page is not a pull request page and there is no Kamino button in the DOM, proceed + if (urlObj.url.indexOf('/pull/') < 0 && $('.kaminoButton').length === 0) { + // append button and modal to DOM + $('.gh-header-actions').append(btn) + $('.gh-header-actions').append(popup) - // grab the PAT + // load the token chrome.storage.sync.get({ githubToken: '' }, (item) => { token = item.githubToken + // grab the PAT if ($('.kaminoButton').length > 0) { - loadRepos(); + loadRepos() } }) $('.kaminoButton').click(() => { // make sure the bootstrap dropdown opens and closes properly if ($('.dropdown').hasClass('open')) { - $('.dropdown').removeClass('open'); + $('.dropdown').removeClass('open') } else { - $('.dropdown').addClass('open'); + $('.dropdown').addClass('open') } }) $('.cloneNow').click(() => { - chrome.storage.sync.get({ - githubToken: '' - }, (item) => { - $('#kaminoModal').removeClass('in'); - $('#kaminoModal').css('display', ''); - - chrome.storage.sync.get({ - githubToken: '' - }, (item) => { - token = item.githubToken - getGithubIssue($('.cloneNow').attr('data-repo')); - }) - }) + closeModal() + getGithubIssue($('.cloneNow').attr('data-repo')) }) $('.close').click(() => { - $('#kaminoModal').removeClass('in'); - $('#kaminoModal').css('display', ''); + closeModal() }) $('.noClone').click(() => { - $('#kaminoModal').removeClass('in'); - $('#kaminoModal').css('display', ''); + closeModal() }) } } -// get all repos for the user function loadRepos() { + // if there's no personal access token, disable the button if (token === '') { console.log('disabling button because there is no Personal Access Token for authentication with Github') - $(".kaminoButton").prop('disabled', true); + $(".kaminoButton").prop('disabled', true) } - $.ajax({ - type: 'GET', - beforeSend: (request) => { - request.setRequestHeader("Authorization", "token " + token) - request.setRequestHeader('Content-Type', 'application/json') - }, - url: 'https://api.github.com/user/repos?per_page=1000', - success: (repos) => { - // get the current github issue info - var url = document.location.href; - var urlArray = url.split('/'); - var currentRepo = urlArray[urlArray.length - 3] + // get a list of repos for the user + ajaxRequest('GET', '', 'https://api.github.com/user/repos?per_page=1000', + (repos) => { + // get the current github issue info from the url + const urlObj = populateUrlMetadata() // sort the repo - repos = repos.sort((a, b) => a.full_name.localeCompare(b.full_name)); + repos = repos.sort((a, b) => a.full_name.localeCompare(b.full_name)) // remove the repo you're currently on repos = repos.filter((item) => { - return item.full_name !== currentRepo; + return item.full_name !== urlObj.currentRepo }) // clear the list each time to avoid duplicates - $('.repoDropdown').empty(); + $('.repoDropdown').empty() repos.forEach((repo) => { $('.repoDropdown').append('
  • ' + repo.full_name + '
  • ') - $('#' + repo.name).bind('click', () => { itemClick(repo.full_name) }); - }); + $('#' + repo.name).bind('click', () => { itemClick(repo.full_name) }) + }) }, - error: (error) => { - console.log('disabling because get repository request failed') - $(".kaminoButton").prop('disabled', true); - } - }) -} - -function itemClick(repo) { - $('.cloneNow').attr('data-repo', repo); - $('.confirmText').text('Are you sure you want to clone this issue to ' + repo + '? The original issue will be closed.'); - $('#kaminoModal').addClass('in'); - $('#kaminoModal').css('display', 'block'); + (error) => { + console.error('disabling because get repository request failed') + console.error(error) + $(".kaminoButton").prop('disabled', true) + }) } function getGithubIssue(repo) { - var url = document.location.href; - var urlArray = url.split('/'); - var currentRepo = urlArray[urlArray.length - 3] - var organization = urlArray[urlArray.length - 4] - var issueNumber = urlArray[urlArray.length - 1].replace('#', ''); - - $.ajax({ - type: 'GET', - beforeSend: (request) => { - request.setRequestHeader("Authorization", "token " + token) - request.setRequestHeader('Content-Type', 'application/json') - }, - url: 'https://api.github.com/repos/' + organization + '/' + currentRepo + '/issues/' + issueNumber, - success: (issue) => { + const urlObj = populateUrlMetadata() + + ajaxRequest('GET', '', 'https://api.github.com/repos/' + urlObj.organization + '/' + urlObj.currentRepo + '/issues/' + urlObj.issueNumber, + (issue) => { // build new issue - var newIssue = { + const newIssue = { title: issue.title, - body: 'From ' + currentRepo + ': ' + organization + '/' + currentRepo + '#' + issueNumber + " \n\n" + issue.body, + body: 'From ' + urlObj.currentRepo + ': ' + urlObj.organization + '/' + urlObj.currentRepo + '#' + urlObj.issueNumber + " \n\n" + issue.body, milestone: issue.milestone, labels: issue.labels, assignees: issue.assignees } - // grab the PAT - chrome.storage.sync.get({ - githubToken: '' - }, (item) => { - token = item.githubToken - createGithubIssue(newIssue, repo, issue); - }) + createGithubIssue(newIssue, repo, issue) }, - error: (error) => { - console.log(error); - } - }) + (error) => { + console.error(error) + }) } // create the cloned GitHub issue function createGithubIssue(newIssue, repo, oldIssue) { - $.ajax({ - type: 'POST', - beforeSend: (request) => { - request.setRequestHeader("Authorization", "token " + token) - request.setRequestHeader('Content-Type', 'application/json') - }, - data: JSON.stringify(newIssue), - url: 'https://api.github.com/repos/' + repo + '/issues', - success: (response) => { + ajaxRequest('POST', newIssue, 'https://api.github.com/repos/' + repo + '/issues', + (response) => { // add a comment to the closed issue - // grab the PAT - chrome.storage.sync.get({ - githubToken: '' - }, (item) => { - var url = document.location.href; - var urlArray = url.split('/'); - var organization = urlArray[urlArray.length - 4] - - token = item.githubToken - commentOnIssue(organization, repo, oldIssue, response); - }) + commentOnIssue(repo, oldIssue, response) }, - error: (error) => { - console.log(error); - } - }) + (error) => { + console.error(error) + }) } function closeGithubIssue(oldIssue) { - var issueToClose = { + const issueToClose = { state: 'closed' - }; - - var url = document.location.href; - var urlArray = url.split('/'); - var currentRepo = urlArray[urlArray.length - 3] - var organization = urlArray[urlArray.length - 4] - var issueNumber = urlArray[urlArray.length - 1].replace('#', ''); - - $.ajax({ - type: 'PATCH', - beforeSend: (request) => { - request.setRequestHeader("Authorization", "token " + token) - request.setRequestHeader('Content-Type', 'application/json') - }, - data: JSON.stringify(issueToClose), - url: 'https://api.github.com/repos/' + organization + '/' + currentRepo + '/issues/' + issueNumber, - success: (response) => { + } + + const urlObj = populateUrlMetadata() + + ajaxRequest('PATCH', issueToClose, 'https://api.github.com/repos/' + urlObj.organization + '/' + urlObj.currentRepo + '/issues/' + urlObj.issueNumber, + (response) => { }, - error: (error) => { - console.log(error); - } - }) + (error) => { + console.error(error) + }) } -function commentOnIssue(org, repo, oldIssue, newIssue) { - var comment = { +function commentOnIssue(repo, oldIssue, newIssue) { + const comment = { body: 'Kamino closed and cloned this issue to ' + org + '/' + repo - }; - - var url = document.location.href; - var urlArray = url.split('/'); - var currentRepo = urlArray[urlArray.length - 3] - var issueNumber = urlArray[urlArray.length - 1].replace('#', ''); - - $.ajax({ - type: 'POST', - beforeSend: (request) => { - request.setRequestHeader("Authorization", "token " + token) - request.setRequestHeader('Content-Type', 'application/json') - }, - data: JSON.stringify(comment), - url: 'https://api.github.com/repos/' + org + '/' + currentRepo + '/issues/' + issueNumber + '/comments', - success: (response) => { + } + + const urlObj = populateUrlMetadata() + + ajaxRequest('POST', comment, 'https://api.github.com/repos/' + urlObj.organization + '/' + urlObj.currentRepo + '/issues/' + urlObj.issueNumber + '/comments', + (response) => { // if success, close the existing issue and open new in a new tab - // grab the PAT - chrome.storage.sync.get({ - githubToken: '' - }, (item) => { - token = item.githubToken - closeGithubIssue(oldIssue); - window.open('https://github.com/' + repo + '/issues/' + newIssue.number, "_blank"); - }) + closeGithubIssue(oldIssue) + window.open('https://github.com/' + repo + '/issues/' + newIssue.number, "_blank") }, - error: (error) => { - console.log(error); - } + (error) => { + console.error(error) + }) +} + +function ajaxRequest(type, data, url, successCallback, errorCallback) { + chrome.storage.sync.get({ + githubToken: '' + }, (item) => { + token = item.githubToken + $.ajax({ + type: type, + beforeSend: (request) => { + request.setRequestHeader('Authorization', 'token ' + token) + request.setRequestHeader('Content-Type', 'application/json') + }, + data: JSON.stringify(data), + url: url, + success: (response) => { + successCallback(response) + }, + error: (err) => { + errorCallback(err) + } + }) }) +} + +function populateUrlMetadata() { + var url = document.location.href + const urlArray = url.split('/') + const currentRepo = urlArray[urlArray.length - 3] + const organization = urlArray[urlArray.length - 4] + const issueNumber = urlArray[urlArray.length - 1].replace('#', '') + + const urlObject = { + url: url, + currentRepo: currentRepo, + organization: organization, + issueNumber: issueNumber + } + + return urlObject +} + +function itemClick(repo) { + $('.cloneNow').attr('data-repo', repo) + $('.confirmText').text('Are you sure you want to clone this issue to ' + repo + '? The original issue will be closed.') + openModal() +} + +function closeModal() { + // make sure the modal closes properly + $('#kaminoModal').removeClass('in') + $('#kaminoModal').css('display', '') +} + +function openModal() { + $('#kaminoModal').addClass('in') + $('#kaminoModal').css('display', 'block') } \ No newline at end of file diff --git a/assets/kamin-promotional-large.png b/assets/kamino-promotional-large.png similarity index 100% rename from assets/kamin-promotional-large.png rename to assets/kamino-promotional-large.png diff --git a/assets/kamin-promotional-marquee.png b/assets/kamino-promotional-marquee.png similarity index 100% rename from assets/kamin-promotional-marquee.png rename to assets/kamino-promotional-marquee.png diff --git a/assets/kamin-promotional-small.png b/assets/kamino-promotional-small.png similarity index 100% rename from assets/kamin-promotional-small.png rename to assets/kamino-promotional-small.png diff --git a/background.js b/background.js index ed9a288..664be31 100644 --- a/background.js +++ b/background.js @@ -1,3 +1,4 @@ +// used when Github uses push state. chrome.webNavigation.onHistoryStateUpdated.addListener((details) => { chrome.tabs.executeScript(null, { file: "jquery/jquery-3.2.0.min.js", runAt: 'document_end' }, (j) => { chrome.tabs.executeScript(null, { file: "app.js", runAt: 'document_end' }, (a) => { @@ -6,6 +7,7 @@ chrome.webNavigation.onHistoryStateUpdated.addListener((details) => { }) }) +// open the Kamino Github page on installed chrome.runtime.onInstalled.addListener((details) => { if (details.reason === 'installed') { chrome.tabs.create({ url: 'https://github.com/gatewayapps/kamino' }, (tab) => { diff --git a/css/options.css b/css/options.css index a9b23ae..166f34a 100644 --- a/css/options.css +++ b/css/options.css @@ -71,7 +71,7 @@ background: #6B6969; box-shadow: 0 10px 0 #6B6969, 0 20px 0 #6B6969; } - .adblock-logo { + .kamino-logo { margin: 10px 0px 0px 10px; } .social .feedback { diff --git a/options.html b/options.html index 2315e5e..4c44b6e 100644 --- a/options.html +++ b/options.html @@ -22,7 +22,7 @@ - +
    diff --git a/options.js b/options.js index e0b0b62..15bbec1 100644 --- a/options.js +++ b/options.js @@ -13,16 +13,15 @@ function save_options() { }); } -// Restores select box and checkbox state using the preferences -// stored in chrome.storage. +// Restores options function restore_options() { - // Use default value color = 'red' and likesColor = true. chrome.storage.sync.get({ githubToken: '' }, function(items) { document.getElementById('github-pat').value = items.githubToken; }); } + document.addEventListener('DOMContentLoaded', restore_options); document.getElementById('saveButton').addEventListener('click', save_options); \ No newline at end of file