-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
286 lines (251 loc) · 10.3 KB
/
generate-hacs-data.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
name: Generate HACS Data
on:
workflow_dispatch:
inputs:
forceRepositoryUpdate:
description: 'Force repository update'
required: false
default: 'False'
type: choice
options:
- "False"
- "True"
category:
description: 'Select a category'
required: false
type: choice
options:
- None
- appdaemon
- integration
- plugin
- python_script
- template
- theme
schedule:
- cron: "0 */2 * * *"
concurrency:
group: category-data
jobs:
generate-matrix:
name: Generate matrix
runs-on: ubuntu-latest
if: github.repository == 'hacs/integration'
outputs:
categories: ${{ steps.set-matrix.outputs.categories }}
steps:
- id: set-matrix
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]] && [[ "${{ inputs.category }}" != "None" ]] && [[ "${{ inputs.category }}" != "" ]]; then
echo "categories=['${{ inputs.category }}']" >> $GITHUB_OUTPUT
else
echo "categories=['appdaemon','integration','plugin','python_script','template','theme']" >> $GITHUB_OUTPUT
fi
category-data:
runs-on: ubuntu-latest
needs: generate-matrix
if: github.repository == 'hacs/integration'
name: Generate ${{ matrix.category }} data
strategy:
fail-fast: false
matrix:
category: ${{ fromJSON( needs.generate-matrix.outputs.categories )}}
steps:
- name: Checkout the repository
uses: actions/[email protected]
- name: Set up Python
uses: actions/[email protected]
id: python
with:
python-version: "3.12"
cache: 'pip'
cache-dependency-path: |
requirements_base.txt
requirements_generate_data.txt
- name: Install dependencies
run: |
scripts/install/frontend
scripts/install/pip_packages --requirement requirements_generate_data.txt
- name: Generate ${{ matrix.category }} data
run: python3 -m scripts.data.generate_category_data ${{ matrix.category }}
env:
DATA_GENERATOR_TOKEN: ${{ secrets.DATA_GENERATOR_TOKEN }}
FORCE_REPOSITORY_UPDATE: ${{ inputs.forceRepositoryUpdate }}
- name: Validate output with JQ
run: |
jq -c . outputdata/${{ matrix.category }}/data.json
jq -c . outputdata/${{ matrix.category }}/repositories.json
- name: Validate output with schema
run: |
python3 -m scripts.data.validate_category_data ${{ matrix.category }} outputdata/${{ matrix.category }}/data.json
- name: Generate diff
run: |
diff -U 8 outputdata/diff/${{ matrix.category }}_before.json outputdata/diff/${{ matrix.category }}_after.json > outputdata/diff/${{ matrix.category }}.diff || true
cat outputdata/diff/${{ matrix.category }}.diff
- name: Upload diff
uses: actions/[email protected]
env:
CATEGORY: ${{ matrix.category }}
with:
script: |
const fs = require('fs');
const diffContents = fs.readFileSync(`outputdata/diff/${process.env.CATEGORY}.diff`);
core.summary.addDetails(`${process.env.CATEGORY}.diff contents`, `\n\n\`\`\`diff\n${diffContents}\`\`\`\n\n`)
core.summary.write()
- name: Upload artifacts
uses: actions/[email protected]
with:
name: ${{ matrix.category }}
path: |
outputdata/${{ matrix.category }}
outputdata/summary.json
outputdata/diff
if-no-files-found: error
retention-days: 7
summarize:
name: Summarize
runs-on: ubuntu-latest
needs: category-data
if: github.repository == 'hacs/integration'
outputs:
changedCategories: ${{ steps.combined.outputs.changedCategories }}
environments: ${{ steps.combined.outputs.environments }}
steps:
- name: Download artifacts
uses: actions/[email protected]
with:
path: outputdata
- name: Generate combined summary
uses: actions/[email protected]
id: combined
env:
HACS_CHANGED_PCT_TARGET: ${{ vars.HACS_CHANGED_PCT_TARGET }}
HACS_DIFF_TARGET: ${{ vars.HACS_DIFF_TARGET }}
with:
script: |
const fs = require('fs');
const summaries = {};
const changedCategories = [];
const environments = {};
const diffTarget = Number(process.env.HACS_DIFF_TARGET || 1)
core.info(`[global] diffTarget: ${diffTarget}`);
const subDirectories = fs.readdirSync("outputdata", { withFileTypes: true })
.filter(entry => entry.isDirectory())
.map(entry => entry.name)
for (const directory of subDirectories) {
let changedPctTarget = Number(process.env.HACS_CHANGED_PCT_TARGET)
const parsed = JSON.parse(fs.readFileSync(`outputdata/${directory}/summary.json`))
if (!changedPctTarget) {
if (!parsed.new_count || parsed.new_count <= 0) {
changedPctTarget = 0;
} else if (parsed.new_count > 750) {
changedPctTarget = 7;
} else if (parsed.new_count > 500) {
changedPctTarget = 8;
} else if (parsed.new_count > 250) {
changedPctTarget = 9;
} else if (parsed.new_count > 100) {
changedPctTarget = 10;
} else if (parsed.new_count > 75) {
changedPctTarget = 15;
} else if (parsed.new_count > 50) {
changedPctTarget = 20;
} else if (parsed.new_count > 25) {
changedPctTarget = 25;
} else if (parsed.new_count > 10) {
changedPctTarget = 28;
} else {
changedPctTarget = 50;
}
}
core.info(`[${directory}] changedPctTarget: ${changedPctTarget}`);
if (parsed.changed_pct >= 1 || parsed.diff >= 1) {
changedCategories.push(directory)
}
if (parsed.changed_pct >= changedPctTarget) {
core.warning(`${directory} changed ${parsed.changed_pct}%!`)
environments[directory] = `publish-${directory}-verify`;
}
if (parsed.diff >= diffTarget) {
core.warning(`${directory} changed ${parsed.diff}!`)
environments[directory] = `publish-${directory}-verify`;
}
summaries[directory] = JSON.parse(fs.readFileSync(`outputdata/${directory}/summary.json`));
}
core.summary.addCodeBlock(JSON.stringify({summaries, environments, changedCategories}, null, 4), "json")
core.summary.write()
core.setOutput("changedCategories", JSON.stringify(changedCategories))
core.setOutput("environments", environments)
- name: Send notification
if: ${{ steps.combined.outputs.environments != '{}' }}
run: |
curl \
-H "Content-Type: application/json" \
-d '{"username": "GitHub action", "content": "[Attention needed!](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}})"}' \
${{ secrets.DISCORD_WEBHOOK_ACTION_FAILURE }}
publish:
runs-on: ubuntu-latest
needs: summarize
if: github.repository == 'hacs/integration'
name: Publish ${{ matrix.category }} data
environment: ${{ fromJSON(needs.summarize.outputs.environments)[matrix.category] }}
strategy:
fail-fast: false
matrix:
category: ${{ fromJSON(needs.summarize.outputs.changedCategories) }}
steps:
- name: Checkout the repository
uses: actions/[email protected]
- name: Set up Python
uses: actions/[email protected]
id: python
with:
python-version: "3.12"
cache: 'pip'
cache-dependency-path: |
requirements_base.txt
requirements_generate_data.txt
- name: Install dependencies
run: |
scripts/install/frontend
scripts/install/pip_packages --requirement requirements_generate_data.txt
- name: Download artifacts
uses: actions/[email protected]
with:
name: ${{ matrix.category }}
path: outputdata
- name: Validate output with JQ
run: |
jq -c . outputdata/${{ matrix.category }}/data.json
jq -c . outputdata/${{ matrix.category }}/repositories.json
- name: Validate output with schema
run: |
python3 -m scripts.data.validate_category_data ${{ matrix.category }} outputdata/${{ matrix.category }}/data.json
- name: Upload to R2
run: |
aws s3 sync \
outputdata/${{ matrix.category }} \
s3://data-v2/${{ matrix.category }} \
--endpoint-url ${{ secrets.CF_R2_ENDPOINT_DATA }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.CF_R2_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.CF_R2_SECRET_ACCESS_KEY }}
- name: Bust Cloudflare cache
run: |
curl --silent --show-error --fail -X POST \
"https://api.cloudflare.com/client/v4/zones/${{ secrets.CF_ZONE_ID }}/purge_cache" \
-H "Authorization: Bearer ${{ secrets.CF_BUST_CACHE_TOKEN }}" \
-H "Content-Type: application/json" \
--data '{"files": ["https:\/\/data-v2.hacs.xyz\/${{ matrix.category }}\/data.json", "https:\/\/data-v2.hacs.xyz\/${{ matrix.category }}\/repositories.json"]}'
notify_on_failure:
runs-on: ubuntu-latest
name: Trigger Discord notification when jobs fail
needs: ["generate-matrix", "category-data", "summarize", "publish"]
steps:
- name: Send notification
if: ${{ always() && contains(join(needs.*.result, ','), 'failure') && github.event_name == 'schedule' }}
run: |
curl \
-H "Content-Type: application/json" \
-d '{"username": "GitHub action failure", "content": "[Scheduled action failed!](https://github.com/${{github.repository}}/actions/runs/${{github.run_id}})"}' \
${{ secrets.DISCORD_WEBHOOK_ACTION_FAILURE }}