|
| 1 | +name: 'Format code' |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +jobs: |
| 8 | + format: |
| 9 | + name: 'Format code' |
| 10 | + runs-on: ubuntu-latest |
| 11 | + if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/format') |
| 12 | + steps: |
| 13 | + - name: 'Post acknowledgement that it will format code' |
| 14 | + continue-on-error: true # Never fail the build if this fails |
| 15 | + uses: actions/github-script@6e5ee1dc1cb3740e5e5e76ad668e3f526edbfe45 # 2.0.0 |
| 16 | + with: |
| 17 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 18 | + script: | |
| 19 | + github.issues.createComment({ |
| 20 | + issue_number: context.issue.number, |
| 21 | + owner: context.repo.owner, |
| 22 | + repo: context.repo.repo, |
| 23 | + body: 'The "Format code" action has started running.' |
| 24 | + }) |
| 25 | +
|
| 26 | + - name: 'Download PR data' |
| 27 | + run: | |
| 28 | + PR_DATA="/tmp/pr.json" |
| 29 | +
|
| 30 | + jq -r ".issue.pull_request.url" "$GITHUB_EVENT_PATH" | \ |
| 31 | + xargs curl --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' -o "$PR_DATA" --url |
| 32 | +
|
| 33 | + - name: 'Check fork status' |
| 34 | + id: fork_status |
| 35 | + run: | |
| 36 | + IS_FORK="$(jq '.head.repo.fork' "/tmp/pr.json")" |
| 37 | + echo "::set-output name=fork::$IS_FORK" |
| 38 | +
|
| 39 | + - name: 'Setup SSH deploy key' |
| 40 | + if: steps.fork_status.outputs.fork == 'false' |
| 41 | + run: | |
| 42 | + mkdir ~/.ssh |
| 43 | + echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519 |
| 44 | + chmod 600 ~/.ssh/id_ed25519 |
| 45 | +
|
| 46 | + - name: 'Checkout code' |
| 47 | + run: | |
| 48 | + PR_DATA="/tmp/pr.json" |
| 49 | +
|
| 50 | + HEAD_REF=$(jq -r ".head.ref" "$PR_DATA") |
| 51 | +
|
| 52 | + if [ ${{ steps.fork_status.outputs.fork }} == "false" ]; then |
| 53 | + echo "::debug::Setting up repo using SSH" |
| 54 | + HEAD_REPO=$(jq -r '.head.repo.ssh_url' "$PR_DATA") |
| 55 | + else |
| 56 | + echo "::debug::Setting up repo using HTTPS" |
| 57 | + HEAD_REPO=$(jq -r '.head.repo.clone_url | sub("https://"; "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@")' "$PR_DATA") |
| 58 | + fi |
| 59 | +
|
| 60 | + git clone $HEAD_REPO . |
| 61 | + git checkout -b "$HEAD_REF" "origin/$HEAD_REF" |
| 62 | +
|
| 63 | + - name: 'Format code' |
| 64 | + run: ./bin/format.sh |
| 65 | + env: |
| 66 | + EXERCISM_PRETTIER_VERSION: '2.2.1' |
| 67 | + |
| 68 | + - name: 'Commit formatted code' |
| 69 | + run: | |
| 70 | + # Check if there is nothing to commit (i.e. no formatting changes made) |
| 71 | + if [ -z "$(git status --porcelain)" ]; then |
| 72 | + echo "Code is already formatted correctly" |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | +
|
| 76 | + # Setup the git user (required to commit anything) |
| 77 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 78 | + git config --global user.name "github-actions[bot]" |
| 79 | +
|
| 80 | + # Commit the changes made by prettier |
| 81 | + git add . |
| 82 | + git commit -m "[CI] Format code" |
| 83 | + git push |
| 84 | +
|
| 85 | + - name: 'Post acknowledgement that it has formatted the code' |
| 86 | + continue-on-error: true # Never fail the build if this fails |
| 87 | + uses: actions/github-script@6e5ee1dc1cb3740e5e5e76ad668e3f526edbfe45 # 2.0.0 |
| 88 | + with: |
| 89 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + script: | |
| 91 | + github.issues.createComment({ |
| 92 | + issue_number: context.issue.number, |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + body: 'The "Format code" action has finished running.' |
| 96 | + }) |
| 97 | +
|
| 98 | + - name: 'Post reminder to trigger build manually' |
| 99 | + continue-on-error: true # Never fail the build if this fails |
| 100 | + if: steps.fork_status.outputs.fork == 'true' |
| 101 | + uses: actions/github-script@6e5ee1dc1cb3740e5e5e76ad668e3f526edbfe45 # 2.0.0 |
| 102 | + with: |
| 103 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 104 | + script: | |
| 105 | + github.issues.createComment({ |
| 106 | + issue_number: context.issue.number, |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + body: 'For security reasons, `/format` does not trigger CI builds when the PR has been submitted from a fork. If checks were not passing due to code format, trigger a build to make the required checks pass, through one of the following ways:\n\n- Push an empty commit to this branch: `git commit --allow-empty -m "Trigger builds"`.\n- Close and reopen the PR.\n- Push a regular commit to this branch.' |
| 110 | + }) |
0 commit comments