diff --git a/manifest.json b/manifest.json index 8fbedf4..b872e2b 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.17", + "version": "1.0.18", "manifest_version": 2, "browser_specific_settings": { "gecko": { diff --git a/server/public/sages-bitbucket-addon-1.0.18.xpi b/server/public/sages-bitbucket-addon-1.0.18.xpi new file mode 100644 index 0000000..193c163 Binary files /dev/null and b/server/public/sages-bitbucket-addon-1.0.18.xpi differ diff --git a/show-errored-prs.js b/show-errored-prs.js index 8bfd65a..2830c3f 100644 --- a/show-errored-prs.js +++ b/show-errored-prs.js @@ -1,29 +1,51 @@ const bbUrlPrefix = 'https://git.add123.com/rest/api/latest' const pullRequestHasErrored = async pr => { - const latestCommitId = pr.fromRef.latestCommit + const latestCommitId = pr?.fromRef?.latestCommit + if (!latestCommitId) { + console.log('no latestCommitId on pr', pr) + return false + } 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 getAllRepos = async () => { + const allProjects = (await fetch(`${bbUrlPrefix}/projects`).then(resp => resp.json())).values + const allProjectKeys = allProjects.map(project => project.key) + let allRepos = await Promise.all(allProjectKeys.map(async key => + await fetch(`${bbUrlPrefix}/projects/${key}/repos?limit=999`) + .then(resp => resp.json()).then(({ values }) => values))) + allRepos = allRepos.flatMap(a => a) + return allRepos +} + +const withErroredPrs = async action => { + const allPullRequests = (await getAllRepos() + .then(async 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) + }).catch(console.error)).filter(Boolean) const erroredPullRequests = [] for (const pullRequest of allPullRequests) { if (await pullRequestHasErrored(pullRequest)) { - erroredPullRequests.push(pullRequest) + action(pullRequest) } } return erroredPullRequests } +const getElementAfterErrorList = mainPanel => { + for (const child of mainPanel.children) { + if (child.innerHTML.includes('Recently closed pull requests')) { + return child + } + } + return mainPanel.children[1] +} + const showErroredPrs = async () => { if (getCurrentUser() !== 'sagevaillancourt') { return @@ -44,12 +66,11 @@ const showErroredPrs = async () => { errorList.style.fontSize = '0.9em' errorParent.appendChild(errorList) - mainPanel.insertBefore(errorParent, mainPanel.children[1]) + mainPanel.insertBefore(errorParent, getElementAfterErrorList(mainPanel)) } Array.from(errorList.children).slice(1).forEach(child => errorList.removeChild(child)) - const erroredPrs = await getErroredPrs() - erroredPrs.forEach(pr => { + await withErroredPrs(pr => { const element = document.createElement('li') const link = document.createElement('a') link.href = pr.links.self[0].href