Last active
May 27, 2021 12:19
-
-
Save moetelo/a1ce9f98e868c0116ab550ab93f9ffbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Highlight Commits | |
// @include http*://dev.azure.com/*/_git/*/commits* | |
// @author sheefoo | |
// @require https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js | |
// @require https://cdn.jsdelivr.net/npm/underscore@1.11.0/underscore-min.js | |
// ==/UserScript== | |
'use strict'; | |
// https://www.base64encode.org/ | |
const b64 = ''; // ':TOKEN' base64 enc | |
const headers = { | |
Authorization: `BASIC ${b64}`, | |
}; | |
const highlightRegex = /\d{14}_(.+)\.Designer\.cs/; | |
const regexedPathName = window.location.pathname.match('/(.+?)/(.+?)/(_git/)?commits.*'); | |
const organization = regexedPathName[1]; | |
const project = regexedPathName[2].split('/')[0]; | |
const repo = project; | |
console.log({organization, project, repo}); | |
let lastLocation = null; | |
setInterval(async () => { | |
// TODO: watch branch name | |
if (lastLocation !== location.href) { | |
lastLocation = location.href; | |
await refreshMigrationsUI(); | |
} | |
}, 1000); | |
document.querySelector('.scroll-auto-hide') | |
.addEventListener('scroll', _.debounce(refreshMigrationsUI, 500), false); | |
async function refreshMigrationsUI() { | |
const locationUrl = new URL(window.location); | |
const branch = document.querySelector('.version-dropdown').textContent; | |
console.log(`refreshing: ${branch}`); | |
try { | |
const reposResp = await getRepositories(); | |
const repositoryId = reposResp.data.value.filter(r => r.name == repo)[0].id; | |
const commitsResp = await getCommits(repositoryId, branch, 30); | |
const commitRows = [...document.querySelectorAll('.repos-commits-table-content .bolt-table-two-line-cell-item:last-child')]; | |
for (const c of commitsResp.data.value) { | |
const commitChangesResp = await getCommitChanges(repositoryId, c.commitId); | |
const changes = commitChangesResp.data.changes; | |
const highlighted = changes.map(ch => ch.item.path) | |
.filter(path => path.match(highlightRegex)) | |
.map(path => path.split('/').reverse()[0]); | |
if (highlighted.length) { | |
// const migrationNames = highlighted.map(m => m.match(highlightRegex)[1]); | |
const coloredSuccess = colorCommitRow(commitRows, c.commitId, 'rgb(152, 212, 152)'); | |
if (!coloredSuccess) { | |
console.log('exiting loop'); | |
break; | |
} | |
console.log(`\n${c.author.name}: ${c.comment}\n\n` | |
+ highlighted.join('\n') + '\n '); | |
} | |
} | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
function colorCommitRow(commitRows, commitId, color, migrationNames) { | |
const commitTxtCell = commitRows.find(r => | |
commitId.startsWith(r.innerText.split('\n')[0])); | |
if (!commitTxtCell) { | |
console.log('commit row not found: ' + commitId); | |
return false; | |
} | |
const $commitRow = $(commitTxtCell); | |
$commitRow.attr('style', `background-color: ${color} !important;`); | |
if (migrationNames) | |
$('.change-link', $commitRow).html(migrationNames.join('<br />')); | |
return true; | |
} | |
async function getRepositories() { | |
return await axios.get(`https://dev.azure.com/${organization}/${project}/_apis/git/repositories?api-version=5.1`, { headers }); | |
} | |
async function getCommits(repositoryId, branch, top) { | |
let url = `https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repositoryId}/commits?api-version=5.1`; | |
if (branch) url += '&searchCriteria.itemVersion.version=' + branch; | |
if (top) url += '&top=' + top; | |
return await axios.get(url, { headers }); | |
} | |
async function getCommitChanges(repositoryId, commitId) { | |
return await axios.get( | |
`https://dev.azure.com/${organization}/${project}/_apis/git/repositories/${repositoryId}/commits/${commitId}/changes?api-version=5.1`, | |
{ headers } | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment