-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
343 lines (288 loc) · 11.8 KB
/
action.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
name: Document Action Workflows in Repo Wiki
description: Reads the workflow files in the repo and creates a wiki page with the workflow details.
inputs:
github-pat:
description: The github personal access token used for interactions with github.
required: true
save-to-artifact:
description: If true, save to documentation as an artifact. This is mostly used for testing purposes.
required: false
default: 'false'
save-to-branch:
description: If true, save to documentation to a branch.
required: false
default: 'false'
save-to-wiki:
description: If true, save to documentation to repository's wiki.
required: false
default: 'false'
permissions:
contents: write
wikis: write
runs:
using: composite
steps:
- name: Set inputs as outputs
id: input-conditions
shell: bash
run: |
set -x
echo "save-to-artifact=${{ inputs.save-to-artifact }}" >> $GITHUB_OUTPUT
echo "save-to-branch=${{ inputs.save-to-branch }}" >> $GITHUB_OUTPUT
echo "save-to-wiki=${{ inputs.save-to-wiki }}" >> $GITHUB_OUTPUT
- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install Python Dependencies
shell: bash
run: |
set -x
pip install PyYAML==6.0.1 cron_descriptor==1.4.0
- name: Checkout Repo Wiki
if: steps.input-conditions.outputs['save-to-wiki'] == 'true'
uses: actions/checkout@v3
with:
repository: ${{ github.repository }}.wiki
path: wiki
token: ${{ inputs.github-pat }}
- name: Document Workflows
shell: python
env:
SAVE_TO_WIKI: ${{ steps.input-conditions.outputs['save-to-wiki'] }}
SAVE_TO_BRANCH: ${{ steps.input-conditions.outputs['save-to-branch'] }}
SAVE_TO_ARTIFACT: ${{ steps.input-conditions.outputs['save-to-artifact'] }}
run: |
print("Starting Workflow Documentation")
import json
import os
import sys
import yaml
def get_triggers(triggers) -> list:
trigger_data = []
for trigger_name in triggers:
trigger = triggers.get(trigger_name)
if trigger_name == "schedule":
cron = trigger.get("cron")
description = get_description(cron)
trigger_data.append({
"name": trigger_name,
"value": cron,
"readable": description
})
elif trigger_name == "workflow_dispatch":
trigger_data.append({
"name": trigger_name,
"value": trigger,
"readable": "Manually triggered"
})
elif trigger_name == "repository_dispatch":
trigger_data.append({
"name": trigger_name,
"value": trigger,
"readable": "API triggered"
})
else:
trigger_data.append({
"name": trigger_name,
"value": trigger,
"readable": trigger
})
return trigger_data
def get_jobs(jobs) -> list:
job_data = []
for job_name in jobs:
job = jobs.get(job_name)
runs_on = None
if isinstance(job.get("runs-on"), list):
runs_on = ", ".join(job.get("runs-on"))
elif isinstance(job.get("runs-on"), str):
runs_on = job.get("runs-on")
uses = None
if job.get("uses"):
uses = job.get("uses")
steps_data = []
steps = job.get("steps")
if steps:
for step_name in steps:
steps_data.append(step_name)
with_data = []
withs = job.get("with")
if withs:
for with_name in withs:
with_data.append(with_name)
job_data.append({
"name": job_name,
"runs_on": runs_on,
"uses": uses,
"steps": steps_data,
"with": with_data
})
return job_data
def create_markdown(workflows: dict, save_to_branch: bool, save_to_wiki: bool):
path_wiki = None
path_docs = None
if not save_to_branch and not save_to_wiki:
print("No save location provided. Exiting")
return
if save_to_wiki:
dir_wiki = os.path.join("wiki")
if not os.path.exists(dir_wiki):
os.mkdir(dir_wiki)
path_wiki = os.path.join(dir_wiki, "WORKFLOWS.md")
if save_to_branch:
path_docs = os.path.join("WORKFLOWS.md")
markdown = [
"# Workflows in this repository"
]
for workflow_file in workflows:
workflow = workflows.get(workflow_file)
name = workflow.get("name")
triggers = workflow.get("triggers")
jobs = workflow.get("jobs")
markdown.append(f"## {name}")
markdown.append("<details><summary>View Triggers and Jobs</summary>")
markdown.append("<p>")
print(f"Creating markdown for {name}")
triggers_md = [
"<h3>Triggers</h3>",
"<table valign='top'>",
"<thead>",
"<tr>",
"<th>ON</th>",
"<th>DETAILS</th>",
"<th>VALUE</th>",
"</tr>",
"</thead>",
"<tbody>"
]
for trigger in triggers:
name = trigger.get("name")
value = trigger.get("value")
readable = trigger.get("readable")
triggers_md.append("<tr>")
triggers_md.append(f"<td valign='top'>{name}</td>")
triggers_md.append(f"<td valign='top'>{readable}</td>")
triggers_md.append(f"<td valign='top'><pre>{value}</pre></td>")
triggers_md.append("</tr>")
triggers_md.append("</tbody>")
triggers_md.append("</table>")
triggers_md.append("")
markdown.extend(triggers_md)
jobs_md = [
"### Jobs",
"<table valign='top'>",
"<thead>",
"<tr>",
"<th>NAME</th>",
"<th>RUNS-ON or USES</th>",
"<th>STEPS</th>",
"</tr>",
"</thead>",
"<tbody>"
]
for job in jobs:
name = job.get("name")
runs_on = job.get("runs_on")
uses = job.get("uses")
steps = job.get("steps")
jobs_md.append("<tr>")
jobs_md.append(f"<td valign='top'>{name}</td>")
if runs_on:
jobs_md.append(f"<td valign='top'>{runs_on}</td>")
elif uses:
jobs_md.append(f"<td valign='top'>{uses}</td>")
else:
jobs_md.append(f"<td valign='top'></td>")
if steps:
jobs_md.append("<td>")
jobs_md.append("<ol>")
for step_name in steps:
jobs_md.append(f"<li>{step_name}</li>")
jobs_md.append("</ol>")
jobs_md.append("</td>")
else:
jobs_md.append("<td></td>")
jobs_md.append("</tr>")
jobs_md.append("</tbody>")
jobs_md.append("</table>")
jobs_md.append("")
markdown.extend(jobs_md)
markdown.append("</p>")
markdown.append("</details>")
markdown.append("")
if path_wiki:
print("Saving docs to Wiki")
with open(path_wiki, "w") as writer:
markdown_str = os.linesep.join(markdown)
writer.write(markdown_str)
if path_docs:
print("Saving docs to branch")
with open(path_docs, "w") as writer:
markdown_str = os.linesep.join(markdown)
writer.write(markdown_str)
save_to_wiki = str(os.getenv("SAVE_TO_WIKI", "false")).lower() == "true"
save_to_branch = str(os.getenv("SAVE_TO_BRANCH", "false")).lower() == "true"
save_to_artifact = str(os.getenv("SAVE_TO_ARTIFACT", "false")).lower() == "true"
if not save_to_branch:
save_to_branch = save_to_artifact
path_workflows = os.path.join(".github", "workflows")
if not os.path.exists(path_workflows):
print(f"Could not find workflows at {path_workflows}")
sys.exit(0)
files_in_workflows = os.listdir(path_workflows)
workflows = {}
for file_name in files_in_workflows:
if not file_name.endswith(".yml"):
continue
print(f"Reading {file_name}")
path_file = os.path.join(path_workflows, file_name)
workflow = None
with open(path_file, "r") as reader:
workflow = yaml.safe_load(reader)
name = workflow.get("name")
triggers = get_triggers(workflow.get(True))
jobs = get_jobs(workflow.get("jobs"))
workflows[file_name] = {
"name": name,
"triggers": triggers,
"jobs": jobs
}
create_markdown(workflows, save_to_branch, save_to_wiki)
print("DONE!")
- name: Save Changes to Wiki
if: steps.input-conditions.outputs['save-to-wiki'] == 'true'
shell: bash
run: |
set -x
cd wiki
git config --global user.name "action-workflow-documenter"
git config --global user.email "[email protected]"
git remote set-url origin https://x-access-token:${{ inputs.github-pat }}@github.com/$GITHUB_REPOSITORY
git add .
git diff-index --quiet HEAD || git commit -m "Update wiki with latest workflow documentation" && git push
- name: Save Changes to Branch
if: steps.input-conditions.outputs['save-to-branch'] == 'true'
shell: bash
run: |
set -x
git config --global user.name "action-workflow-documenter"
git config --global user.email "[email protected]"
git remote set-url origin https://x-access-token:${{ inputs.github-pat }}@github.com/$GITHUB_REPOSITORY
git add .
git checkout -b actions-documentation
git commit -m "Add actions documentation"
git push --set-upstream origin actions-documentation
- name: Save Changes to Artifact
if: steps.input-conditions.outputs['save-to-artifact'] == 'true'
uses: actions/upload-artifact@v4
with:
name: actions-documentation
path: WORKFLOWS.md
retention-days: 1
- name: Cleanup Changes
if: steps.input-conditions.outputs['save-to-artifact'] == 'true'
shell: bash
run: |
set -x
rm WORKFLOWS.md