-
Notifications
You must be signed in to change notification settings - Fork 2
165 lines (150 loc) · 5.87 KB
/
FormatPullRequest.yml
File metadata and controls
165 lines (150 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
name: "FormatPullRequest"
on:
workflow_call:
inputs:
directory:
description: "The directory on which ITensorFormatter needs to be run"
default: "."
required: false
type: string
julia-version:
description: "Julia version"
default: "1"
required: false
type: string
trigger:
description: "Comment trigger phrase for on-demand formatting"
default: "/format"
required: false
type: string
jobs:
format-pull-request:
name: "FormatPullRequest"
runs-on: ubuntu-latest
if: |
github.event_name != 'issue_comment' || (
github.event.issue.pull_request != null &&
contains(fromJSON('["OWNER", "COLLABORATOR", "MEMBER"]'), github.event.comment.author_association) &&
contains(github.event.comment.body, inputs.trigger)
)
permissions:
contents: write
pull-requests: write
steps:
- name: "Get PR branch"
if: github.event_name == 'issue_comment'
id: pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
HEAD_REF=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.issue.number }} --jq '.head.ref')
echo "head_ref=$HEAD_REF" >> $GITHUB_OUTPUT
- uses: actions/checkout@v6
with:
ref: ${{ steps.pr.outputs.head_ref }}
token: ${{ secrets.FORMATPULLREQUEST_PAT || secrets.GITHUB_TOKEN }}
- name: "Setup Julia ${{ inputs.julia-version }}"
uses: julia-actions/setup-julia@v3
with:
version: "${{ inputs.julia-version }}"
arch: "${{ runner.arch }}"
- uses: julia-actions/cache@v3
- name: "Install ITensorFormatter"
run: |
julia -e '
using Pkg
Pkg.Registry.add("General")
Pkg.Registry.add(; url = "https://github.com/ITensor/ITensorRegistry")
Pkg.Apps.add("ITensorFormatter")
'
echo "$HOME/.julia/bin" >> $GITHUB_PATH
- name: "Run ITensorFormatter"
env:
DIRECTORY: ${{ inputs.directory }}
run: |
itpkgfmt "$DIRECTORY"
continue-on-error: true
- name: "Detect formatting changes"
id: changes
shell: bash
run: |
# Include newly created (untracked) files in the change detection.
git add -N .
if ! git diff --quiet; then
echo "changes=true" >> "$GITHUB_OUTPUT"
else
echo "changes=false" >> "$GITHUB_OUTPUT"
fi
# Resolve the PAT owner so commit author, PR committer, and reaction
# attribution all match the identity the token authenticates as.
# Falls back to github-actions[bot] when FORMATPULLREQUEST_PAT is
# unset (forks, etc.).
- name: "Detect token owner"
id: token-owner
env:
GH_TOKEN: ${{ secrets.FORMATPULLREQUEST_PAT || secrets.GITHUB_TOKEN }}
shell: bash
run: |
json=$(gh api user 2>/dev/null || echo '{}')
login=$(echo "$json" | jq -r '.login // ""')
id=$(echo "$json" | jq -r '.id // ""')
if [ -n "$login" ]; then
name="$login"
email="${id}+${login}@users.noreply.github.com"
else
name="github-actions[bot]"
email="github-actions[bot]@users.noreply.github.com"
fi
echo "name=$name" >> "$GITHUB_OUTPUT"
echo "email=$email" >> "$GITHUB_OUTPUT"
# issue_comment mode: commit directly to the PR branch
- name: "Commit and push changes"
if: github.event_name == 'issue_comment' && steps.changes.outputs.changes == 'true'
env:
GIT_NAME: ${{ steps.token-owner.outputs.name }}
GIT_EMAIL: ${{ steps.token-owner.outputs.email }}
run: |
git config user.name "$GIT_NAME"
git config user.email "$GIT_EMAIL"
git add -A
git commit -m "Format .jl files (ITensorFormatter)"
git push
# issue_comment mode: react to the triggering comment
- name: "React to comment"
if: github.event_name == 'issue_comment'
uses: peter-evans/create-or-update-comment@v5
with:
token: ${{ secrets.FORMATPULLREQUEST_PAT || secrets.GITHUB_TOKEN }}
comment-id: ${{ github.event.comment.id }}
reactions: '+1'
# schedule/dispatch mode: bump version and open a new PR
- name: "Bump patch version"
if: github.event_name != 'issue_comment' && steps.changes.outputs.changes == 'true'
shell: julia --color=yes {0}
run: |
import Pkg
project_file = "Project.toml"
project = Pkg.Types.read_project(project_file)
project.version = Base.nextpatch(project.version)
Pkg.Types.write_project(project, project_file)
- name: "Stage changes (including new files)"
if: github.event_name != 'issue_comment' && steps.changes.outputs.changes == 'true'
shell: bash
run: git add -A
- name: "Create pull request"
if: github.event_name != 'issue_comment' && steps.changes.outputs.changes == 'true'
id: cpr
uses: peter-evans/create-pull-request@v8
with:
token: ${{ secrets.FORMATPULLREQUEST_PAT || secrets.GITHUB_TOKEN }}
commit-message: "Format .jl files (ITensorFormatter)"
title: "Automatic ITensorFormatter run"
branch: auto-format-pr
delete-branch: true
author: "${{ steps.token-owner.outputs.name }} <${{ steps.token-owner.outputs.email }}>"
committer: "${{ steps.token-owner.outputs.name }} <${{ steps.token-owner.outputs.email }}>"
- name: "Check outputs"
if: github.event_name != 'issue_comment'
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"