Skip to content

Auto Close Issues Based on Priority Levels #60

Auto Close Issues Based on Priority Levels

Auto Close Issues Based on Priority Levels #60

Workflow file for this run

name: Auto Close Issues Based on Priority Levels
on:
schedule:
# Run the job every day at midnight
- cron: '0 0 * * *'
permissions:
issues: write
contents: read
jobs:
close_issues:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v3
- name: Close Issues Based on Labels
uses: actions/github-script@v6
with:
script: |
const now = new Date();
const issues = await github.paginate(github.rest.issues.listForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
});
for (const issue of issues) {
const daysSinceOpened = (now - new Date(issue.created_at)) / (1000 * 60 * 60 * 24);
// Log the issue and the number of days it has been open
console.log(`Checking issue #${issue.number}, opened ${daysSinceOpened.toFixed(1)} days ago.`);
console.log(`Issue #${issue.number} has labels: ${issue.labels.map(label => label.name).join(", ")}`);
if (issue.labels.some(label => label.name === "level 1")) {
if (daysSinceOpened >= 1) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: "closed"
});
console.log(`Closed level 1 issue #${issue.number}`);
}
} else if (issue.labels.some(label => label.name === "level 2")) {
if (daysSinceOpened >= 2) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: "closed"
});
console.log(`Closed level 2 issue #${issue.number}`);
}
} else if (issue.labels.some(label => label.name === "level 3")) {
if (daysSinceOpened >= 4) {
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: "closed"
});
console.log(`Closed level 3 issue #${issue.number}`);
}
}
}