Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XS✔ ◾ Improve-stale-pr-check #21

Merged
merged 2 commits into from Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/dependabot.yml
@@ -0,0 +1,23 @@
version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
commit-message:
prefix: "⬆️ github-actions"

- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
commit-message:
prefix: "⬆️ npm"

- package-ecosystem: "nuget"
directory: "/"
schedule:
interval: "weekly"
commit-message:
prefix: "⬆️ nuget"
169 changes: 25 additions & 144 deletions .github/workflows/pr-manage-stale.yml
Expand Up @@ -4,157 +4,38 @@
name: "PR - Manage Stale PRs"

on:
pull_request:
types: [opened, ready_for_review, closed]
branches: [main]

# schedule:
# # run every 2nd hour
# # https://crontab.guru/#0_0/2_*_*_*
# - cron: "0 0/2 * * *"

schedule:
# https://crontab.guru/#0_20_*_*_0 - At 20:00 on Sunday (UTC)
- cron: '0 20 * * 0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not run this at least once a day?
Instead of once a week?

workflow_dispatch:

permissions:
contents: read
issues: write
repository-projects: read
pull-requests: write

jobs:
new_pull_request:
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: add label
shell: pwsh
run: |
$thisPr = gh pr view ${{ github.event.pull_request.number }} --json number,isCrossRepository | ConvertFrom-Json
if ($thisPr.isCrossRepository) {
Write-Output "Skipping cross repository PR"
return
}
review-reminder:
permissions:
contents: read
pull-requests: write

# ensure labels exist - create them if they don't
# https://docs.github.com/en/rest/reference/issues#create-a-label
$label = "Age: 🥚 - New"
$description = "About 2 hours old"
Write-Output "Ensuring label exists: $label ($description))"
gh api repos/${{ github.repository }}/labels -f name="$label" -f color=000000 -f description="$description"

Write-Output "Adding label: $label"
gh pr edit $thisPr.number --add-label "$label"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}


scheduled_run:
if: ${{ github.event_name != 'pull_request' }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Find PRs
shell: pwsh
run: |
# labels for each age in hours (as an array)
$labels = @(
'Age: 🥚 - New', # 2 hours
'Age: 🐣 - Young', # 4 hours
'Age: 🐥 - Adolescent', # 8 hours
'Age: 🐤 - Mature', # 16 hours
'Age: 🐓 - Old', # 32 hours
'Age: 🍗 - Ancient', # 64 hours
'Age: 🦖 - Extinct' # 128 hours
)

# ensure labels exist - create them if they don't
# https://docs.github.com/en/rest/reference/issues#create-a-label
$index = 0
$labels | ForEach-Object {
$label = $_
$description = "About $([math]::Pow(2, $index + 1)) hours old"
Write-Output "Ensuring label exists: $label ($description))"
gh api repos/${{ github.repository }}/labels -f name="$label" -f color=000000 -f description="$description"
$index++
}
$mergeDebtLabelText = "Merge Debt"
$mergeDebtLabel = "🔥 $mergeDebtLabelText"
$description = "This PR contains merge debt, see https://www.ssw.com.au/rules/merge-debt/"
Write-Output "Ensuring label exists: $mergeDebtLabel ($description))"
gh api repos/${{ github.repository }}/labels -f name="$mergeDebtLabel" -f color=000000 -f description="$description"

$now = Get-Date -AsUTC

# use github cli to get a list of open pull requests
$openPullRequests = gh pr list -s open --json title,number,createdAt,labels,author

Write-Output "Open pull requests:"

# for each pull request in the list calculate the age in hours
$openPullRequests | ConvertFrom-Json | ForEach-Object {
$title = $_.title
$number = $_.number
$createdAt = $_.createdAt
$prLabels = $_.labels
$author = $_.author
$ageInHours = [int]($now - $createdAt).TotalHours

if ($ageInHours -eq 0) {
Write-Output "$($number): $title ($ageInHours hours old) - skipping"
return
}

Write-Output "$($number): $title ($ageInHours hours old)"

# find age as nearest doubling number of 2
$ageAsPoints = [math]::Ceiling([math]::Log($ageInHours, 2))
Write-Output "ageAsPoints: $([math]::Pow(2,$ageAsPoints))"

# if the age is 0, then set it to 1
if ($ageAsPoints -eq 0) {
$ageAsPoints = 1
}

# if the age is greater than the length of the labels array
# then set it to the last label
if ($ageAsPoints -gt $labels.Length) {
$ageAsPoints = $labels.Length
}

$newLabel = $labels[$ageAsPoints - 1]
Write-Output "Adding label: $newLabel"

# all labels except the new label
$allLabels = ($labels | Where-Object { $_ -ne $newLabel }) -join ','
Write-Output "Removing labels: $allLabels"
gh pr edit $number --add-label "$newLabel" --remove-label "$allLabels"

$mergeLabelExists = $prLabels | Where-Object { $_.name.trim().EndsWith($mergeDebtLabelText) }
if ($ageInHours -ge 36 -and $mergeLabelExists -eq $null -and $author.is_bot -eq $false) { # after 36 hours old
Write-Output "Adding comment to PR: $number"
gh pr edit $number --add-label "$mergeDebtLabel"
$comment = @"
Howzit @$($author.login),

This PR has been here a while.

[Did you know you should avoid merge debt?](https://www.ssw.com.au/rules/merge-debt/)

Please review and merge or close.

Thanks!
"@
Set-Content -Path "comment.md" -Value $comment
gh pr comment $number --body-file comment.md
}

Write-Output ""
}

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/stale@v9
with:
days-before-issue-stale: -1 # don't mark issues as stale
days-before-pr-stale: 3
days-before-close: -1 # don't close issues/PRs
exempt-draft-pr: true
ignore-updates: true
stale-pr-message: |
Hi there!

This PR has been here a while.

[Did you know you should avoid merge debt?] (https://www.ssw.com.au/rules/merge-debt/)

Please review and merge or close.

Thanks!