1
+ name : Auto Format
2
+ on :
3
+ pull_request_target :
4
+ types : [opened, synchronize]
5
+ workflow_dispatch :
6
+
7
+ jobs :
8
+ auto-format :
9
+ runs-on : ubuntu-latest
10
+ container : osodevops/build-harness:latest
11
+ steps :
12
+ # Checkout the pull request branch
13
+ # "An action in a workflow run can’t trigger a new workflow run. For example, if an action pushes code using
14
+ # the repository’s GITHUB_TOKEN, a new workflow will not run even when the repository contains
15
+ # a workflow configured to run when push events occur."
16
+ # However, using a personal access token will cause events to be triggered.
17
+ # We need that to ensure a status gets posted after the auto-format commit.
18
+ # We also want to trigger tests if the auto-format made no changes.
19
+ - uses : actions/checkout@v2
20
+ if : github.event.pull_request.state == 'open'
21
+ name : Privileged Checkout
22
+ with :
23
+ token : ${{ secrets.TOPBOT_PUBLIC_REPO_ACCESS_TOKEN }}
24
+ repository : ${{ github.event.pull_request.head.repo.full_name }}
25
+ # Check out the PR commit, not the merge commit
26
+ # Use `ref` instead of `sha` to enable pushing back to `ref`
27
+ ref : ${{ github.event.pull_request.head.ref }}
28
+
29
+ # Do all the formatting stuff
30
+ - name : Auto Format
31
+ if : github.event.pull_request.state == 'open'
32
+ shell : bash
33
+ env :
34
+ GITHUB_TOKEN : " ${{ secrets.TOPBOT_PUBLIC_REPO_ACCESS_TOKEN }}"
35
+ run : make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true -f /build-harness/templates/Makefile.build-harness pr/readme/host
36
+
37
+ # Commit changes (if any) to the PR branch
38
+ - name : Commit changes to the PR branch
39
+ if : github.event.pull_request.state == 'open'
40
+ shell : bash
41
+ id : commit
42
+ env :
43
+ SENDER : ${{ github.event.sender.login }}
44
+ run : |
45
+ set -x
46
+ output=$(git diff --name-only)
47
+
48
+ if [ -n "$output" ]; then
49
+ echo "Changes detected. Pushing to the PR branch"
50
+ git config --global user.name 'osotopbot'
51
+ git config --global user.email '[email protected] '
52
+ git add -A
53
+ git commit -m "Auto Format"
54
+ # Prevent looping by not pushing changes in response to changes from cloudpossebot
55
+ [[ $SENDER == "osotopbot" ]] || git push
56
+ # Set status to fail, because the push should trigger another status check,
57
+ # and we use success to indicate the checks are finished.
58
+ printf "::set-output name=%s::%s\n" "changed" "true"
59
+ exit 1
60
+ else
61
+ printf "::set-output name=%s::%s\n" "changed" "false"
62
+ echo "No changes detected"
63
+ fi
0 commit comments