|
| 1 | +name: Auto Format |
| 2 | +on: |
| 3 | + pull_request_target: |
| 4 | + types: [opened, synchronize] |
| 5 | + |
| 6 | +jobs: |
| 7 | + auto-format: |
| 8 | + runs-on: ubuntu-latest |
| 9 | + container: cloudposse/build-harness:slim-latest |
| 10 | + steps: |
| 11 | + # Checkout the pull request branch |
| 12 | + # "An action in a workflow run can’t trigger a new workflow run. For example, if an action pushes code using |
| 13 | + # the repository’s GITHUB_TOKEN, a new workflow will not run even when the repository contains |
| 14 | + # a workflow configured to run when push events occur." |
| 15 | + # However, using a personal access token will cause events to be triggered. |
| 16 | + # We need that to ensure a status gets posted after the auto-format commit. |
| 17 | + # We also want to trigger tests if the auto-format made no changes. |
| 18 | + - uses: actions/checkout@v2 |
| 19 | + if: github.event.pull_request.state == 'open' |
| 20 | + name: Privileged Checkout |
| 21 | + with: |
| 22 | + token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }} |
| 23 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 24 | + # Check out the PR commit, not the merge commit |
| 25 | + # Use `ref` instead of `sha` to enable pushing back to `ref` |
| 26 | + ref: ${{ github.event.pull_request.head.ref }} |
| 27 | + |
| 28 | + # Do all the formatting stuff |
| 29 | + - name: Auto Format |
| 30 | + if: github.event.pull_request.state == 'open' |
| 31 | + shell: bash |
| 32 | + run: make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true -f /build-harness/templates/Makefile.build-harness pr/auto-format/host |
| 33 | + |
| 34 | + # Commit changes (if any) to the PR branch |
| 35 | + - name: Commit changes to the PR branch |
| 36 | + if: github.event.pull_request.state == 'open' |
| 37 | + shell: bash |
| 38 | + id: commit |
| 39 | + env: |
| 40 | + SENDER: ${{ github.event.sender.login }} |
| 41 | + run: | |
| 42 | + set -x |
| 43 | + output=$(git diff --name-only) |
| 44 | +
|
| 45 | + if [ -n "$output" ]; then |
| 46 | + echo "Changes detected. Pushing to the PR branch" |
| 47 | + git config --global user.name 'cloudpossebot' |
| 48 | + git config --global user.email '[email protected]' |
| 49 | + git add -A |
| 50 | + git commit -m "Auto Format" |
| 51 | + # Prevent looping by not pushing changes in response to changes from cloudpossebot |
| 52 | + [[ $SENDER == "cloudpossebot" ]] || git push |
| 53 | + # Set status to fail, because the push should trigger another status check, |
| 54 | + # and we use success to indicate the checks are finished. |
| 55 | + printf "::set-output name=%s::%s\n" "changed" "true" |
| 56 | + exit 1 |
| 57 | + else |
| 58 | + printf "::set-output name=%s::%s\n" "changed" "false" |
| 59 | + echo "No changes detected" |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Auto Test |
| 63 | + uses: cloudposse/actions/github/[email protected] |
| 64 | + # match users by ID because logins (user names) are inconsistent, |
| 65 | + # for example in the REST API Renovate Bot is `renovate[bot]` but |
| 66 | + # in GraphQL it is just `renovate`, plus there is a non-bot |
| 67 | + # user `renovate` with ID 1832810. |
| 68 | + # Mergify bot: 37929162 |
| 69 | + # Renovate bot: 29139614 |
| 70 | + # Cloudpossebot: 11232728 |
| 71 | + # Need to use space separators to prevent "21" from matching "112144" |
| 72 | + if: > |
| 73 | + contains(' 37929162 29139614 11232728 ', format(' {0} ', github.event.pull_request.user.id)) |
| 74 | + && steps.commit.outputs.changed == 'false' && github.event.pull_request.state == 'open' |
| 75 | + with: |
| 76 | + token: ${{ secrets.PUBLIC_REPO_ACCESS_TOKEN }} |
| 77 | + repository: cloudposse/actions |
| 78 | + event-type: test-command |
| 79 | + client-payload: |- |
| 80 | + { "slash_command":{"args": {"unnamed": {"all": "all", "arg1": "all"}}}, |
| 81 | + "pull_request": ${{ toJSON(github.event.pull_request) }}, |
| 82 | + "github":{"payload":{"repository": ${{ toJSON(github.event.repository) }}, |
| 83 | + "comment": {"id": ""} |
| 84 | + } |
| 85 | + } |
| 86 | + } |
0 commit comments