diff --git a/manifest.json b/manifest.json index ca8a604..8fbedf4 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "Sage's BitBucket Addon", "description": "Ensure searches don't include forks (and other enhancements)", - "version": "1.0.16", + "version": "1.0.17", "manifest_version": 2, "browser_specific_settings": { "gecko": { @@ -31,6 +31,7 @@ "csharp-quotes.js", "file-scroll.js", "repo-search-sorter.js", + "show-errored-prs.js", "var-highlighter.js" ], "run_at": "document_start" diff --git a/server/public/sages-bitbucket-addon-1.0.17.xpi b/server/public/sages-bitbucket-addon-1.0.17.xpi new file mode 100644 index 0000000..7d3c0ce Binary files /dev/null and b/server/public/sages-bitbucket-addon-1.0.17.xpi differ diff --git a/show-errored-prs.js b/show-errored-prs.js new file mode 100644 index 0000000..8bfd65a --- /dev/null +++ b/show-errored-prs.js @@ -0,0 +1,66 @@ +const bbUrlPrefix = 'https://git.add123.com/rest/api/latest' + +const pullRequestHasErrored = async pr => { + const latestCommitId = pr.fromRef.latestCommit + const buildStats = await fetch(`https://git.add123.com/rest/build-status/latest/commits/stats/${latestCommitId}`).then(resp => resp.json()) + return buildStats.failed !== 0 +} + +const getErroredPrs = async () => { + const allPullRequests = await fetch(`${bbUrlPrefix}/projects/ADD/repos?limit=999`) + .then(resp => resp.json()) + .then(async ({values: allRepos}) => { + const prListUrls = allRepos.map(repo => `${bbUrlPrefix}/projects/ADD/repos/${repo.slug}/pull-requests`) + return (await Promise.all( + prListUrls.map(async prListUrl => await fetch(prListUrl).then(resp => resp.json()))) + ).flatMap(({values}) => values) + }).catch(console.error) + const erroredPullRequests = [] + for (const pullRequest of allPullRequests) { + if (await pullRequestHasErrored(pullRequest)) { + erroredPullRequests.push(pullRequest) + } + } + return erroredPullRequests +} + +const showErroredPrs = async () => { + if (getCurrentUser() !== 'sagevaillancourt') { + return + } + const mainPanel = document.getElementsByClassName('main-panel')[0] + let errorParent = document.getElementById('bb-addon-pr-error-table') + let errorList = document.getElementById('bb-addon-pr-error-list') + if (!errorParent) { + errorParent = mainPanel.getElementsByClassName('dashboard-pull-request-table')[0].cloneNode() + + const header = document.createElement('h3') + header.innerText = 'Pull requests with errors' + errorParent.appendChild(header) + + errorList = document.createElement('ul') + errorList.id = 'bb-addon-pr-error-list' + errorList.style.listStyle = 'none' + errorList.style.fontSize = '0.9em' + errorParent.appendChild(errorList) + + mainPanel.insertBefore(errorParent, mainPanel.children[1]) + } + Array.from(errorList.children).slice(1).forEach(child => errorList.removeChild(child)) + + const erroredPrs = await getErroredPrs() + erroredPrs.forEach(pr => { + const element = document.createElement('li') + const link = document.createElement('a') + link.href = pr.links.self[0].href + link.innerText = pr.title + link.style.color = 'black' + element.appendChild(link) + errorList.appendChild(element) + }) +} + +if (location.toString().includes('/dashboard')) { + addFix(() => showErroredPrs().catch(console.error)) + setInterval(() => showErroredPrs().catch(console.error), 1000 * 60 * 5) +} \ No newline at end of file