From a0527cf2d7cceef09933d0ec39509f1772868819 Mon Sep 17 00:00:00 2001 From: "Gordon Beeming [SSW]" Date: Wed, 20 Sep 2023 10:47:52 +1000 Subject: [PATCH] Add a label for merge debt as well to manage when to comment (#14) --- .github/workflows/pr-manage-stale.yml | 49 ++++++++++++++++----------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/.github/workflows/pr-manage-stale.yml b/.github/workflows/pr-manage-stale.yml index 9dc1580..40cc312 100644 --- a/.github/workflows/pr-manage-stale.yml +++ b/.github/workflows/pr-manage-stale.yml @@ -71,7 +71,7 @@ jobs: '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 @@ -82,14 +82,19 @@ jobs: 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 @@ -98,54 +103,58 @@ jobs: $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" - Write-Output "" - if ($ageInHours -ge 36 -and $ageInHours -lt 38 -and $author.is_bot -eq $false) { # after 36 hours old + $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 + Set-Content -Path "comment.md" -Value $comment + gh pr comment $number --body-file comment.md } + + Write-Output "" } + env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file