name: OpenOSRS - CI (PR)

on:
  pull_request:
    types: ['opened', 'edited', 'reopened', 'synchronize']

jobs:
  pr-lint:
    name: PR title
    runs-on: ubuntu-latest

    steps:
      - name: PR title lint
        if: github.event_name == 'pull_request'
        uses: morrisoncole/pr-lint-action@v1.4.1
        with:
          title-regex: '^([\w-/]+): \w+'
          on-failed-regex-fail-action: false
          on-failed-regex-request-changes: false
          on-failed-regex-create-review: true
          on-failed-regex-comment: "Your PR title should match the following regex: `%regex%`!"
          repo-token: ${{ secrets.GITHUB_TOKEN }}

  validate:
    name: Validate gradle wrapper
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2.3.4
      - uses: gradle/wrapper-validation-action@v1.0.3

  build:
    name: Build
    needs: [pr-lint, validate]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - name: Make gradlew executable
        run: chmod +x ./gradlew
      - name: Set up JDK 11
        uses: actions/setup-java@v1.4.3
        with:
          java-version: 11
      - name: Assembling
        run: ./gradlew assemble --console=plain
      - name: Building
        run: ./gradlew build --stacktrace -x test -x checkstyleMain -x checkstyleTest --console=plain

  test:
    name: Test
    needs: [pr-lint, validate]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - name: Make gradlew executable
        run: chmod +x ./gradlew
      - name: Set up JDK 11
        uses: actions/setup-java@v1.4.3
        with:
          java-version: 11
      - name: Assembling
        run: ./gradlew assemble --console=plain
      - name: Testing
        run: ./gradlew test -x deobfuscator:test --stacktrace --console=plain

  checkstyleMain:
    name: Checkstyle main
    needs: [pr-lint, validate]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - name: Make gradlew executable
        run: chmod +x ./gradlew
      - name: Set up JDK 11
        uses: actions/setup-java@v1.4.3
        with:
          java-version: 11
      - name: Assembling
        run: ./gradlew assemble --console=plain
      - name: Checking code conventions
        run: ./gradlew checkstyleMain --console=plain

  checkstyleTest:
    name: Checkstyle test
    needs: [pr-lint, validate]
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2.3.4
      - uses: actions/cache@v2.1.3
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - name: Make gradlew executable
        run: chmod +x ./gradlew
      - name: Set up JDK 11
        uses: actions/setup-java@v1.4.3
        with:
          java-version: 11
      - name: Assembling
        run: ./gradlew assemble --console=plain
      - name: Checking code conventions
        run: ./gradlew checkstyleTest --console=plain

  approve:
    name: Approve
    needs: [build, test, checkstyleMain, checkstyleTest]
    runs-on: ubuntu-latest

    steps:
      - name: Approve pull request
        if: github.event_name == 'pull_request' && github.actor == 'OpenOSRS'
        uses: hmarr/auto-approve-action@v2.0.0
        with:
          github-token: ${{ secrets.Owain }}
