forked from hpcc-systems/HPCC-Platform
-
Notifications
You must be signed in to change notification settings - Fork 0
229 lines (200 loc) · 9.96 KB
/
Actions-workflow.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
name: Generate Graphs for GitHub Action workflows
on:
push:
branches:
- master
paths:
- .github/workflows/*.yml
- .github/workflows/*.yaml
# pull_request: #trigger on pull_request is just for testing purpose
workflow_dispatch:
jobs:
main:
runs-on: ubuntu-22.04
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- uses: "actions/setup-python@v5"
with:
python-version: "3.8"
- name: generate Graph
shell: python
run: |
import glob
class YamlFileObject():
def __init__(self, filename):
self.filename = filename
self.data = None
self.read()
def read(self):
with open(self.filename, 'r') as file:
self.data = file.readlines()
def getFileContent(self):
return iter(self.data)
def getNextLine(file):
try:
line = next(file)
while not line.strip("\n ") or line.strip("\n ").startswith('#'):
line = next(file)
except StopIteration :
return "EOF"
return line.strip('\n')
def getColumnNum(line): #gets the Column number of first word
i = 0
while line[i] == ' ':
i += 1
columnNum = i
return columnNum
def getWorkflowName(baseDir,file):
workflowDetails[file] = {'name':file.split('.')[0]}
# name: is not mandatory, so by default assign the name of the yaml file as the workflow name by removing the .yml/.yaml extension
yamlFile = YamlFileObject(baseDir+file)
fileContent = yamlFile.getFileContent()
line = getNextLine(fileContent)
while line != "EOF":
if line.startswith("name:"): # if 'name:' is present in the yaml file then update it
nameLine = line.split(":")[1]
name = nameLine.strip(" ")
workflowDetails[file] = {'name':name}
getWorkflowTriggers(yamlFile,file)
getWorkflowJobs(yamlFile,file)
break
line = getNextLine(fileContent)
def getWorkflowJobs(yamlFile,file):
fileContent = yamlFile.getFileContent()
ColumnNumOfJob = -1
workflowDetails[file]['jobs'] = {}
line = getNextLine(fileContent)
while line != "EOF" :
if line.startswith("jobs:"):
line = getNextLine(fileContent)
ColumnNumOfJob = getColumnNum(line)
break
line = getNextLine(fileContent)
while ColumnNumOfJob != -1 and line != "EOF" :
if line[ColumnNumOfJob] != ' ' :
jobName = line.strip(" ") #remove trailing spaces
jobName = jobName[:-1] #remove ':' at the end
workflowDetails[file]['jobs'][jobName] = {'name':jobName}
getJobDetails(yamlFile,file,jobName,ColumnNumOfJob)
line = getNextLine(fileContent)
def getJobDetails(yamlFile,file,jobName,columnNum):
fileContent = yamlFile.getFileContent()
for line in fileContent:
if line.strip("\n ") == jobName+':' :
line = getNextLine(fileContent)
subColumnNum = getColumnNum(line)
workflowDetails[file]['jobs'][jobName] = {'name':jobName,'uses':"",'needs':"",'inputs':[] }
job = workflowDetails[file]['jobs'][jobName]
while len(line) > columnNum and line[columnNum] == ' ':
if line.find('name:') != -1 and line[subColumnNum:subColumnNum+4]=='name':
name = line.split(':')[1]
name = name.strip()
job['name'] = name
elif line.find('uses:') != -1 :
uses = line.split(':')[1]
uses = uses.strip(' ')
job['uses'] = uses
elif line.find('needs:') != -1 :
needs = line.split(':')[1]
needs = needs.strip(" ")
job['needs'] = needs
elif line.find('with:') != -1 :
columnNumOfWith = line.find('with:')
line = getNextLine(fileContent)
while len(line) > columnNumOfWith and line[columnNumOfWith] == " " :
input = line.strip(" ")
if len(input) > 30:
input = input[:30]+'...'
job['inputs'].append(input)
line = getNextLine(fileContent)
if(line.find('steps:') != -1 or line == "EOF" or line[columnNum] != " "):
break
line = getNextLine(fileContent)
def getWorkflowTriggers(yamlFile,file):
workflowDetails[file]['trigger'] = []
fileContent = yamlFile.getFileContent()
for line in fileContent:
if line.strip("\n ").startswith("on:"):
line = line.split(':')[1]
line = line.strip("\n ")
if line and not line.startswith('#'): # This case handles triggers which are declared on the same line of "on:" key example: on: [push, pull_request]
line = line.replace("[","")
line = line.replace("]","")
line = line.replace(" ","")
line = line.replace('#',',#')
for trigger in line.split(','):
if not trigger.startswith('#'):
workflowDetails[file]['trigger'].append(trigger)
else : # Here it handles triggers that are declared on the next line of "on:" key
columnNumOfOn = 0
line = getNextLine(fileContent)
subColumnNum = getColumnNum(line)
while line[columnNumOfOn] == " ":
if line[subColumnNum] != " ":
trigger = line.strip(" ")
trigger = trigger.split(':')[0]
workflowDetails[file]['trigger'].append(trigger)
line = getNextLine(fileContent)
workflowsDir = '.github/workflows/'
yaml_files = glob.glob(workflowsDir+'*.y*ml')
workflowDetails = {} #This is the main dictionary where all the workflow details are stored
for file in sorted(yaml_files):
file = file.split('/')[-1]
getWorkflowName(workflowsDir,file)
markdown = workflowsDir+'ActionFlow.md'
with open( markdown ,'w') as markdownFile:
markdownFile.write('# GitHub Actions Workflow Overview\n\n')
sortedDictionary = dict(sorted(workflowDetails.items(),key = lambda item:item[1]['name']))
for file in sortedDictionary:
uniqFileName = workflowDetails[file]['name']
uniqFileName = uniqFileName.replace(' ','_') #replace spaces with underscore
uniqFileName = uniqFileName.replace('(','_') #replace parenthesis with underscore
uniqFileName = uniqFileName.replace(')','_')
uniqFileName = uniqFileName.upper()
fileBlock = f"{uniqFileName}[\"{workflowDetails[file]['name']}[{file}]\"]"
jobs = workflowDetails[file]['jobs']
markdownFile.write('```mermaid\n')
markdownFile.write('\nflowchart LR\n')
for job in jobs:
needs = jobs[job]['needs']
needs = needs.replace('[','')
needs = needs.replace(']','')
needs = needs.replace(' ','')
if len(needs) == 0:
markdownFile.write(f"\n {fileBlock} ---> {job}(\"{jobs[job]['name']}\")")
else:
needs = list(needs.split(','))
for need in needs:
markdownFile.write(f"\n {need}(\"{jobs[need]['name']}\") ---> {job}(\"{jobs[job]['name']}\")")
uses = jobs[job]['uses']
if uses != "":
uses = uses.split('/')[-1]
str = ","
inputs = str.join(jobs[job]['inputs'])
inputs = inputs.replace('\"','') #remove " (double quotes)
inputs = inputs.replace("\'",'') #remove ' (single quotes)
if len(inputs) != 0:
markdownFile.write(f"\n {job}(\"{jobs[job]['name']}\ninputs:[{inputs}]\") ---> {uses}(\"{workflowDetails[uses]['name']}[{uses}]\")")
else:
markdownFile.write(f"\n {job}(\"{jobs[job]['name']}\") ---> {uses}")
markdownFile.write('\n\n```\n\n')
markdownFile.write('## GitHub Actions Trigger Overview\n')
triggersList = set()
for file in workflowDetails:
for trigger in workflowDetails[file]['trigger']:
triggersList.add(trigger) #triggersList contains unique list of all types of triggers used in the workflow file
for trigger in sorted(triggersList):
markdownFile.write('\n```mermaid\n')
markdownFile.write('\nflowchart TB\n')
markdownFile.write(f'\n subgraph {trigger.upper()}')
for file in sortedDictionary:
if trigger in workflowDetails[file]['trigger']:
markdownFile.write(f'\n {trigger} ---> {file}[\"{workflowDetails[file]["name"]}[{file}]\"]')
markdownFile.write('\n end\n')
markdownFile.write('\n```\n')
- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: Actions-workflow-MarkdownFile
path: .github/workflows/ActionFlow.md