Skip to content

Commit

Permalink
Refactor add-label.js to use label-directory.json (#7538)
Browse files Browse the repository at this point in the history
* add label key references

* remove excess console.logs, match module names

* full substitution of labelId values

* adding omitted labels, fixing call to label dir, adjusting module

* Update add-label.js

changed note to `labelKeys` from `labelIds`

* Update retrieve-label-directory.js

Refactored non-variatic funct, renamed  funct

* Update retrieve-label-directory.js

fix comments for singular use

* Update retrieve-label-directory.js

add comment first line, remove space
  • Loading branch information
t-will-gillis authored Oct 19, 2024
1 parent 1b94d5b commit f425c4c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// Import modules
const fs = require("fs");
const queryIssueInfo = require("../../utils/query-issue-info");
// Import modules
const fs = require('fs');
const retrieveLabelDirectory = require('../../utils/retrieve-label-directory');
const queryIssueInfo = require('../../utils/query-issue-info');
const findLinkedIssue = require('../../utils/find-linked-issue');
const getTimeline = require('../../utils/get-timeline');
const minimizeIssueComment = require('../../utils/hide-issue-comment');
Expand All @@ -9,13 +10,28 @@ const minimizeIssueComment = require('../../utils/hide-issue-comment');
var github;
var context;

const statusUpdatedLabel = 'Status: Updated';
const toUpdateLabel = 'To Update !';
const inactiveLabel = '2 weeks inactive';
// Use labelKeys to map current labelNames from label directory
const [
statusUpdated,
statusInactive1,
statusInactive2,
draft,
er,
epic,
dependency,
] = [
"statusUpdated",
"statusInactive1",
"statusInactive2",
"draft",
"er",
"epic",
"dependency"
].map(retrieveLabelDirectory);

const updatedByDays = 3; // If last update update 3 days, the issue is considered updated
const commentByDays = 7; // If last update between 7 and 14 days ago, issue outdated, needs 'To Update !'
const inactiveUpdatedByDays = 14; // If last update greater than 14 days ago, issue considered '2 weeks inactive'
const commentByDays = 7; // If last update between 7 to 14 days ago, issue is outdated and needs update
const inactiveUpdatedByDays = 14; // If last update greater than 14 days ago, the issue is considered inactive

const threeDayCutoffTime = new Date();
threeDayCutoffTime.setDate(threeDayCutoffTime.getDate() - updatedByDays);
Expand Down Expand Up @@ -52,25 +68,21 @@ async function main({ g, c }) {
continue;
}

// Add and remove labels as well as post comment if the issue's timeline indicates the issue is inactive, to be updated or up to date accordingly
// Add and remove labels as well as post comment if the issue's timeline indicates the issue is inactive, to be updated or up-to-date accordingly
const responseObject = await isTimelineOutdated(timeline, issueNum, assignees);

if (responseObject.result === true && responseObject.labels === toUpdateLabel) { // 7-day outdated, add 'To Update !' label
console.log(` Issue #${issueNum}: Going to ask for an update now`);
await removeLabels(issueNum, statusUpdatedLabel, inactiveLabel);
if (responseObject.result === true && responseObject.labels === statusInactive1) { // 7-day outdated: add to be updated label, remove others
await removeLabels(issueNum, statusUpdated, statusInactive2);
await addLabels(issueNum, responseObject.labels);
await postComment(issueNum, assignees, toUpdateLabel);
} else if (responseObject.result === true && responseObject.labels === inactiveLabel) { // 14-day outdated, add '2 Weeks Inactive' label
console.log(` Issue #${issueNum}: Going to ask for an update now`);
await removeLabels(issueNum, toUpdateLabel, statusUpdatedLabel);
await postComment(issueNum, assignees, statusInactive1);
} else if (responseObject.result === true && responseObject.labels === statusInactive2) { // 14-day outdated: add inactive label, remove others
await removeLabels(issueNum, statusInactive1, statusUpdated);
await addLabels(issueNum, responseObject.labels);
await postComment(issueNum, assignees, inactiveLabel);
} else if (responseObject.result === false && responseObject.labels === statusUpdatedLabel) { // Updated within 3 days, retain 'Status: Updated' label if there is one
console.log(` Issue #${issueNum}: Updated within 3 days, retain updated label`);
await removeLabels(issueNum, toUpdateLabel, inactiveLabel);
await postComment(issueNum, assignees, statusInactive2);
} else if (responseObject.result === false && responseObject.labels === statusUpdated) { // Updated within 3 days: retain up-to-date label if there is one
await removeLabels(issueNum, statusInactive1, statusInactive2);
} else if (responseObject.result === false && responseObject.labels === '') { // Updated between 3 and 7 days, or recently assigned, or fixed by a PR by assignee, remove all three update-related labels
console.log(` Issue #${issueNum}: No updates needed, will remove all labels`);
await removeLabels(issueNum, toUpdateLabel, inactiveLabel, statusUpdatedLabel);
await removeLabels(issueNum, statusInactive1, statusInactive2, statusUpdated);
}
}
}
Expand All @@ -84,7 +96,7 @@ async function main({ g, c }) {
* @returns {Promise<Array>} issueNums - an array of open, assigned, and statused issue numbers
*/
async function getIssueNumsFromRepo() {
const labelsToExclude = ['Draft', 'ER', 'Epic', 'Dependency'];
const labelsToExclude = [draft, er, epic, dependency];
let issueNums = [];
let pageNum = 1;
let result = [];
Expand Down Expand Up @@ -182,8 +194,8 @@ function isTimelineOutdated(timeline, issueNum, assignees) { // assignees is an
minimizeComments(commentsToBeMinimized);

if (lastCommentTimestamp && isMomentRecent(lastCommentTimestamp, threeDayCutoffTime)) { // if commented by assignee within 3 days
console.log(`Issue #${issueNum}: Commented by assignee within 3 days, retain '${statusUpdatedLabel}' label`);
return { result: false, labels: statusUpdatedLabel } // retain (don't add) updated label, remove the other two
console.log(`Issue #${issueNum}: Commented by assignee within 3 days, retain '${statusUpdated}' label`);
return { result: false, labels: statusUpdated } // retain (don't add) updated label, remove the other two
}

if (lastAssignedTimestamp && isMomentRecent(lastAssignedTimestamp, threeDayCutoffTime)) { // if an assignee was assigned within 3 days
Expand All @@ -200,18 +212,18 @@ function isTimelineOutdated(timeline, issueNum, assignees) { // assignees is an
return { result: false, labels: '' } // remove all three labels
}

if ((lastCommentTimestamp && isMomentRecent(lastCommentTimestamp, fourteenDayCutoffTime)) || (lastAssignedTimestamp && isMomentRecent(lastAssignedTimestamp, fourteenDayCutoffTime))) { // if last comment was between 7-14 days, or no comment but an assginee was assigned during this period, issue is outdated and add 'To Update !' label
if ((lastCommentTimestamp && isMomentRecent(lastCommentTimestamp, fourteenDayCutoffTime)) || (lastAssignedTimestamp && isMomentRecent(lastAssignedTimestamp, fourteenDayCutoffTime))) { // if last comment was between 7-14 days, or no comment but an assginee was assigned during this period, issue is outdated and add needs update label
if ((lastCommentTimestamp && isMomentRecent(lastCommentTimestamp, fourteenDayCutoffTime))) {
console.log(`Issue #${issueNum}: Commented by assignee between 7 and 14 days, use '${toUpdateLabel}' label; timestamp: ${lastCommentTimestamp}`)
console.log(`Issue #${issueNum}: Commented by assignee between 7 and 14 days, use '${statusInactive1}' label; timestamp: ${lastCommentTimestamp}`)
} else if (lastAssignedTimestamp && isMomentRecent(lastAssignedTimestamp, fourteenDayCutoffTime)) {
console.log(`Issue #${issueNum}: Assigned between 7 and 14 days, use '${toUpdateLabel}' label; timestamp: ${lastAssignedTimestamp}`)
console.log(`Issue #${issueNum}: Assigned between 7 and 14 days, use '${statusInactive1}' label; timestamp: ${lastAssignedTimestamp}`)
}
return { result: true, labels: toUpdateLabel } // outdated, add 'To Update!' label
return { result: true, labels: statusInactive1 } // outdated, add needs update label
}

// If no comment or assigning found within 14 days, issue is outdated and add '2 weeks inactive' label
console.log(`Issue #${issueNum}: No update within 14 days, use '${inactiveLabel}' label`)
return { result: true, labels: inactiveLabel }
// If no comment or assigning found within 14 days, issue is outdated and add inactive label
console.log(`Issue #${issueNum}: No update within 14 days, use '${statusInactive2}' label`)
return { result: true, labels: statusInactive2 }
}


Expand All @@ -231,10 +243,10 @@ async function removeLabels(issueNum, ...labels) {
issue_number: issueNum,
name: label,
});
console.log(` '${label}' label has been removed`);
console.log(` '${label}' label has been removed`);
} catch (err) {
if (err.status === 404) {
console.log(` '${label}' label not found, no need to remove`);
console.log(` '${label}' label not found, no need to remove`);
} else {
console.error(`Function failed to remove labels. Please refer to the error below: \n `, err);
}
Expand All @@ -258,7 +270,7 @@ async function addLabels(issueNum, ...labels) {
issue_number: issueNum,
labels: labels,
});
console.log(` '${labels}' label has been added`);
console.log(` '${labels}' label has been added`);
// If an error is found, the rest of the script does not stop.
} catch (err) {
console.error(`Function failed to add labels. Please refer to the error below: \n `, err);
Expand Down Expand Up @@ -364,4 +376,4 @@ async function minimizeComments(comment_node_ids) {
}
}

module.exports = main;
module.exports = main;
39 changes: 21 additions & 18 deletions github-actions/utils/retrieve-label-directory.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
// Import modules
const fs = require('fs');

// Global variables
var filepath = 'github-actions/utils/_data/label-directory.json';
var labelData;

/*
* Matches label reference name(s) to the label display name(s) from JSON
* @param {string } filepath - Path to `label_directory.json`
* @param {Array} labelKeys - List of reference names to look up display names
* @return {Array} displayNames - List of display names
* Matches label reference name to the label display name from JSON
* @param {Array} labelKey - Key reference to look up display name
* @return {Array} labelName - Display name for each label
*/
function labelRetrieveNames(...labelKeys) {
function retrieveLabelName(labelKey) {

// Retrieve label directory
const rawData = fs.readFileSync(filepath, 'utf8');
const data = JSON.parse(rawData);
// Retrieve label directory if not read already
if (labelData === undefined) {
console.log(`Reading label directory...`);
const rawData = fs.readFileSync(filepath, 'utf8');
labelData = JSON.parse(rawData);
}

let labelName = '';

const displayNames = [ ];
for(let labelKey of labelKeys) {
try {
displayNames.push(data[labelKey][0]);
console.log(`Success! From label key: '${labelKey}' found label display: '${data[labelKey][0]}'`);
} catch (err) {
console.error(`Failed to find label display for label key: '${labelKey}'`)
}
try {
labelName = labelData[labelKey][0];
console.log(`Success! Found labelKey: '${labelKey}', returning labelName: '${labelName}'`);
} catch (err) {
console.error(`Failed to find labelKey: '${labelKey}'`);
}

return displayNames;
return labelName;
}

module.exports = labelRetrieveNames;
module.exports = retrieveLabelName;

0 comments on commit f425c4c

Please sign in to comment.