Skip to content

Commit

Permalink
fix: Use new url for pull requests list API.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Nov 7, 2024
1 parent 8d51b5e commit 1b6563d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 80 deletions.
1 change: 0 additions & 1 deletion .ruby-version

This file was deleted.

199 changes: 120 additions & 79 deletions toktok/static/js/pr-table.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Polyfill for replaceWith for IE, Firefox < v49, and Safari
// from: https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/replaceWith()/replaceWith().md
// from:
// https://github.com/jserz/js_piece/blob/master/DOM/ChildNode/replaceWith()/replaceWith().md
(function (arr) {
arr.forEach(function (item) {
if (item.hasOwnProperty("replaceWith")) {
Expand All @@ -15,20 +16,22 @@

argArr.forEach(function (argItem) {
var isNode = argItem instanceof Node;
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem)));
docFrag.appendChild(
isNode ? argItem : document.createTextNode(String(argItem)),
);
});

this.parentNode.replaceChild(docFrag, this);
}
},
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);

function escapeHTML(text) {
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
return text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;");
}

function reloadPrTable() {
Expand All @@ -39,10 +42,12 @@ function reloadPrTable() {
reloadButton.className += " pr-reloading";

var requestHeaders = new Headers();
var requestInit = { method: "GET",
headers: requestHeaders,
mode: "cors",
cache: "default" };
var requestInit = {
method: "GET",
headers: requestHeaders,
mode: "cors",
cache: "default",
};

var stateIcon = {
clean: "&#x2705;",
Expand All @@ -54,76 +59,112 @@ function reloadPrTable() {
unstable: "&#x1f6a7;",
};

fetch("https://git-critique.herokuapp.com/hello/pulls", requestInit)
.then(function(response) { return response.json(); })
.then(function(json) {
var newRepoSection = document.createElement("div");
newRepoSection.className = "tables-wrapper"

for (var i = 0; i < json.length; i++) {
if (json[i].length > 0) {
var repoTitle = document.createElement("h2");
repoTitle.innerHTML = json[i][0].prRepoName;
newRepoSection.appendChild(repoTitle);

var prTable = document.createElement("table");
prTable.className = "pr-table"
var prHeaders = document.createElement("tr");
prHeaders.innerHTML =
"<th>" + "#" + "</th>" +
"<th>" + "Branch" + "</th>" +
"<th>" + "Title" + "</th>" +
"<th>" + "State" + "</th>" +
"<th>" + "Reviewers" + "</th>";
prTable.appendChild(prHeaders);

for (var j = 0; j < json[i].length; j++) {
var listItem = document.createElement("tr");
var reviewableBranch =
"<a href='https://reviewable.io/reviews/toktok/" + json[i][j].prRepoName +
"/" + json[i][j].prNumber + "'>" +
escapeHTML(json[i][j].prBranch).replace(/_/g, "_<wbr>") +
"</a>";

var githubNumber =
" <a href='https://github.com/TokTok/" + json[i][j].prRepoName +
"/pull/" + json[i][j].prNumber + "'>" +
json[i][j].prNumber + "</a>";

// Insert <wbr> tags after _ characters for neater word-wrapping.
var titleWithTooltip =
"<div class='tooltip'>" +
escapeHTML(json[i][j].prTitle).replace(/_/g, "_<wbr>") +
"&#8203;<span class='tooltiptext'>" + escapeHTML(json[i][j].prUser) + "</span>" +
"</div>";

var dayInMs = 24*60*60*1000;
var ageInDays = Math.round((Date.now() - Date.parse(json[i][j].prCreated)) / dayInMs);
var stateWithTooltip =
"<div class='tooltip'>" + stateIcon[json[i][j].prState] +
"&#8203;<span class='tooltiptext'>Created " + ageInDays +
(ageInDays == 1 ? " day ago." : " days ago.") +
"</span>" + "</div>";

listItem.innerHTML =
"<td>" + githubNumber + "</td>" +
"<td>" + reviewableBranch + "</td>" +
"<td>" + titleWithTooltip + "</td>" +
"<td>" + stateWithTooltip + "</td>" +
"<td>" + json[i][j].prReviewers.join(", ") + "</td>";

prTable.appendChild(listItem);
fetch("https://github-tools.onrender.com/hello/pulls", requestInit)
.then(function (response) {
return response.json();
})
.then(function (json) {
var newRepoSection = document.createElement("div");
newRepoSection.className = "tables-wrapper";

for (var i = 0; i < json.length; i++) {
if (json[i].length > 0) {
var repoTitle = document.createElement("h2");
repoTitle.innerHTML = json[i][0].prRepoName;
newRepoSection.appendChild(repoTitle);

var prTable = document.createElement("table");
prTable.className = "pr-table";
var prHeaders = document.createElement("tr");
prHeaders.innerHTML =
"<th>" +
"#" +
"</th>" +
"<th>" +
"Branch" +
"</th>" +
"<th>" +
"Title" +
"</th>" +
"<th>" +
"State" +
"</th>" +
"<th>" +
"Reviewers" +
"</th>";
prTable.appendChild(prHeaders);

for (var j = 0; j < json[i].length; j++) {
var listItem = document.createElement("tr");
var reviewableBranch =
"<a href='https://reviewable.io/reviews/toktok/" +
json[i][j].prRepoName +
"/" +
json[i][j].prNumber +
"'>" +
escapeHTML(json[i][j].prBranch).replace(/_/g, "_<wbr>") +
"</a>";

var githubNumber =
" <a href='https://github.com/TokTok/" +
json[i][j].prRepoName +
"/pull/" +
json[i][j].prNumber +
"'>" +
json[i][j].prNumber +
"</a>";

// Insert <wbr> tags after _ characters for neater word-wrapping.
var titleWithTooltip =
"<div class='tooltip'>" +
escapeHTML(json[i][j].prTitle).replace(/_/g, "_<wbr>") +
"&#8203;<span class='tooltiptext'>" +
escapeHTML(json[i][j].prUser) +
"</span>" +
"</div>";

var dayInMs = 24 * 60 * 60 * 1000;
var ageInDays = Math.round(
(Date.now() - Date.parse(json[i][j].prCreated)) / dayInMs,
);
var stateWithTooltip =
"<div class='tooltip'>" +
stateIcon[json[i][j].prState] +
"&#8203;<span class='tooltiptext'>Created " +
ageInDays +
(ageInDays == 1 ? " day ago." : " days ago.") +
"</span>" +
"</div>";

listItem.innerHTML =
"<td>" +
githubNumber +
"</td>" +
"<td>" +
reviewableBranch +
"</td>" +
"<td>" +
titleWithTooltip +
"</td>" +
"<td>" +
stateWithTooltip +
"</td>" +
"<td>" +
json[i][j].prReviewers.join(", ") +
"</td>";

prTable.appendChild(listItem);
}
newRepoSection.appendChild(prTable);
}
newRepoSection.appendChild(prTable);
}
}
var repoSection = document.querySelector(".tables-wrapper");
repoSection.replaceWith(newRepoSection);
reloadButton.className = reloadButtonClass;
var end = new Date();
var deltaTime = end.getTime() - start.getTime();
console.log("PR table took %s seconds to load.", (deltaTime)/1000.0);
});
var repoSection = document.querySelector(".tables-wrapper");
repoSection.replaceWith(newRepoSection);
reloadButton.className = reloadButtonClass;
var end = new Date();
var deltaTime = end.getTime() - start.getTime();
console.log("PR table took %s seconds to load.", deltaTime / 1000.0);
});
}

reloadPrTable();

0 comments on commit 1b6563d

Please sign in to comment.