Close Inactive Issues #39
Workflow file for this run
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
name: Close Inactive Issues | |
on: | |
schedule: | |
- cron: '0 0 * * *' # Run daily | |
workflow_dispatch: # This line enables manual triggering | |
jobs: | |
close-issues: | |
runs-on: ubuntu-latest | |
permissions: | |
issues: write # Ensure necessary permissions for issues | |
steps: | |
- name: Close inactive issues | |
uses: actions/github-script@v7 | |
with: | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
script: | | |
const octokit = github; | |
// Get the repository owner and name | |
const { owner, repo } = context.repo; | |
// Define the inactivity period (14 days) | |
const inactivityPeriod = new Date(); | |
inactivityPeriod.setDate(inactivityPeriod.getDate() - 14); | |
const labelKeepIssue = 'Keep Issue Open'; | |
try { | |
// Get all open issues with pagination | |
for await (const response of octokit.paginate.iterator(octokit.rest.issues.listForRepo, { | |
owner, | |
repo, | |
state: 'open', | |
})) { | |
const issues = response.data; | |
// Close issues inactive for more than the inactivity period | |
for (const issue of issues) { | |
const respondIssueLabels = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}/labels", { | |
owner: owner, | |
repo: repo, | |
issue_number: issue.number | |
}); | |
const closeIssue = true; | |
// Get all Labels of issue, and compared each label with the labelKeepIssue const variable | |
const labels = respondIssueLabels.data; | |
console.log(`${JSON.stringify(labels, null, 4)}`); | |
for (let i = 0; i < labels.length; i++) { | |
//console.log(`${JSON.stringify(labels[i], null, 4)}`); | |
const label = labels[i] | |
if (label.name === labelKeepIssue) { | |
console.log(`Issue #${issue.number} will not be closed`); | |
closeIssue = false; | |
} | |
} | |
if (!closeIssue) { | |
continue; | |
} | |
console.log('Hello'); | |
const lastCommentDate = issue.updated_at; | |
if (new Date(lastCommentDate) < inactivityPeriod) { | |
try { | |
// Close the issue | |
await octokit.rest.issues.update({ | |
owner, | |
repo, | |
issue_number: issue.number, | |
state: 'closed', | |
}); | |
// Add a comment | |
await octokit.rest.issues.createComment({ | |
owner, | |
repo, | |
issue_number: issue.number, | |
body: 'Closed due to inactivity', | |
}); | |
} catch (error) { | |
console.error(`Error updating or commenting on issue #${issue.number}: ${error}`); | |
} | |
} | |
} | |
} | |
} catch (error) { | |
console.error(`Error fetching issues: ${error}`); | |
} |