const summaryAnswerID = 380354; // ID of the summary answer
const key = "U4DMV*8nvpm3EOpvf69Rxw(("; // taken from API documentation
const typeTagRegex = /\[(meta-)?tag:(discussion|support|feature-request|bug)\]/i;
const statusTagRegex = /\[(meta-)?tag:(status-[-\w]+)\]/i;
const summaryRegex = /#+\s+(.*)/i;
const gitHubLinkRegex = /\/\/github.com\/\S+\/issues\/(\d+)/i;
const site = "meta";
async function main() {
// Find question ID and current summary
var result = await $.get("https://api.stackexchange.com/2.3/answers/" + summaryAnswerID + "?site=" + site + "&filter=!nKzQURFm*e&key=" + key);
let questionID = result.items[0]["question_id"];
let summaryBody = result.items[0]["body"];
let summaryMarkdown = result.items[0]["body_markdown"];
let lines = summaryMarkdown.split(/\r?\n/);
var linesToAdd = [];
var linesToChange = {};
var page = 0;
while (true) {
// Fetch answers
result = await $.get("https://api.stackexchange.com/2.3/questions/380295/answers?page=" + ++page +
"&pagesize=100&order=asc&sort=creation&site=" + site + "&filter=!nKzQURFm*e&key=" + key);
for (let item of result.items) {
// Summary?
let answerID = item["answer_id"];
if (answerID == summaryAnswerID)
continue;
// Present in summary?
var currentLine = null;
for (let line of lines) {
if (line.search(new RegExp('/a/' + answerID + '[/)]')) != -1) {
currentLine = line;
break;
}
}
// Analyze answer
let markdown = item["body_markdown"];
let typeTagMatch = typeTagRegex.exec(markdown);
let summaryMatch = summaryRegex.exec(markdown);
let statusTagMatch = statusTagRegex.exec(markdown);
let gitHubLinkMatch = gitHubLinkRegex.exec(markdown);
if (currentLine == null) {
// Generate new entry
let title = summaryMatch == null ? "(please add summary)" : summaryMatch[1];
let summary = "[" + title + "](/a/" + answerID + ")";
linesToAdd.push(generateLine(typeTagMatch, summary, statusTagMatch, gitHubLinkMatch));
} else {
// Compare against summary
let components = currentLine.split("|");
let typeTag = typeTagRegex.exec(components[1].trim());
let summary = components[2].trim();
let statusTag = statusTagRegex.exec(components[3].trim());
let gitHubLink = gitHubLinkRegex.exec(components[4].trim()); // may be absent, if GitHub isn't used
var changeNeeded = typeTagMatch != null && (typeTag == null || typeTag[2].toLowerCase() != typeTagMatch[2].toLowerCase());
changeNeeded |= statusTagMatch != null && (statusTag == null || statusTag[2].toLowerCase() != statusTagMatch[2].toLowerCase());
changeNeeded |= gitHubLinkMatch != null && (gitHubLink == null || gitHubLink[1].toLowerCase() != gitHubLinkMatch[1].toLowerCase());
if (!changeNeeded)
continue;
linesToChange[currentLine] = generateLine(typeTagMatch, summary, statusTagMatch, gitHubLinkMatch);
}
}
// Any more answers?
if (!result["has_more"])
break;
}
// Changes needed?
if (linesToAdd.length == 0 && Object.keys(linesToChange).length == 0) {
alert("The summary is up-to-date.");
return;
}
var messages = [];
if (linesToAdd.length != 0) {
messages.push("\nThe following lines need to be added:");
for (let lineToAdd of linesToAdd) {
messages.push(lineToAdd);
}
}
if (Object.keys(linesToChange).length != 0) {
messages.push("\nThe following lines need to be changed:");
for (let lineToChange in linesToChange) {
messages.push("\n" + lineToChange + "\n-->\n" + linesToChange[lineToChange] + "\n");
}
}
console.log(messages.join("\n"));
alert("Changes needed, please check the console for details.")
}
main();
function generateLine(typeTagMatch, summary, statusTagMatch, gitHubLinkMatch) {
var components = [""];
components.push(typeTagMatch == null ? "(please add type)" : "[tag:" + typeTagMatch[2] + "]");
components.push(summary);
components.push(statusTagMatch == null ? "" : "[tag:" + statusTagMatch[2] + "]");
if (gitHubLinkMatch != null) {
components.push("[#" + gitHubLinkMatch[1] + "](https:" + gitHubLinkMatch[0] + ")");
}
components.push("");
return components.join(" | ").trim();
}
<script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript" ></script>
<script src="https://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript" ></script>
const summaryAnswerID = 380354; // ID of the summary answer
const key = "U4DMV*8nvpm3EOpvf69Rxw(("; // taken from API documentation
const typeTagRegex = /\[(meta-)?tag:(discussion|support|feature-request|bug)\]/i;
const statusTagRegex = /\[(meta-)?tag:(status-[-\w]+)\]/i;
const summaryRegex = /#+\s+(.*)/i;
const gitHubLinkRegex = /\/\/github.com\/\S+\/issues\/(\d+)/i;
const site = "meta";
async function main() {
// Find question ID and current summary
var result = await $.get("https://api.stackexchange.com/2.3/answers/" + summaryAnswerID + "?site=" + site + "&filter=!nKzQURFm*e&key=" + key);
let questionID = result.items[0]["question_id"];
let summaryBody = result.items[0]["body"];
let summaryMarkdown = result.items[0]["body_markdown"];
let lines = summaryMarkdown.split(/\r?\n/);
var linesToAdd = [];
var linesToChange = {};
var page = 0;
while (true) {
// Fetch answers
result = await $.get("https://api.stackexchange.com/2.3/questions/380295/answers?page=" + ++page +
"&pagesize=100&order=asc&sort=creation&site=" + site + "&filter=!nKzQURFm*e&key=" + key);
for (let item of result.items) {
// Summary?
let answerID = item["answer_id"];
if (answerID == summaryAnswerID)
continue;
// Present in summary?
var currentLine = null;
for (let line of lines) {
if (line.search(new RegExp('/a/' + answerID + '[/)]')) != -1) {
currentLine = line;
break;
}
}
// Analyze answer
let markdown = item["body_markdown"];
let typeTagMatch = typeTagRegex.exec(markdown);
let summaryMatch = summaryRegex.exec(markdown);
let statusTagMatch = statusTagRegex.exec(markdown);
let gitHubLinkMatch = gitHubLinkRegex.exec(markdown);
if (currentLine == null) {
// Generate new entry
let title = summaryMatch == null ? "(please add summary)" : summaryMatch[1];
let summary = "[" + title + "](/a/" + answerID + ")";
linesToAdd.push(generateLine(typeTagMatch, summary, statusTagMatch, gitHubLinkMatch));
} else {
// Compare against summary
let components = currentLine.split("|");
let typeTag = typeTagRegex.exec(components[1].trim());
let summary = components[2].trim();
let statusTag = statusTagRegex.exec(components[3].trim());
let gitHubLink = gitHubLinkRegex.exec(components[4].trim()); // may be absent, if GitHub isn't used
var changeNeeded = typeTagMatch != null && (typeTag == null || typeTag[2].toLowerCase() != typeTagMatch[2].toLowerCase());
changeNeeded |= statusTagMatch != null && (statusTag == null || statusTag[2].toLowerCase() != statusTagMatch[2].toLowerCase());
changeNeeded |= gitHubLinkMatch != null && (gitHubLink == null || gitHubLink[1].toLowerCase() != gitHubLinkMatch[1].toLowerCase());
if (!changeNeeded)
continue;
linesToChange[currentLine] = generateLine(typeTagMatch, summary, statusTagMatch, gitHubLinkMatch);
}
}
// Any more answers?
if (!result["has_more"])
break;
}
// Changes needed?
if (linesToAdd.length == 0 && Object.keys(linesToChange).length == 0) {
alert("The summary is up-to-date.");
return;
}
var messages = [];
if (linesToAdd.length != 0) {
messages.push("\nThe following lines need to be added:");
for (let lineToAdd of linesToAdd) {
messages.push(lineToAdd);
}
}
if (Object.keys(linesToChange).length != 0) {
messages.push("\nThe following lines need to be changed:");
for (let lineToChange in linesToChange) {
messages.push("\n" + lineToChange + "\n-->\n" + linesToChange[lineToChange] + "\n");
}
}
console.log(messages.join("\n"));
alert("Changes needed, please check the console for details.")
}
main();
function generateLine(typeTagMatch, summary, statusTagMatch, gitHubLinkMatch) {
var components = [""];
components.push(typeTagMatch == null ? "(please add type)" : "[tag:" + typeTagMatch[2] + "]");
components.push(summary);
components.push(statusTagMatch == null ? "" : "[tag:" + statusTagMatch[2] + "]");
if (gitHubLinkMatch != null) {
components.push("[#" + gitHubLinkMatch[1] + "](https:" + gitHubLinkMatch[0] + ")");
}
components.push("");
return components.join(" | ").trim();
}