From 59b4246d0612017ceb267c57f49239a6e30ac489 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Fri, 11 Oct 2024 16:33:57 -0400 Subject: [PATCH] Add PR reviewer suggestions --- manifest.json | 2 +- repo-search-sorter.js | 73 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 31a1f5d..ca8a604 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.15", + "version": "1.0.16", "manifest_version": 2, "browser_specific_settings": { "gecko": { diff --git a/repo-search-sorter.js b/repo-search-sorter.js index 3293869..77a400f 100644 --- a/repo-search-sorter.js +++ b/repo-search-sorter.js @@ -33,4 +33,77 @@ const sortSearchResult = () => { } } +///////////////////////////// +// PR Reviewer Suggestions // +///////////////////////////// +if (location.toString().includes('/pull-requests?create')) { + const names = [ + "Tam Nguyen", + "David Daniel", + "Nikalas Culvey", + "Michael Mezzina", + "Sage Vaillancourt", + "John Cyr", + "Adam Weizman", + "Caleb Soper", + "Rockey Liles", + "Fernando Batista" + ] + + const urlPrefix = 'https://git.add123.com/rest/api/latest' + + fetch(`${urlPrefix}/projects/ADD/repos?limit=999`) + .then(resp => resp.json()) + .then(async ({values: allRepos}) => { + const prListUrls = allRepos.map(repo => `${urlPrefix}/projects/ADD/repos/${repo.slug}/pull-requests`) + const allPullRequests = (await Promise.all( + prListUrls.map(async prListUrl => await fetch(prListUrl).then(resp => resp.json()))) + ).flatMap(({values}) => values) + + const reviewerCounts = Object.fromEntries(names.map(name => [name, 0])) + allPullRequests.forEach(({reviewers}) => { + reviewers?.map(r => r.user.displayName) + .filter(displayName => !["Chris Gamache", "Tom Keels"].includes(displayName)) + .forEach(displayName => { + reviewerCounts[displayName] ??= 0 + reviewerCounts[displayName] += 1 + }) + }) + const sortedCounts = Object.entries(reviewerCounts) + .map(([name, count]) => ({ name, count })) + .toSorted((a, b) => a.count - b.count) + const tryDisplay = () => setTimeout(() => { + if (document.getElementById('reviewer-counts')) { + return + } + const formBodyMain = document.querySelector('.create-pull-request-form .form-body .form-body-main') + if (formBodyMain) { + const countsParent = formBodyMain.lastChild.cloneNode(true) + const reviewerCountsNode = document.createElement('ol') + reviewerCountsNode.id = 'reviewer-counts' + reviewerCountsNode.style.columns = '3' + reviewerCountsNode.style.listStyle = 'none' + reviewerCountsNode.style.paddingLeft = '2px' + reviewerCountsNode.style.marginTop = 'auto' + // reviewerCountsNode.style.lineHeight = '95%' + reviewerCountsNode.style.fontSize = '0.9em' + sortedCounts.slice(0, 6).forEach(({ name, count }) => { + const listItem = document.createElement('li') + listItem.innerText = `${count} PR${count !== 1 ? 's' : 's'} – ${name}` + reviewerCountsNode.appendChild(listItem) + }) + + const label = countsParent.querySelector('.form-label') + label.innerText = 'Now reviewing' + const message = countsParent.querySelector('.form-field-content') + message.replaceChildren(reviewerCountsNode) + formBodyMain.appendChild(countsParent) + } else { + tryDisplay() + } + }, 300) + tryDisplay() + }) +} + addFix(sortSearchResult)