-
Notifications
You must be signed in to change notification settings - Fork 0
66 lines (55 loc) · 2.33 KB
/
Copy pathstale-pr-notifier.yml
File metadata and controls
66 lines (55 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
name: Stale PR Notifier
on:
push:
branches:
- main
permissions:
pull-requests: write
jobs:
notify:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches and tags
- name: Get recent commits from main
id: get_commits
run: |
COMMITS=$(git log origin/main --pretty=format:'- %h %s' -n 5)
echo "commits<<EOF" >> $GITHUB_OUTPUT
echo "$COMMITS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Check open PRs and notify if stale
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMITS: ${{ steps.get_commits.outputs.commits }}
run: |
for pr_number in $(gh pr list --state open --json number -q '.[].number'); do
echo "Checking PR number $pr_number"
pr_branch=$(gh pr view $pr_number --json headRefName -q '.headRefName')
git fetch origin $pr_branch
# Check if main is an ancestor of the PR branch. If it is, the PR is up to date.
if git merge-base --is-ancestor origin/main "origin/$pr_branch"; then
echo "PR #$pr_number is up to date."
else
echo "PR #$pr_number is stale. Commenting."
# Check if a "stale" comment already exists to avoid spamming
existing_comment=$(gh pr view $pr_number --json comments -q '.comments[] | select(.body | contains("This PR is out-of-date")) | .id')
if [ -z "$existing_comment" ]; then
# Define the multiline body using a here document to ensure proper expansion and formatting
read -r -d '' BODY <<EOF
⚠️ **This PR is out-of-date with the `main` branch.**
The base branch has been updated with new changes.
**Recent commits to `main`:**
```
$COMMITS
```
To ensure a clean merge, please rebase your branch on top of the latest `main` and push your changes.
EOF
gh pr comment "$pr_number" --body "$BODY"
else
echo "A stale comment already exists on PR #$pr_number. Skipping."
fi
fi
done