Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GitHub Status script #2240

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 73 additions & 11 deletions tools/status-tools/github-status.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require("fs");
const child_process = require("child_process");
const NO_LABELS = "<No Labels>";
const NO_MILESTONE = "<No Milestone>";
const DEFAULT_LABELS = [ "bug", "enhancement", "feature", "question", "documentation", "duplicate", "invalid", "wontfix" ];

let _startMonth = 0;
Expand All @@ -12,6 +13,7 @@
let _noLabels = false;
let _missingLabels = false;
let _prevMonths = 6;
let _dump = false;

function showHelp() {
var scriptParts;
Expand All @@ -38,6 +40,7 @@
console.log(" -noLabels - Don't report on any labels (overrides -l, -px and -all)");
console.log(" -missingLabels - Identify issues with no assigned labels");
console.log(" -noDefault - Don't add the default labels (bug,enhancement,feature,question,documentation,duplicate,invalid,wontfix)");
console.log(" -dump - Dump the raw JSON data to a file (issues.json)")

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (96% of all statements in
the enclosing function
have an explicit semicolon).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(" -dump - Dump the raw JSON data to a file (issues.json)")
console.log(" -dump - Dump the raw JSON data to a file (issues.json)");

console.log(" -? | -h | -help - This help message");
console.log("");
console.log("Examples:");
Expand Down Expand Up @@ -129,6 +132,8 @@
if (_prevMonths < 1) {
_prevMonths = 1;
}
} else if (theArg === "-dump") {
_dump = true;
} else {
console.error("!!! Unknown switch [" + theArg + "] detected");
return false;
Expand Down Expand Up @@ -266,7 +271,7 @@
}
}

function sumValues(issues, createdAt, closedAt) {
function sumValues(issues, createdAt, closedAt, issue) {
let openYear = createdAt.getFullYear();
let openMonth = String(createdAt.getMonth() + 1).padStart(2, "0");
let year = issues[openYear] = issues[openYear] || { cnt: 0, opened: 0, closed: 0 };
Expand All @@ -276,7 +281,29 @@
let month = year[openMonth] = year[openMonth] || { cnt: 0, opened: 0, closed: 0 };
month.opened++;
month.cnt++;

if (issue) {
let openIssues = month.openedIssues = (month.openedIssues || {});

if (issue.milestone) {
openIssues = openIssues[issue.milestone.title] = openIssues[issue.milestone.title] || [];
} else {
openIssues = openIssues[NO_MILESTONE] = openIssues[NO_MILESTONE] || [];
}

let labels = "";
if (issue.labels) {
issue.labels.forEach(label => {
labels += (labels ? ", " : "") + label.name;
});
}

if (!closedAt) {
openIssues.push(`#${issue.number} ${labels ? "[" + labels + "]" : ""} :${issue.title}`);
} else {
openIssues.push(`#${issue.number} ${labels ? "[" + labels + "]" : ""} =<(Closed)>=- :${issue.title}`);
}
}

if (closedAt) {
let closeYear = closedAt.getFullYear();
let closeMonth = String(closedAt.getMonth() + 1).padStart(2, "0");
Expand All @@ -287,6 +314,34 @@
let month = year[closeMonth] = year[closeMonth] || { cnt: 0, opened: 0, closed: 0 };
month.closed++;
month.cnt--;

if (issue) {
let closedIssues = month.closedIssues = (month.closedIssues || {});
if (issue.milestone) {
closedIssues = closedIssues[issue.milestone.title] = closedIssues[issue.milestone.title] || [];
} else {
closedIssues = closedIssues[NO_MILESTONE] = closedIssues[NO_MILESTONE] || [];
}

closedIssues.push(`#${issue.number} - ${issue.title}`);
}
}
}

function writeFile(filename, data, extension, overwrite = true, idx = 0) {
let newFilename = filename + (idx ? ("-" + idx) : "") + "." + extension;

if (!overwrite && fs.existsSync(newFilename)) {
console.log(" -- Existing " + newFilename);
writeFile(filename, data, extension, overwrite, idx + 1);
return;
}

try {
fs.writeFileSync(newFilename, data);
} catch (e) {
console.error(` -- Failed to write newFilename - ${e}`);
writeFile(filename, data, extension, overwrite, idx + 1);
}
}

Expand Down Expand Up @@ -325,7 +380,7 @@
}
}

sumValues(openIssues, createdAt, closedAt);
sumValues(openIssues, createdAt, closedAt, issue);
if (!_noLabels && issue.labels && issue.labels.length > 0) {
issue.labels.forEach(label => {
if (!_labels || _labels.includes(label.name)) {
Expand Down Expand Up @@ -360,6 +415,8 @@
_startMonth = (yr * 100) + mon;
}

let filename = `issues-${_startMonth + 1}-${lastMonth + 1}`;

logMessage(`Reporting from: ${_startMonth + 1} to ${lastMonth + 1}`);
logHeader("Issues", firstYear, lastYear, lastMonth, openIssues);
dumpCount("New", openIssues, firstYear, lastYear, lastMonth, "opened");
Expand All @@ -371,7 +428,16 @@
});
//console.log(JSON.stringify(labels, null, 4));

return lastMonth;

if (_dump) {
console.log("Dumping raw JSON data to: " + filename + "-dump");
writeFile(filename + "-dump", JSON.stringify(openIssues, null, 4), "json", true);

console.log("Dumping raw JSON data to: " + filename + "-issues");
writeFile(filename + "-issues", JSON.stringify(issues, null, 4), "json", true);
}

return filename;
}

if (parseArgs()) {
Expand All @@ -382,15 +448,11 @@
console.log(`Running: \"${npmCmd}\"`);
try {
let output = child_process.execSync(npmCmd);
let lastMonth = processIssues(JSON.parse(output));
let filename = processIssues(JSON.parse(output));

if (_csv) {
fs.writeFileSync(`issues-${_startMonth + 1}-${lastMonth + 1}.csv`, _csvOutput, (err, data) => {
if (err) {
console.error(err);
throw `Failed to write ${dtsFileRollup}`;
}
});
console.log("Writing CSV data to: " + filename);
writeFile(filename, _csvOutput, "csv", true);
}
} catch (e) {
console.error("This command requires the Github CLI to be installed and configured.");
Expand Down
Loading