-
Notifications
You must be signed in to change notification settings - Fork 4
223 lines (205 loc) · 7.74 KB
/
diff.yml
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
name: Diff HTML
on:
pull_request:
types:
# Defaults
- opened
- synchronize
- reopened
# Additional
- closed
env:
BRANCH_NAME_HTML_OLD: __diff__html_old
DIFF_CONTEXT: 5
DIFF_TRUNCATE_TO_BYTES: 250000
POST_COLLAPSE_GT_LINES: 10
jobs:
Build:
uses: ./.github/workflows/build.yml
with:
prettyfy: true
skip_lints_tests: true
sphinx_github_branch: __diff__html_old
Commit:
if: github.event.action == 'closed' && github.event.pull_request.merged && github.event.pull_request.base.ref == 'main'
runs-on: ubuntu-latest
needs: Build
steps:
- name: Fetch New HTML
uses: actions/download-artifact@v4
with:
name: html
path: html
- name: Store HTML in Orphaned Branch
uses: peaceiris/actions-gh-pages@v4
with:
commit_message: "[skip ci]"
enable_jekyll: true # Don't create .nojekyll
force_orphan: true # Discard git history
github_token: "${{ secrets.GITHUB_TOKEN }}"
publish_branch: "${{ env.BRANCH_NAME_HTML_OLD }}"
publish_dir: html
Diff:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
needs: Build
steps:
- name: Fetch Old HTML
uses: actions/checkout@v4
with:
ref: "${{ env.BRANCH_NAME_HTML_OLD }}"
path: html
- name: Prepare for New HTML
run: git -C html rm -r . && git -C html restore --staged .
- name: Fetch new HTML
uses: actions/download-artifact@v4
with:
name: html
path: html
- name: Git Status
id: git_status
run: |
git -C html add .
git -C html status --porcelain |tee git_status.txt
if [ -s git_status.txt ]; then
echo "changed=true" >> $GITHUB_OUTPUT
else
echo "no-changes=true" >> $GITHUB_OUTPUT
rm -v git_status.txt
fi
- name: Git Diff
id: git_diff
if: hashFiles('git_status.txt')
run: |
git -C html diff --diff-filter=M --unified="$DIFF_CONTEXT" HEAD |grep -Pv "^(diff|index) " |tee git_diff.patch
if [ -s git_diff.patch ]; then
echo "modified=true" >> $GITHUB_OUTPUT
else
echo "no-modifications=true" >> $GITHUB_OUTPUT
rm -v git_diff.patch
fi
- name: Truncate
if: hashFiles('git_diff.patch')
run: |
cp git_diff.patch git_diff.patch.orig
truncate_to=$(( DIFF_TRUNCATE_TO_BYTES - $(wc -c < git_status.txt) ))
head --bytes="$truncate_to" git_diff.patch.orig > git_diff.patch
if ! cmp -s git_diff.patch git_diff.patch.orig; then
echo -e "\n\nTruncated" |tee -a git_diff.patch
tail --bytes=+"$(( truncate_to + 1 ))" git_diff.patch.orig > git_diff.patch.truncated
fi
rm -v git_diff.patch.orig
- name: Store Metafiles as Temporary Artifacts
if: hashFiles('git_status.txt')
uses: actions/upload-artifact@v4
with:
name: git_status_diff
path: |
git_status.txt
git_diff.patch
git_diff.patch.truncated
outputs:
changed: "${{ steps.git_status.outputs.changed }}"
no-changes: "${{ steps.git_status.outputs.no-changes }}"
modified: "${{ steps.git_diff.outputs.modified }}"
no-modifications: "${{ steps.git_diff.outputs.no-modifications }}"
Post:
if: github.event.action != 'closed'
runs-on: ubuntu-latest
needs: Diff
concurrency:
cancel-in-progress: true
group: diff-post
env:
HEADER_PREFIX: "Diff Against [Old HTML]"
HEADER_URL_BASE: "https://github.com/${{ github.repository }}/tree"
steps:
- name: Fetch Metafiles
if: needs.Diff.outputs.changed
uses: actions/download-artifact@v4
with:
name: git_status_diff
- name: Split Patch
if: hashFiles('git_diff.patch')
run: |
PS4="\n\033[1;33m$PS4\033[0m"
set -euxo pipefail
sudo apt-get update
sudo apt-get install -y rename splitpatch
splitpatch git_diff.patch
rm -v git_diff.patch
rename 's/.patch.(\d{3})$/.$1.patch/' *.patch.???
ls -lah
- name: Post (No Changes)
if: needs.Diff.outputs.no-changes
uses: actions/github-script@v7
with:
script: |
const url = `${process.env.HEADER_URL_BASE}/${process.env.BRANCH_NAME_HTML_OLD}`
const header = `${process.env.HEADER_PREFIX}(${url})`
const body = `*No HTML changes detected.*`
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `### ${header}\n\n${body}`
})
- name: Post (No Modified)
if: needs.Diff.outputs.changed && needs.Diff.outputs.no-modifications
uses: actions/github-script@v7
with:
script: |
const url = `${process.env.HEADER_URL_BASE}/${process.env.BRANCH_NAME_HTML_OLD}`
const header = `${process.env.HEADER_PREFIX}(${url})`
const fs = require(`fs`)
const gitStatus = fs.readFileSync(`git_status.txt`, {encoding:`utf8`, flag:`r`}).trimEnd()
const body = `\`\`\`\n${gitStatus}\n\`\`\`\n`
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `### ${header}\n\n${body}`
})
- name: Post Diff
if: needs.Diff.outputs.changed && needs.Diff.outputs.modified
uses: actions/github-script@v7
env:
IS_TRUNCATED: "${{ hashFiles('git_diff.patch.truncated') }}"
with:
script: |
const fs = require(`fs`)
function genHeader() {
const url = `${process.env.HEADER_URL_BASE}/${process.env.BRANCH_NAME_HTML_OLD}`
var header = `${process.env.HEADER_PREFIX}(${url})`
if (process.env.IS_TRUNCATED) {
var size = fs.statSync(`git_diff.patch.truncated`).size
header += ` (${size} Byte${size == 1 ? '' : 's'} Truncated)`
}
return header
}
function* yieldFileContents(dir, suffix) {
const files = fs.readdirSync(dir).filter(fileName => fileName.endsWith(suffix))
for (const fileName of files) {
const contents = fs.readFileSync(fileName, {encoding:`utf8`, flag:`r`})
yield {fileName: fileName.slice(0, -suffix.length), contents: contents}
}
}
function collapse(fileName, contents) {
var numLines = contents.match(/^/mg).length
if (numLines > parseInt(process.env.POST_COLLAPSE_GT_LINES))
return `<details>\n<summary>Show ${numLines} lines from ${fileName}</summary>\n\n${contents}\n</details>`
return contents
}
// git status
const gitStatus = fs.readFileSync(`git_status.txt`, {encoding:`utf8`, flag:`r`})
var body = collapse(`git_status.txt`, `\`\`\`\n${gitStatus.trimEnd()}\n\`\`\``)
// git diff
for (const {fileName, contents} of yieldFileContents(`./`, `.patch`)) {
body += `\n\n` + collapse(fileName, `\`\`\`diff\n${contents.trimEnd()}\n\`\`\``)
}
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `### ${genHeader()}\n\n${body}`
})