Skip to content

Commit

Permalink
enhance board id and project key detection
Browse files Browse the repository at this point in the history
  • Loading branch information
rehangit committed Sep 3, 2021
1 parent 2ee05c0 commit eebc204
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 74 deletions.
150 changes: 77 additions & 73 deletions src/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ global.prStatus = global.prStatus || {};
global.prStatus.config = global.prStatus.config = {};
const prStatus = global.prStatus;

let JIRA_BOARD_ID;

const updateConfig = async () => {
const config = await new Promise(resolve =>
chrome.runtime.sendMessage({ action: "sendConfig" }, resolve),
Expand Down Expand Up @@ -58,40 +56,49 @@ const attribs = {
undefined: { text: "Undefined", color: "gray", svg: "" },
};

//https://company.atlassian.net/secure/RapidBoard.jspa?projectKey=ABCD&useStoredSettings=true&rapidView=123
//https://company.atlassian.net/jira/software/c/projects/ABCD/boards/123

const getBoardId = () =>
(document.location.href.match("rapidView=([0-9]+)") ||
document.location.href.match("/boards/([0-9]+)"))?.[1];

const getProjectKey = () =>
(document.location.href.match("projectKey=([0-9]+)") ||
document.location.href.match("/projects/([0-9]+)"))?.[1] ||
document.querySelector('meta[name="ghx-project-key"]')?.content ||
document
.querySelector(".ghx-issue[data-issue-key]")
?.dataset?.issueKey.split("-")?.[0];

let refreshing = false;
const refresh = async useCache => {
let JIRA_PROJECT_KEY;
try {
JIRA_PROJECT_KEY = (window.location.search.match(
"projectKey=([A-Z0-9]+)",
) || window.location.href.match("projectKey=([A-Z0-9]+)"))[1];
JIRA_BOARD_ID = (window.location.search.match(
".+atlassian.net/.+rapidView=([0-9]+)",
) || window.location.href.match(".+atlassian.net/.+/boards/([0-9]+)"))[1];

JIRA_BOARD_ID = (search.match("rapidView=([0-9]+)") ||
search.match("/boards/([0-9]+)"))[1];
} catch (err) {}

if (!JIRA_BOARD_ID || !JIRA_BOARD_ID.length) {
const config = prStatus.config;
const jiraBoardId = getBoardId();
const jiraBoardKey = getProjectKey();

if (
!jiraBoardId ||
!jiraBoardId.length ||
!jiraBoardKey ||
!jiraBoardKey.length
) {
refreshing = false;
logger.log("refresh aborted: jira board id not available in the url", {
...config,
search,
});
logger.debug(
"refresh aborted: jira board id not available in the url",
window.document.location.href,
config,
);
return;
}

const config = prStatus.config;
logger.debug("refresh triggered", config);
if (!config.GITHUB_TOKEN || !config.GITHUB_TOKEN.length) {
logger.error("refresh aborted: no github token", config);
refreshing = false;
return;
}

let GITHUB_ACCOUNT = config.GITHUB_ACCOUNT;

if (refreshing) {
logger.debug("Already refreshing...");
return;
Expand All @@ -104,9 +111,11 @@ const refresh = async useCache => {

let issuesUpdated = [];

let GITHUB_ACCOUNT = config.GITHUB_ACCOUNT;

if (issues.length) {
if (!JIRA_PROJECT_KEY || !JIRA_PROJECT_KEY.length) {
JIRA_PROJECT_KEY = issues[0].key.split("-")[0];
if (!jiraBoardKey || !jiraBoardKey.length) {
jiraBoardKey = issues[0].key.split("-")[0];
}

if (!GITHUB_ACCOUNT || !GITHUB_ACCOUNT.length) {
Expand All @@ -115,14 +124,14 @@ const refresh = async useCache => {
}

logger.debug("extracted details", {
JIRA_BOARD_ID,
JIRA_PROJECT_KEY,
jiraBoardId,
jiraBoardKey,
GITHUB_ACCOUNT,
});

const openPrs =
JIRA_PROJECT_KEY && JIRA_PROJECT_KEY.length
? await getOpenPrs(JIRA_PROJECT_KEY, GITHUB_ACCOUNT)
jiraBoardKey && jiraBoardKey.length
? await getOpenPrs(jiraBoardKey, GITHUB_ACCOUNT)
: [];
const draftPrs = openPrs.filter(pr => pr.draft);
logger.debug("all open prs from github", { openPrs, draftPrs });
Expand Down Expand Up @@ -228,7 +237,7 @@ const refresh = async useCache => {
nopr.name !== "master",
)
.map(nopr => {
logger.log("issue with noprs", { issue });
logger.debug("issue with noprs", { issue });
const repo = nopr.repository.url.split("/").slice(-1)[0];
return htmlToInsert({
...nopr,
Expand Down Expand Up @@ -299,52 +308,47 @@ const observeCallback = async (mutationsList, observer) => {
};

window.addEventListener("load", async e => {
try {
JIRA_BOARD_ID = (window.location.search.match(
".+atlassian.net/.+rapidView=([0-9]+)",
) || window.location.href.match(".+atlassian.net/.+/boards/([0-9]+)"))[1];
} catch (err) {
logger.log("Did not detect JIRA_BOARD_ID in the Url", window.location.href);
if (!getBoardId()) {
logger.log("Jira board id not detected in the url", window.location.href);
return;
}

if (JIRA_BOARD_ID && JIRA_BOARD_ID.length) {
logger.debug("content script load");
await updateConfig().then(refresh);
logger.debug("content script refreshed with config", prStatus.config);

const targetNode = document.querySelector("#ghx-work");
if (targetNode) {
const observer = new MutationObserver(observeCallback);
observer.observe(targetNode, { childList: true, subtree: true });

const throttledRefresh = throttle(async () => {
observer.pause = true;
try {
await refresh();
} catch (err) {
logger.error("refresh error", err);
}
observer.pause = false;
}, THROTTLE_RATE);

observer.refresh = throttledRefresh;
targetNode.addEventListener("dragend", throttledRefresh, false);
}

// targetNode.addEventListener("mouseup", throttledRefresh, false);

window.addEventListener("keydown", event => {
if (event.key === "Shift") {
chrome.runtime.sendMessage({ action: "shiftPressed" });
}
});
window.addEventListener("keyup", event => {
if (event.key === "Shift") {
chrome.runtime.sendMessage({ action: "shiftReleased" });
logger.debug("content script event load");
await updateConfig().then(refresh);
logger.debug("content script refreshed with config", prStatus.config);

const targetNode = document.querySelector("#ghx-work");
if (targetNode) {
const observer = new MutationObserver(observeCallback);
observer.observe(targetNode, { childList: true, subtree: true });

const throttledRefresh = throttle(async () => {
observer.pause = true;
try {
await refresh();
} catch (err) {
logger.error("refresh error", err);
}
});
observer.pause = false;
}, THROTTLE_RATE);

const refreshNow = document.querySelector(".js-refresh-now");
if (refreshNow) refresh.addEventListener("click", throttledRefresh, false);
observer.refresh = throttledRefresh;
targetNode.addEventListener("dragend", throttledRefresh, false);
}

// targetNode.addEventListener("mouseup", throttledRefresh, false);

window.addEventListener("keydown", event => {
if (event.key === "Shift") {
chrome.runtime.sendMessage({ action: "shiftPressed" });
}
});
window.addEventListener("keyup", event => {
if (event.key === "Shift") {
chrome.runtime.sendMessage({ action: "shiftReleased" });
}
});

const refreshNow = document.querySelector(".js-refresh-now");
if (refreshNow) refresh.addEventListener("click", throttledRefresh, false);
});
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "PR Status",
"description": "Show Github PR Review Approval status in the cards on a Jira board",
"version": "1.0.15",
"version": "1.0.16",
"background": { "scripts": ["background.js"], "persistent": false },
"browser_action": {
"default_icon": "icons/icon-active.png",
Expand Down

0 comments on commit eebc204

Please sign in to comment.