Pull Request Cleaner #12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Pull Request Cleaner | |
| on: | |
| workflow_dispatch: # Allows manual triggering of the workflow | |
| inputs: | |
| org_name: | |
| description: >- | |
| Name of GitHub organization. | |
| required: true | |
| default: 'Netcracker' | |
| branch_name_prefix: | |
| description: >- | |
| Prefix of the branch name to be deleted. | |
| required: true | |
| default: 'bot/' | |
| branch_delete: | |
| description: >- | |
| Whether to delete the branch after closing the PR. | |
| If true, the branch will be deleted. | |
| required: false | |
| default: 'true' | |
| remove_orphant_branches: | |
| description: >- | |
| Whether to remove orphan branches. | |
| If true, the branches not related to PRs will be removed. | |
| required: false | |
| default: 'false' | |
| target_repo: | |
| description: >- | |
| Name of the target repository. | |
| If empty all organization repos will be updated. | |
| required: false | |
| comment: | |
| description: >- | |
| Comment to be added when closing the PR. | |
| required: true | |
| default: 'This PR is closed as part of the cleanup process.' | |
| jobs: | |
| pull_request_cleanup: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # Step 1: Checkout the repository | |
| - name: Checkout Repository | |
| uses: actions/checkout@v4 | |
| # Step 2 Setup Pull Request Cleaner | |
| - name: Setup Pull Request Cleaner | |
| run: | | |
| if [ -n "${{ github.event.inputs.target_repo }}" ]; then | |
| repos="${{ github.event.inputs.target_repo }}" | |
| else | |
| repos=$(gh repo list ${{ github.event.inputs.org_name }} --visibility public --limit 1000 --json name --jq '.[].name') | |
| fi | |
| for repo in $repos; do | |
| echo "Processing repository: $repo" | |
| # Get open pull requests with branch names containing the specified prefix | |
| prs=$(gh pr list --repo "${{ github.event.inputs.org_name }}/$repo" --state open --json number,headRefName --jq '.[] | select(.headRefName | contains("${{ github.event.inputs.branch_name_prefix }}")) | .number') | |
| for pr in $prs; do | |
| echo "Closing PR #$pr in $repo" | |
| # Close the pull request with a comment | |
| if [ "${{ github.event.inputs.branch_delete }}" = "true" ]; then | |
| gh pr close "$pr" --repo "${{ github.event.inputs.org_name }}/$repo" --delete-branch --comment "${{ github.event.inputs.comment }}" | |
| else | |
| gh pr close "$pr" --repo "${{ github.event.inputs.org_name }}/$repo" --comment "${{ github.event.inputs.comment }}" | |
| fi | |
| done | |
| # Remove orphan branches if specified | |
| if [ "${{ github.event.inputs.remove_orphant_branches }}" = "true" ] && [ -n "${{ github.event.inputs.branch_name_prefix }}" ]; then | |
| echo "Removing orphan branches in $repo with prefix '${{ github.event.inputs.branch_name_prefix }}'" | |
| orphan_branches=$(gh api repos/${{ github.event.inputs.org_name }}/$repo/branches --jq '.[] | select(.name | startswith("${{ github.event.inputs.branch_name_prefix }}")) | .name') | |
| for branch in $orphan_branches; do | |
| echo "Deleting orphan branch: $branch" | |
| gh api -X DELETE repos/${{ github.event.inputs.org_name }}/$repo/git/refs/heads/$branch | |
| sleep 3s | |
| done | |
| fi | |
| done | |
| echo "Pull request cleanup completed." | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }} |