Skip to content

Label PRs that are behind upstream #3348

Label PRs that are behind upstream

Label PRs that are behind upstream #3348

name: Label PRs that are behind upstream
on:
schedule:
- cron: "0 * * * *" # every hour
push:
branches:
- data
pull_request:
branches:
- data
types:
- opened
- synchronize
- reopened
workflow_dispatch:
jobs:
label-behind-prs:
if: ${{ github.event.repository.fork == false }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: List all open PR numbers
id: list
run: |
prs=$(gh pr list --state open --json number,headRefName,headRepositoryOwner,headRepository --jq '.[] | @base64' | tr "\n" " "|sed "s/ *$//")
echo "prs=$prs" >> $GITHUB_OUTPUT
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Process each PR
run: |
echo "${{ steps.list.outputs.prs }}" \
| tr " " "\n" \
| while read pr_encoded; do
pr=$(echo "$pr_encoded" | base64 --decode)
pr_number=$(echo "$pr" | jq -r '.number')
pr_branch=$(echo "$pr" | jq -r '.headRefName')
pr_owner=$(echo "$pr" | jq -r '.headRepositoryOwner.login')
pr_repo=$(echo "$pr" | jq -r '.headRepository.name')
# Skip PRs from the same repository
if [ "$pr_owner" == "${GITHUB_REPOSITORY_OWNER}" ]; then
echo "::notice:: Skipping PR #$pr_number from the same repository"
continue
fi
# Skip PRs if the trigger is other pull_request
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "::notice:: Triggered by PR #$pr_number"
if [ ${{ github.event.pull_request.number }} -ne $pr_number ]; then
continue
fi
fi
echo "::notice::Processing PR #$pr_number from $pr_owner/$pr_repo ($pr_branch)"
# Add the fork as a remote
remote_name="fork-$pr_number"
git remote add $remote_name https://github.com/$pr_owner/$pr_repo.git 2>/dev/null || true
git fetch $remote_name $pr_branch
# Fetch upstream branch
git fetch origin data
# Check if PR branch is behind upstream
behind=$(git rev-list --count $remote_name/$pr_branch..origin/data)
if [ "$behind" -gt 0 ]; then
echo "::notice:: PR #$pr_number is behind by $behind commits. Adding label..."
gh pr edit $pr_number --add-label "upstream-updated"
else
echo "::notice:: PR #$pr_number is up-to-date. Removing label if exists..."
gh pr edit $pr_number --remove-label "upstream-updated" || true
fi
done
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}