name: Pull Request

on:
  pull_request:
    branches:
      - main
      - 'release/**'

jobs:
  title-check:
    runs-on: ubuntu-latest
    steps:
      - uses: deepakputhraya/action-pr-title@master
        name: 'PR title should match with "hotfix/vX.X.X: some description" or "release/vX.X.X: some description" or "YY-XXXX: some description" (for features) or "vX.X.X: main to feature branch" (for merging release to feature branch)'
        with:
          regex: '^((([Hh]otfix|[Rr]elease)[\/| ])?((v(\d+\.)(\d+\.)(\*|\d+))): *.{10,})$|^([A-Z]{1,10}-([0-9]{1,7}): *.{10,})$' # Regex the title should match.
          github_token: ${{ secrets.GITHUB_TOKEN }}
  branch-naming-rules:
    runs-on: ubuntu-latest
    steps:
      - uses: deepakputhraya/action-branch-name@master
        name: 'Branch name should match with "hotfix/vX.X.X" or "release/vX.X.X" or "feature/YY-XXXX-some-description" (for features) or "devops/YYYY-XXXX-some-description"'
        with:
          regex: '^(((hotfix|release)\/)((v(\d+\.)(\d+\.)(\*|\d+)))(-[a-zA-Z0-9-]{7,})?)$|^(((feature|devops)\/)([A-Z]{1,10}-([0-9]{1,7})-[a-zA-Z0-9-]{7,}))$' # Regex the branch should match
          ignore: main # Ignore exactly matching branch names from  convention
  restrict-main:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Main will only accept PRs from release branches
        run: |
          branch_name=$(jq --raw-output .pull_request.head.ref $GITHUB_EVENT_PATH)
          if [[ $branch_name == feature/* ]]; then
            echo "This branch is a feature branch. Merging not allowed."
            exit 1
          elif [[ ! $branch_name == release/* ]]; then
            echo "This branch is not a release branch. Merging not allowed."
            exit 1
          else
            echo "Branch is allowed for merging."
          fi