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

Refactor add-label.js to use label-directory.json #7538

Open
wants to merge 3 commits into
base: gh-pages
Choose a base branch
from
Open
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
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,14 @@ 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 labelIds to retrieve current labelNames from directory
const statusUpdated = retrieveLabelDirectory('statusUpdated');
const statusInactive1 = retrieveLabelDirectory('statusInactive1');
const statusInactive2 = retrieveLabelDirectory('statusInactive2');

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 +54,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 Down Expand Up @@ -182,8 +180,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 +198,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 +229,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 +256,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 +362,4 @@ async function minimizeComments(comment_node_ids) {
}
}

module.exports = main;
module.exports = main;