From 87261d33a020379e0c2f88423420e318e773a1b0 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Thu, 8 Jul 2021 17:33:23 +0300 Subject: [PATCH] github list pr script cleanup and ui changes. - Isolated in a function - changed namespace - updated description - added PR number - list is more concise - removed open button. Clicing on an item will open PR in new tab --- github_list_own_prs_in_repo.js | 122 ++++++++++++++++++--------------- 1 file changed, 68 insertions(+), 54 deletions(-) diff --git a/github_list_own_prs_in_repo.js b/github_list_own_prs_in_repo.js index 8379c14..3dbf7b8 100644 --- a/github_list_own_prs_in_repo.js +++ b/github_list_own_prs_in_repo.js @@ -1,74 +1,88 @@ // ==UserScript== // @name GitHub - list own PRs in a repository -// @description A brief description of your script -// @namespace http://arantius.com/misc/greasemonkey/ +// @description List Your own (open) PRs in a repo with CI state for easy navigation +// @namespace https://git.sagidayan.com/sagi/greasemonkey-scripts // @author Sagi Dayan -// @match https://github.com/* +// @match https://github.com/* // @version 1.0 // ==/UserScript== -// Update These -const GITHUB_AUTH = ''; // https://github.com/settings/tokens -const GITHUB_USERNAME = ''; -// +(function () { + // Update These + const GITHUB_AUTH = ''; + const GITHUB_USERNAME = ''; + // + let currentRepo = ''; -let currentRepo = ''; + // fetch my prs + async function getPRs() { + const response = await fetch("https://api.github.com/graphql", { + "method": "POST", + "headers": { + "Content-Type": "application/json", + "Authorization": `token ${GITHUB_AUTH}` + }, + "body": "{\"query\":\"{\\n user(login: \\\"" + GITHUB_USERNAME + "\\\") {\\n pullRequests(first: 100, states: OPEN, baseRefName:\\\"master\\\") {\\n totalCount\\n nodes {\\n createdAt\\n number\\n title\\n mergeable\\n url\\n commits(last:1) {\\n nodes {\\n commit {\\n statusCheckRollup{\\n state\\n }\\n }\\n }\\n }\\n baseRefName\\n headRefName\\n repository{\\n nameWithOwner\\n }\\n }\\n pageInfo {\\n hasNextPage\\n endCursor\\n }\\n }\\n }\\n}\"}" + }); + const payload = await response.json(); + const prs = payload.data.user.pullRequests.nodes.filter(pr => { + return pr.repository.nameWithOwner === currentRepo; + }); -// fetch my prs -async function getPRs() { - const response = await fetch("https://api.github.com/graphql", { - "method": "POST", - "headers": { - "Content-Type": "application/json", - "Authorization": `token ${GITHUB_AUTH}` - }, - "body": "{\"query\":\"{\\n user(login: \\\"" + GITHUB_USERNAME + "\\\") {\\n pullRequests(first: 100, states: OPEN, baseRefName:\\\"master\\\") {\\n totalCount\\n nodes {\\n createdAt\\n number\\n title\\n mergeable\\n url\\n commits(last:1) {\\n nodes {\\n commit {\\n statusCheckRollup{\\n state\\n }\\n }\\n }\\n }\\n baseRefName\\n headRefName\\n repository{\\n nameWithOwner\\n }\\n }\\n pageInfo {\\n hasNextPage\\n endCursor\\n }\\n }\\n }\\n}\"}" - }); - const payload = await response.json(); - const prs = payload.data.user.pullRequests.nodes.filter(pr => { - return pr.repository.nameWithOwner === currentRepo; - }); - - const main = document.getElementsByTagName('main')[0]; - if (!main) return; - prs.forEach(pr => { - main.lastElementChild.insertBefore(createPrHTML(pr), main.lastElementChild.firstElementChild); - }); -} - -setInterval(() => { - const urlSplit = document.URL.split('/'); - if (urlSplit.length !== 5) { // Not A base repo URL - currentRepo = ''; - return; + const main = document.getElementsByTagName('main')[0]; + if (!main) return; + prs.forEach(pr => { + // Check if element Exists - if it does, remove and add updated one. + const prItem = document.getElementById(generatePrItemId(pr.number)); + if (prItem) { + console.log('Item Found', prItem.id); + main.removeChild(prItem); + } + main.lastElementChild.insertBefore(createPrHTML(pr), main.lastElementChild.firstElementChild); + }); } - const location = urlSplit.slice(-2).join('/') - if (location !== currentRepo) { - currentRepo = location; - getPRs(); - } -}, 1000); -function createPrHTML(pr) { - const div = document.createElement('div'); - const prState = pr.commits.nodes[0].commit.statusCheckRollup?.state || ''; - const prStateColor = prState === 'FAILURE' ? 'red' : (prState === 'SUCCESS' ? 'green' : 'yellow'); + setInterval(() => { + const urlSplit = document.URL.split('/'); + if (urlSplit.length !== 5) { + currentRepo = ''; + return; // Not A base repo URL + } - div.className = "d-sm-flex Box mb-2 Box-body color-bg-secondary"; - div.innerHTML = ` + const location = urlSplit.slice(-2).join('/') + if (location !== currentRepo) { + currentRepo = location; + getPRs(); + } + }, 1000); + + function createPrHTML(pr) { + const div = document.createElement('div'); + const prState = pr.commits.nodes[0].commit.statusCheckRollup?.state || ''; + const prStateColor = prState === 'FAILURE' ? 'red' : (prState === 'SUCCESS' ? 'green' : 'yellow'); + div.id = generatePrItemId(pr.number); + div.onclick = () => { + window.open(pr.url, '_blank'); + } + div.className = "btn d-sm-flex Box mb-2 Box-body color-bg-secondary"; + div.innerHTML = `
-
+
- ${prState} + #${pr.number} - ${prState}
-

+

${pr.title}

-
- Open
`; - return div; -} + return div; + } + + function generatePrItemId(prNumber) { + return `PR-${prNumber}`; + } +})(); +