generated from hackforla/.github-hackforla-base-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1385 from hackforla/dev
Dev
- Loading branch information
Showing
2 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |