Skip to content

PR: Find branches without PR's #506

PR: Find branches without PR's

PR: Find branches without PR's #506

name: "PR: Find branches without PR's"
on:
push:
branches:
- "main"
paths:
- ".github/workflows/create-prs-for-stale-branches.yml"
schedule:
- cron: "0 1 * * *"
concurrency:
group: ${{github.workflow}}-${{github.ref}}
cancel-in-progress: false
jobs:
build-matrix:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- run: sudo chown -R "$USER:$USER" "$GITHUB_WORKSPACE"
- run: |
echo "Repo: ${{github.repository}}"
echo "Owner: ${{github.repository_owner}}"
- run: sudo apt install -y jq
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main
repository: ${{github.repository}}
- id: set-matrix
run: |
echo "branches=$(git branch --remote --format='%(refname:short)' | grep -Po 'origin/\K[^*]*' | grep -vE 'HEAD|release/|hotfix/' | jq -R -s -c 'split("\n")[:-1]')" >> "$GITHUB_OUTPUT"
outputs:
branches: ${{steps.set-matrix.outputs.branches}}
check-branch:
needs: build-matrix
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
strategy:
matrix:
branch: ${{fromJson(needs.build-matrix.outputs.branches)}}
env:
BRANCH: ${{matrix.branch}}
CREATE_DRAFT: true
steps:
- run: sudo chown -R "$USER:$USER" "$GITHUB_WORKSPACE"
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main
repository: ${{github.repository}}
- if: ${{github.repository_owner != 'funfair-tech'}}
name: "Check Repo Visibility"
uses: credfeto/[email protected]
id: visibility
with:
repository: ${{github.repository}}
token: ${{secrets.GITHUB_TOKEN}}
- if: ${{github.repository_owner != 'funfair-tech' && env.REPO_STATUS == 'private'}}
run: echo "CREATE_DRAFT=false" >> "$GITHUB_ENV"
- id: detection
run: |
echo "defaultbranch=$(git remote show origin | grep -Po 'HEAD\sbranch:\s+\K[^.*]*')" >> "$GITHUB_OUTPUT"
echo "DEFAULT_BRANCH=$(git remote show origin | grep -Po 'HEAD\sbranch:\s+\K[^.*]*')" >> "$GITHUB_ENV"
# On Default branch
- if: ${{steps.detection.outputs.defaultbranch == matrix.branch}}
run: echo "On Default branch - don't do anything"
# On Non-Default branch
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch}}
run: echo "On non-default branch"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch}}
uses: juliangruber/[email protected]
id: find-pull-request
with:
branch: "${{env.BRANCH}}"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch}}
run: echo "Pull Request ${{steps.find-pull-request.outputs.number}} (${{steps.find-pull-request.outputs.head-sha}})"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''}}
run:
echo "Check to see if ${{env.DEFAULT_BRANCH}} is ahead or behind ${{matrix.branch}}..."
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{matrix.branch}}"
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{matrix.branch}}" | awk '{ print "${{env.BRANCH}} is behind ${{env.DEFAULT_BRANCH}} by "$1" commit(s)" }'
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{matrix.branch}}" | awk '{ print "${{matrix.BRANCH}} is ahead of ${{env.DEFAULT_BRANCH}} by "$2" commit(s)" }'
env:
DEFAULT_BRANCH: ${{steps.detection.outputs.defaultbranch}}
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''}}
id: lastupdate
run: echo "time=$(git log -1 --format=@%ct origin/${{env.BRANCH}})" >> "$GITHUB_OUTPUT"
- if: ${{steps.detection.outputs.defaultbranch != matrix.branch && steps.find-pull-request.outputs.number == ''}}
id: aheadbehind
name: Check Ahead\Behind Status
run: >
{ \
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{env.BRANCH}}" \
| awk '{ print "behind="$1 }' ; \
git rev-list --left-right --count "origin/${{env.DEFAULT_BRANCH}}...origin/${{env.BRANCH}}" \
| awk '{ print "ahead="$2 }' ; \
echo "dayssinceupdate=$((($(date +%s)-$(date +%s --date ${{env.LAST_UPDATE}}))/(3600*24)))" ; \
echo "COMMIT_MSG=$(git log -1 --pretty=%B "${{env.ORIGIN_DEFAULT_BRANCH}}")" ; \
} >> "$GITHUB_OUTPUT"
env:
DEFAULT_BRANCH: ${{steps.detection.outputs.defaultbranch}}
ORIGIN_DEFAULT_BRANCH: origin/${{steps.detection.outputs.defaultbranch}}
LAST_UPDATE: ${{steps.lastupdate.outputs.time}}
# Non-default - ahead of default branch, and no -re-existing PR then create one.
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7}}
id: pr-template
uses: juliangruber/[email protected]
with:
path: ./.github/PULL_REQUEST_TEMPLATE.md
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7}}
id: open-pr
uses: repo-sync/[email protected]
with:
source_branch: ${{matrix.branch}} # If blank, default: triggered branch
destination_branch: ${{steps.detection.outputs.defaultbranch}} # If blank, default: master
pr_assignee: "${{github.actor}}" # Comma-separated list (no spaces)
pr_label: "auto-pr,stale" # Comma-separated list (no spaces)
pr_draft: ${{env.CREATE_DRAFT}} # Creates pull request as draft
pr_title: "${{steps.aheadbehind.outputs.COMMIT_MSG}} [No commits for ${{steps.aheadbehind.outputs.dayssinceupdate}} days]"
pr_body: "${{steps.pr-template.outputs.content}}"
github_token: ${{github.token}}
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7}}
name: New PR Details
run: |
echo "URL: ${{steps.open-pr.outputs.pr_url}}"
echo "PR: ${{steps.open-pr.outputs.pr_number}}"
echo "CF: ${{steps.open-pr.outputs.has_changed_files}}"
- if: ${{steps.open-pr.outputs.pr_number != ''}}
uses: actions/[email protected]
with:
repo-token: ${{secrets.SOURCE_PUSH_TOKEN}}
configuration-path: .github/labeler.yml
sync-labels: true
pr-number: ${{steps.open-pr.outputs.pr_number}}
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.behind != 0
&& steps.aheadbehind.outputs.ahead != 0
&& steps.aheadbehind.outputs.dayssinceupdate > 7}}
name: Rebase
run: echo "Rebase?"
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''
&& steps.aheadbehind.outputs.ahead == '0'
&& steps.aheadbehind.outputs.dayssinceupdate > 60}}
name: Delete Branch
uses: dawidd6/[email protected]
with:
github_token: ${{secrets.SOURCE_PUSH_TOKEN}}
branches: ${{matrix.branch}}
- if: |-
${{steps.detection.outputs.defaultbranch != matrix.branch
&& steps.find-pull-request.outputs.number == ''}}
run: |
echo "Ahead: ${{steps.aheadbehind.outputs.ahead}}"
echo "Behind: ${{steps.aheadbehind.outputs.behind}}"
echo "Last Commit: ${{steps.aheadbehind.outputs.dayssinceupdate}} days ago"