Skip to content

Instantly share code, notes, and snippets.

@ChrisPritchard
Created May 26, 2022 20:56
Show Gist options
  • Save ChrisPritchard/bfd20fe89737d4ce071d1323d73353bb to your computer and use it in GitHub Desktop.
Save ChrisPritchard/bfd20fe89737d4ce071d1323d73353bb to your computer and use it in GitHub Desktop.
Simple JS Page that retrieves latest commits from all Azure DevOps repos in a project, with optional filters
<html>
<head>
<title>Latest Commits</title>
<style>
label {
display:block;
}
</style>
</head>
<body>
<div id="controls">
<label>Personal Access Token<br/><input type="text" id="pat" /></label>
<label>From Date<br/><input type="date" id="from" /></label>
<label>Repo Filter<br/><input type="text" id="filter" /></label>
<input type="button" id="loadCommits" value="Load Commits" />
</div>
<div id="repos">
</div>
<script>
var repoUrl = "https://dev.azure.com/[orgname]/[projectname]/_apis/git/repositories?api-version=6.0";
var commits = "/commits?api-version=6.0";
var filters = "";
var target = document.getElementById("repos");
pat = "";
document.getElementById("loadCommits").addEventListener("click", loadRepos);
function loadRepos() {
pat = document.getElementById("pat").value;
if(!pat) {
target.innerHTML = "please specify a valid personal access token";
return;
}
filters = "";
let fromDate = document.getElementById("from").value;
if(fromDate) {
filters += "&searchCriteria.fromDate=" + fromDate;
}
let textFilter = document.getElementById("filter").value;
target.innerHTML = "loading...";
var request = new XMLHttpRequest();
request.open('GET', repoUrl, true);
request.setRequestHeader('Content-Type', 'application/json; charset=utf-8;');
request.setRequestHeader('Authorization', 'Basic '+btoa(':'+pat));
request.onload = function() {
if(this.status >= 200 && this.status < 400) {
target.innerHTML = "";
var repos = JSON.parse(this.response);
for (var i = 0; i < repos.value.length; i++) {
var repo = repos.value[i];
if (!textFilter || repo.name.includes(textFilter)) {
getCommits(repo.name, repo.url);
}
}
}
};
request.send();
}
function getCommits(name, url) {
var request = new XMLHttpRequest();
request.open('GET', url+commits+filters, true);
request.setRequestHeader('Content-Type', 'application/json; charset=utf-8;');
request.setRequestHeader('Authorization', 'Basic '+btoa(':'+pat));
request.onload = function() {
if(this.status >= 200 && this.status < 400) {
var commits = JSON.parse(this.response);
printCommits(name, url, commits);
}
};
request.send();
}
function printCommits(name, url, commits) {
let container = document.createElement("div");
let heading = document.createElement("h2");
let link = document.createElement("a");
link.setAttribute("href", url);
link.innerText = name;
heading.appendChild(link);
container.appendChild(heading);
if(commits.count > 0) {
for (var i = 0; i < commits.value.length; i++) {
container.appendChild(printCommitInfo(commits.value[i]));
}
} else {
let noCommits = document.createElement("div");
noCommits.innerText = "no commits in the specified time period";
container.appendChild(noCommits);
}
target.appendChild(container);
}
function printCommitInfo(commit) {
let commitInfo = document.createElement("div");
let heading = document.createElement("h3");
let link = document.createElement("a");
link.setAttribute("href", commit.remoteUrl);
link.innerText = `Commit by ${commit.author.name} on ${commit.author.date}`;
heading.appendChild(link);
commitInfo.appendChild(heading);
let comment = document.createElement("p");
if (commit.comment) {
comment.innerText = commit.comment;
} else {
comment.innerText = "no comment provided"
}
commitInfo.appendChild(comment);
return commitInfo;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment