Skip to content

Commit cb6e9bd

Browse files
committed
- adds a workflow to handle stale issues
Signed-off-by: Vincent Biret <[email protected]>
1 parent 7817060 commit cb6e9bd

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

.github/workflows/inactive-issues.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
on:
2+
issues:
3+
types: labeled
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '*/5 * * * *'
7+
8+
permissions:
9+
issues: write
10+
11+
name: Label and close issues with no recent activity
12+
13+
env:
14+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
15+
NEEDS_ATTENTION_LABEL: "Needs attention"
16+
NEEDS_AUTHOR_FEEDBACK_LABEL: "Needs author feedback"
17+
NO_RECENT_ACTIVITY_LABEL: "No recent activity"
18+
NO_RECENT_ACTIVITY_DURATION: "00:00:01"
19+
NO_RECENT_ACTIVITY_DURATION_CLOSE: "00:00:01"
20+
ORG_NAME: ${{ github.repository_owner }}
21+
REPO_NAME: ${{ github.event.repository.name }}
22+
NO_RECENT_ACTIVITY_COMMENT: "This issue has been labeled because there has been no recent activity. It will be closed if no further activity occurs within 1 day."
23+
24+
25+
jobs:
26+
run:
27+
runs-on: ubuntu-latest
28+
name: Label issues with no recent activity
29+
steps:
30+
- uses: actions/checkout@v4
31+
- run: scripts/label-no-recent.ps1
32+
shell: pwsh
33+
- run: scripts/close-no-recent.ps1
34+
shell: pwsh

scripts/close-no-recent.ps1

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
$inactivityDelay = [timespan]::Parse($Env:NO_RECENT_ACTIVITY_DURATION_CLOSE)
2+
$oldIssues = gh issue list --label "$Env:NO_RECENT_ACTIVITY_LABEL" --state open --limit 100 --json number,author,createdAt | ConvertFrom-Json
3+
foreach($oldIssue in $oldIssues) {
4+
$lastComment = gh issue view $oldIssue.number --json comments | ConvertFrom-Json | Select-Object -ExpandProperty comments | Where-Object {$_.author.login -eq $oldIssue.author.login} | Select-Object -Last 1
5+
if($null -eq $lastComment) {
6+
$lastCommentDate = [datetime]::Parse($oldIssue.createdAt)
7+
8+
} else {
9+
$lastCommentDate = [datetime]::Parse($lastComment.createdAt)
10+
}
11+
$lastLabelEvent = gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "/repos/$($Env:ORG_NAME)/$($Env:REPO_NAME)/issues/$($oldIssue.number)/events?per_page=100" | ConvertFrom-Json | Where-Object {$_.event -eq "labeled" -and $_.label.name -eq "$Env:NO_RECENT_ACTIVITY_LABEL"} | Select-Object -Last 1
12+
$lastLabelEventDate = [datetime]::Parse($lastLabelEvent.created_at)
13+
if ($lastCommentDate -gt $lastLabelEventDate) {
14+
gh issue edit $oldIssue.number --remove-label "$Env:NO_RECENT_ACTIVITY_LABEL" --remove-label "$Env:NEEDS_AUTHOR_FEEDBACK_LABEL" --add-label "$Env:NEEDS_ATTENTION_LABEL"
15+
} elseif (($lastCommentDate - $lastLabelEventDate) -le $inactivityDelay) {
16+
gh issue close $oldIssue.number -r "not planned"
17+
}
18+
}

scripts/label-no-recent.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
$inactivityDelay = [timespan]::Parse($Env:NO_RECENT_ACTIVITY_DURATION)
2+
$oldIssues = gh issue list --label "$Env:NEEDS_AUTHOR_FEEDBACK_LABEL" --state open --limit 100 --json number,author,createdAt | ConvertFrom-Json
3+
foreach($oldIssue in $oldIssues) {
4+
$lastComment = gh issue view $oldIssue.number --json comments | ConvertFrom-Json | Select-Object -ExpandProperty comments | Where-Object {$_.author.login -eq $oldIssue.author.login} | Select-Object -Last 1
5+
if($null -eq $lastComment) {
6+
$lastCommentDate = [datetime]::Parse($oldIssue.createdAt)
7+
8+
} else {
9+
$lastCommentDate = [datetime]::Parse($lastComment.createdAt)
10+
}
11+
$lastLabelEvent = gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "/repos/$($Env:ORG_NAME)/$($Env:REPO_NAME)/issues/$($oldIssue.number)/events?per_page=100" | ConvertFrom-Json | Where-Object {$_.event -eq "labeled" -and $_.label.name -eq "$Env:NEEDS_AUTHOR_FEEDBACK_LABEL"} | Select-Object -Last 1
12+
$lastLabelEventDate = [datetime]::Parse($lastLabelEvent.created_at)
13+
if ($lastCommentDate -gt $lastLabelEventDate) {
14+
gh issue edit $oldIssue.number --remove-label "$Env:NO_RECENT_ACTIVITY_LABEL" --remove-label "$Env:NEEDS_AUTHOR_FEEDBACK_LABEL" --add-label "$Env:NEEDS_ATTENTION_LABEL"
15+
} elseif (($lastCommentDate - $lastLabelEventDate) -le $inactivityDelay) {
16+
gh issue edit $oldIssue.number --add-label "$Env:NO_RECENT_ACTIVITY_LABEL" --remove-label "$Env:NEEDS_ATTENTION_LABEL"
17+
gh issue comment $oldIssue.number -b "$Env:NO_RECENT_ACTIVITY_COMMENT"
18+
}
19+
}

0 commit comments

Comments
 (0)