Skip to content

Commit

Permalink
Merge pull request #1385 from hackforla/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
sydneywalcoff authored May 6, 2024
2 parents 3c779a3 + 60fb25f commit 8e9aa58
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
101 changes: 101 additions & 0 deletions .github/workflows/scripts/weekly-label-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const { Octokit } = require("@octokit/rest");

const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN });

const REQUIRED_LABELS = ["priority", "role", "feature", "size", "issue level"];
const LABEL_MISSING = REQUIRED_LABELS.map((label) => {
return `${label}: missing`;
});

async function fetchOpenIssues() {
let allIssues = [];
let hasNextPage = true;
let currentPage = 1;

while (hasNextPage) {
const { data: issues } = await octokit.issues.listForRepo({
owner: "hackforla",
repo: "expunge-assist",
state: "open",
page: currentPage,
per_page: 100,
});

allIssues = allIssues.concat(issues);

hasNextPage = issues.length === 100;
currentPage++;
}

const allOpenIssues = allIssues.filter(
(issue) => issue.pull_request === undefined
);
console.log("Fetched all open issues (excluding PRs):", allOpenIssues.length);
return allOpenIssues;
}

async function main() {
const openIssues = await fetchOpenIssues();

for (const issue of openIssues) {
const labels = issue.labels.map((label) => label.name);
const filteredLabels = filterLabels(labels);
const labelsToAdd = checkLabels(filteredLabels);

if (labelsToAdd.length === 0) {
console.log(
`All required labels are included for issue #${issue.number}; no labels to add.`
);
} else {
console.log(`Labels to add for issue #${issue.number}: `, labelsToAdd);
await addLabels(labelsToAdd, issue.number);
}
}
}

function filterLabels(labels) {
return labels.filter((label) => LABEL_MISSING.includes(label) === false);
}

function checkLabels(labels) {
let labelsToAdd = [];

REQUIRED_LABELS.forEach((requiredLabel, i) => {
const regExp = new RegExp(`${requiredLabel}`, "gi");
const isLabelPresent = labels.some((label) => {
return regExp.test(label);
});

if (isLabelPresent === false) {
labelsToAdd.push(LABEL_MISSING[i]);
}
});
return labelsToAdd;
}

async function addLabels(
labelsToAdd,
issueNum,
owner = "hackforla",
repo = "expunge-assist"
) {
const labels = [...new Set([...labelsToAdd])];

try {
await octokit.issues.addLabels({
owner: owner,
repo: repo,
issue_number: issueNum,
labels: labels,
});
if (labelsToAdd.length > 0) {
console.log("Successfully added labels to issue #", issueNum);
}
return true;
} catch (err) {
console.error("Error editing labels: ", err);
return false;
}
}

main().catch(console.error);
30 changes: 30 additions & 0 deletions .github/workflows/weekly-label-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Weekly Label Check

on:
schedule:
# Runs every Sunday at 00:00 UTC
- cron: "0 0 * * 0"

jobs:
Add-Missing-Labels-To-Issues:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install @octokit/rest

- name: Process Open Issues
run: node ./.github/workflows/scripts/weekly-label-check.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit 8e9aa58

Please sign in to comment.