From 2e2ad9556a917bc455195af6613da89ff7ad8c52 Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Tue, 6 Jul 2021 11:44:12 +0300 Subject: [PATCH] Added small script to list your PRs in a repository page. --- .gitignore | 1 + RH_jira_bz_linker.js | 10 ++--- github_list_own_prs_in_repo.js | 72 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 github_list_own_prs_in_repo.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..50e403d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.variables \ No newline at end of file diff --git a/RH_jira_bz_linker.js b/RH_jira_bz_linker.js index c621ab8..cc200ce 100644 --- a/RH_jira_bz_linker.js +++ b/RH_jira_bz_linker.js @@ -1,8 +1,8 @@ - // ==UserScript== // @name RedHat jira BZ Linker // @version 0.0.1 // @namespace http://arantius.com/misc/greasemonkey/ +// @author Sagi Dayan // @description Adds BZ links in (RedHat) Jira when needed // @match https://issues.redhat.com/* // ==/UserScript== @@ -18,8 +18,8 @@ observer.observe(document.body, { childList: true, characterData: true, subtree: // adds a tag prefix to logs for easy filters function log(...args) { const msg = args.reduce((msg, data) => { - return `${msg} ${data}` - }, LOG_TAG) + return `${msg} ${data}`; + }, LOG_TAG); console.log(msg); } @@ -28,7 +28,7 @@ function update() { const issues = filterAndInflate([...document.getElementsByClassName("issuerow")]); if (issues.length) log(`Found #${issues.length} new unlinked issues. Linking...`); issues.forEach(issue => { - const issueId = issue.bzId + const issueId = issue.bzId; if (issueId) { const link = document.createElement('a'); const p = document.createElement('p'); @@ -49,7 +49,7 @@ function filterAndInflate(rows) { if (r.tagName === 'TR' && !r.touched) { const summary = [...r.children].reduce((prev, child) => { if (prev) return prev; - if (child.getAttribute('data-field-id') === 'summary' + if (child.getAttribute('data-field-id') === 'summary' || child.className.indexOf('summary') != -1) { r.summaryElm = child; return child.innerHTML; diff --git a/github_list_own_prs_in_repo.js b/github_list_own_prs_in_repo.js new file mode 100644 index 0000000..6b08e46 --- /dev/null +++ b/github_list_own_prs_in_repo.js @@ -0,0 +1,72 @@ +// ==UserScript== +// @name GitHub - list own PRs in a repository +// @description A brief description of your script +// @namespace http://arantius.com/misc/greasemonkey/ +// @author Sagi Dayan +// @match https://github.com/* +// @version 1.0 +// ==/UserScript== + +// Update These +const GITHUB_AUTH = ''; // https://github.com/settings/tokens +const GITHUB_USERNAME = ''; +// + + +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; + }); + + 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) return; // Not A base repo URL + + 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.className = "d-sm-flex Box mb-2 Box-body color-bg-secondary"; + div.innerHTML = ` +
+
+ + ${prState} + +
+

+ ${pr.title} +

+
+ Open +
+`; + return div; +} \ No newline at end of file