|
| 1 | +--- |
| 2 | +#------------------------------------------------------------------------------ |
| 3 | +# Pull Request Workflow Controller. |
| 4 | +# |
| 5 | +# Triggers: |
| 6 | +# - Called automatically on relevant actions performed on pull requests. |
| 7 | +# - Can also be run manually by clicking the "Run workflow" button. |
| 8 | +# |
| 9 | +# Actions: |
| 10 | +# - Use semantic release rules to determine if a new release will be published. |
| 11 | +# - run Python tests, but only if Python-related files have changed. |
| 12 | +# - run Terraform tests, but only if Terraform-related files have changed. |
| 13 | +# - run ReactJS tests, but only if ReactJS-related files have changed. |
| 14 | +# - run pre-commit hooks to ensure code is formatted correctly. |
| 15 | +# |
| 16 | +# To-Do: |
| 17 | +# If a new release is to be published then we want to consider running QA tests |
| 18 | +# to ensure formatting and documentation is correct. |
| 19 | +#------------------------------------------------------------------------------ |
| 20 | +name: Pull Request Controller |
| 21 | + |
| 22 | +on: |
| 23 | + workflow_dispatch: |
| 24 | + # GitHub Copilot: The `pull_request` and `pull_request_target` are two different |
| 25 | + # event types in GitHub Actions that trigger workflows when activity related |
| 26 | + # to pull requests occurs. |
| 27 | + # - `pull_request`: This event triggers a workflow run whenever a pull |
| 28 | + # request is opened, synchronized, or closed. The workflow runs in the context of the |
| 29 | + # pull request, meaning it has access to the code and environment variables of the head |
| 30 | + # branch of the pull request. This is safe for pull requests within the same repository, |
| 31 | + # but for pull requests from a fork, this could potentially expose sensitive information. |
| 32 | + # |
| 33 | + # - `pull_request_target`: This event is similar to `pull_request`, but it runs in the context |
| 34 | + # of the base of the pull request, rather than the head. This means it has access to the code |
| 35 | + # and environment variables of the base branch, not the head branch. This is safer for |
| 36 | + # pull requests from forks, as it prevents the fork from accessing sensitive information |
| 37 | + # in the base repository. However, it means the workflow does not have access to the code |
| 38 | + # in the pull request by default. If you need to access the code in the pull request, |
| 39 | + # you can use the `actions/checkout` action with the `ref` input |
| 40 | + # set to `github.event.pull_request.head.ref`. |
| 41 | + # |
| 42 | + # In general, use `pull_request` for workflows that need to access the code in the pull request, |
| 43 | + # and `pull_request_target` for workflows that need to be safe for pull requests from forks. |
| 44 | + pull_request_target: |
| 45 | + types: [opened, synchronize] |
| 46 | + paths: |
| 47 | + - "**.py" |
| 48 | + - "**.requirements.txt" |
| 49 | + - "**.package.json" |
| 50 | + - "./secure_logger/**" |
| 51 | + |
| 52 | +env: |
| 53 | + python-version: "3.11" |
| 54 | + |
| 55 | +jobs: |
| 56 | + check_for_pending_release: |
| 57 | + name: test-semantic-release |
| 58 | + runs-on: ubuntu-latest |
| 59 | + steps: |
| 60 | + - name: Checkout |
| 61 | + uses: actions/checkout@v4 |
| 62 | + |
| 63 | + - name: Semantic Release |
| 64 | + uses: cycjimmy/semantic-release-action@v4 |
| 65 | + id: semantic |
| 66 | + with: |
| 67 | + dry_run: true |
| 68 | + branches: | |
| 69 | + [ |
| 70 | + '+([0-9])?(.{+([0-9]),x}).x', |
| 71 | + 'main', |
| 72 | + 'next', |
| 73 | + 'next-major', |
| 74 | + { |
| 75 | + name: 'beta', |
| 76 | + prerelease: true |
| 77 | + }, |
| 78 | + { |
| 79 | + name: 'alpha', |
| 80 | + prerelease: true |
| 81 | + } |
| 82 | + ] |
| 83 | + extra_plugins: | |
| 84 | + @semantic-release/git |
| 85 | + @semantic-release/changelog |
| 86 | + env: |
| 87 | + GITHUB_TOKEN: ${{ secrets.PAT }} |
| 88 | + |
| 89 | + - name: Test Outputs |
| 90 | + if: steps.semantic.outputs.new_release_published == 'true' |
| 91 | + run: | |
| 92 | + echo ${{ steps.semantic.outputs.new_release_version }} |
| 93 | + echo ${{ steps.semantic.outputs.new_release_major_version }} |
| 94 | + echo ${{ steps.semantic.outputs.new_release_minor_version }} |
| 95 | + echo ${{ steps.semantic.outputs.new_release_patch_version }} |
| 96 | +
|
| 97 | + python_tests: |
| 98 | + needs: check_for_pending_release |
| 99 | + runs-on: ubuntu-latest |
| 100 | + |
| 101 | + steps: |
| 102 | + - name: Checkout code |
| 103 | + id: checkout |
| 104 | + uses: actions/checkout@v4 |
| 105 | + |
| 106 | + - name: Check for changed files |
| 107 | + id: file_changes |
| 108 | + run: | |
| 109 | + echo "::set-output name=files_changed::$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '\.py$' || true)" |
| 110 | + echo "::set-output name=requirements_changed::$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep 'requirements.txt$' || true)" |
| 111 | +
|
| 112 | + - name: Run Python tests |
| 113 | + if: steps.file_changes.outputs.files_changed != '' || steps.file_changes.outputs.requirements_changed != '' |
| 114 | + uses: ./.github/actions/tests/python |
| 115 | + with: |
| 116 | + python-version: "${{ env.python-version}}" |
0 commit comments