hackforla / website

Hack for LA's website
https://www.hackforla.org
GNU General Public License v2.0
292 stars 707 forks source link

ER: Code enhancement for `add-label.js` #6840

Open joey-ma opened 3 weeks ago

joey-ma commented 3 weeks ago

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:

Issue you discovered this emergent requirement in

Date 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

/**
 * 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

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