[ ] purpose 1: to improve readability (ignore console log on line:47)
[ ] purpose 2: avoid getting following error (login exists for when response event: 'assigned', but not when event: 'committed', done in 2 commits, here's the other commit. Feel free to do a diff on this file with the existing file)
TypeError: Cannot read properties of undefined (reading 'login')
| at isTimelineOutdated (/Users/joeyma/Documents/GitHub/hfla/ops/github-actions/trigger-schedule/add-update-label-weekly/add-label.js:125:67)
| at main (/Users/joeyma/Documents/GitHub/hfla/ops/github-actions/trigger-schedule/add-update-label-weekly/add-label.js:46:34)
| at processTicksAndRejections (node:internal/process/task_queues:96:5)
[ ] purpose: to enhance logging and error handling, specifically to avoid getting the following error. This is when the request is attempting to delete a label that does not exist on the issue (responseObject). The 404 is then rather the expected behavior, so I opted to return a response object instead of the entire error message Function failed to remove labels. Please refer to the error below: \n , err, which otherwise alerted me that something is wrong and should be investigated.
/**
* Function that returns the issue's timeline of events.
* https://octokit.github.io/rest.js/v20#issues-list-events-for-timeline
* https://docs.github.com/en/rest/issues/events?apiVersion=2022-11-28#list-issue-events
* @param {number} issue_number
* @returns {Array} of Objects containing the issue's timeline of events
*/
async function getTimeline(issue_number, github, context) {
const { owner, repo } = context.repo;
let timelineArr = [];
let page = 1, retries = 0;
const per_page = 100, maxRetries = 3;
while (true) {
try {
const { data } = await github.rest.issues.listEventsForTimeline({
owner,
repo,
issue_number,
per_page,
page,
});
if (data.length) {
timelineArr = timelineArr.concat(data);
page++;
} else {
break;
}
} catch (err) {
if (err instanceof TypeError) throw new Error(err);
if (retries < maxRetries) {
const delay = Math.pow(2, retries);
console.log(`Retrying in ${delay} seconds...`);
await new Promise(resolve => setTimeout(resolve, delay * 1000));
retries++;
} else {
console.error(err);
throw new Error(`Failed to fetch timeline for issue #${issue_number}`);
}
}
}
return timelineArr;
}
module.exports = getTimeline;
github-actions/utils/find-linked-issue.js
/**
* Function that returns a linked issue.
* @param text the text to match keywords
* @returns
*/
function findLinkedIssue(text) {
// Create RegEx for capturing KEYWORD #ISSUE-NUMBER syntax (i.e. resolves #1234)
const KEYWORDS = ['close', 'closes', 'closed', 'fix', 'fixes', 'fixed', 'resolve', 'resolves', 'resolved']
let reArr = [] // an array used to store multiple RegExp patterns
for (const word of KEYWORDS) {
reArr.push(`[\\n|\\s|^]${word} #\\d+\\s|^${word} #\\d+\\s|\\s${word} #\\d+$|^${word} #\\d+$`)
}
// Receive and unpack matches into an Array of Array objs
let re = new RegExp(reArr.join('|'), 'gi')
let matches = text.matchAll(re)
matches = [...matches]
// If only one match is found, return the issue number & console.log results.
if (matches.length == 1) {
const issueNumber = matches[0][0].match(/\d+/)
return issueNumber[0]
} else {
return null
}
}
module.exports = findLinkedIssue
Emergent Requirement - Problem
Since these are mostly enhancement changes, and my commits were not the cleanest so I'm including my reasonings below ππ»ππ»ππ»
for
github-actions/utils/get-timeline.js
:for
github-actions/trigger-schedule/add-update-label-weekly/add-label.js
:login
exists for when responseevent: 'assigned'
, but not whenevent: 'committed'
, done in 2 commits, here's the other commit. Feel free to do a diff on this file with the existing file)404
is then rather the expected behavior, so I opted to return a response object instead of the entire error messageFunction failed to remove labels. Please refer to the error below: \n , err
, which otherwise alerted me that something is wrong and should be investigated.Issue you discovered this emergent requirement in
hackforla/ops
issue #58Date discovered
03/12/2024
Did you have to do something temporarily
Who was involved
@joey-ma
What happens if this is not addressed
N/A
Resources
Recommended Action Items
Potential solutions [draft]
github-actions/utils/get-timeline.js
github-actions/utils/find-linked-issue.js
github-actions/trigger-schedule/add-update-label-weekly/add-label.js
at https://github.com/hackforla/ops/blob/2b4ac464755bc16f75aff61f813c620f0d80babb/github-actions/trigger-schedule/add-update-label-weekly/add-label.js