const bbUrlPrefix = 'https://git.add123.com/rest/api/latest' const pullRequestHasErrored = async pr => { const latestCommitId = pr?.fromRef?.latestCommit if (!latestCommitId) { 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 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 getErroredPrs = async () => { 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)).filter(Boolean) const erroredPullRequests = [] for (const pullRequest of allPullRequests) { if (await pullRequestHasErrored(pullRequest)) { erroredPullRequests.push(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 buildPrRow = pr => { const row = document.createElement('tr') row.className = 'pull-request-row' const avatar = document.createElement('td') avatar.className = 'avatar-column' avatar.innerHTML = `
` row.appendChild(avatar) const summaryColumn = document.createElement('td') summaryColumn.className = 'summary-column' row.appendChild(summaryColumn) const prSummary = document.createElement('div') prSummary.className = 'pr-summary' summaryColumn.appendChild(prSummary) const title = document.createElement('div') title.className = 'title' prSummary.appendChild(title) const link = document.createElement('a') link.href = pr.links.self[0].href link.innerText = pr.title title.appendChild(link) const details = document.createElement('div') details.className = 'details' prSummary.appendChild(details) console.log('pr', JSON.stringify(pr, null, 2)) details.innerHTML = ` ${pr.author.user.displayName} - #${pr.id} ${pr.toRef.repository.project.name} / ${pr.toRef.repository.name} ${pr.toRef.displayId} ` const placeholderColumn = document.createElement('td') placeholderColumn.className = 'placeholder-column' placeholderColumn.innerHTML = ' ' row.appendChild(placeholderColumn) const buildColumn = document.createElement('td') buildColumn.className = 'build-column' buildColumn.innerHTML = ' ' row.appendChild(buildColumn) return row } 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('closed-pull-requests')[0].cloneNode() errorParent.id = 'bb-addon-pr-error-table' const header = document.createElement('h3') header.innerText = 'Pull requests with errors' errorParent.appendChild(header) const table = document.createElement('table') errorParent.appendChild(table) const thead = document.createElement('thead') table.appendChild(thead) const tr = document.createElement('tr') thead.appendChild(tr) const summaryColumn = document.createElement('th') summaryColumn.className = 'summary-column' summaryColumn.colSpan = 6 tr.appendChild(summaryColumn) const reviewersColumn = document.createElement('th') reviewersColumn.className = 'reviewers-column' reviewersColumn.innerHTML = ' ' tr.appendChild(reviewersColumn) const buildsColumn = document.createElement('th') buildsColumn.className = 'builds-column' buildsColumn.innerHTML = ' ' tr.appendChild(buildsColumn) errorList = document.createElement('tbody') table.appendChild(errorList) errorList.id = 'bb-addon-pr-error-list' mainPanel.insertBefore(errorParent, getElementAfterErrorList(mainPanel)) } const erroredPrs = await getErroredPrs() Array.from(errorList.children).forEach(child => errorList.removeChild(child)) erroredPrs.map(buildPrRow).forEach(row => errorList.appendChild(row)) } if (location.toString().includes('/dashboard')) { addFix(() => showErroredPrs().catch(console.error)) setInterval(() => showErroredPrs().catch(console.error), 1000 * 60 * 2) }