-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.rb
303 lines (235 loc) · 7.03 KB
/
model.rb
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
# TODO map methods to yaml attributes using method_missing
require 'yaml'
class String
def parameterize
return self.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
end
end
class Pipeline
attr_reader :stages, :jobs
def initialize(yaml)
@yaml = yaml
@stages = @yaml['stages'].map { |stage| Stage.new(stage, self) }
@jobs = @stages.flat_map { |stage| stage.jobs }.reject { |job| job.to_s.empty? }
end
def stage(internal_id)
stage = @stages.find { |stage| stage.internal_id == internal_id }
raise "Stage not found: #{internal_id}" unless stage
stage
end
end
class JobFactory
def self.create_job(yaml, parent)
return DeploymentJob.new(yaml, parent) if yaml['deployment']
return Job.new(yaml, parent) if yaml['job']
raise "Unexpected job type defined by: #{yaml.inspect}"
end
end
class Base
attr_reader :internal_id, :id, :display_name, :parent, :children
def initialize(yaml, parent)
@yaml = yaml
@display_name = @yaml['displayName']
@parent = parent
end
def name
@display_name || @internal_id
end
end
class Stage < Base
attr_reader :jobs
def initialize(yaml, parent)
super(yaml, parent)
@internal_id = yaml['stage']
@id = "stage_#{@internal_id}".parameterize
if yaml['jobs']
@jobs = yaml['jobs'].map { |job| JobFactory.create_job(job, self) }
end
end
def previous_stage
return nil if first_stage?
@parent.stages[my_index - 1]
end
def next_stage
return nil if last_stage?
@parent.stages[my_index + 1]
end
def siblings
stages_from_parent = @parent.stages.dup
stages_from_parent.delete_at(my_index)
stages_from_parent
end
def siblings_and_me
@parent.stages
end
def dependency_level
return 0 if depends_on.empty?
return 1 + depends_on.map { |dependency| dependency.dependency_level }.max
end
def my_index_at_my_level
my_dependency_level = dependency_level
my_id = id
siblings_and_me.select { |sibling| sibling.dependency_level == my_dependency_level }.index { |item| item.id == my_id }
end
def my_index
@my_index ||= @parent.stages.find_index { |stage| stage.name == name }
end
def last_stage?
my_index == @parent.stages.size - 1
end
def first_stage?
my_index == 0
end
def job(internal_id)
job = @jobs.find { |job| job.internal_id == internal_id }
raise "Job not found: #{internal_id}" unless job
job
end
def data
{
type: 'Stage',
id: id,
internal_id: internal_id,
name: name,
children_type: 'Jobs',
children: jobs.map { |job| job.name },
attributes: attributes,
attributes_as_yaml: attributes_as_yaml,
show_children: true
}
end
def attributes
@yaml.reject { |key| ['jobs', 'stage', 'displayName'].include?(key) }.map { |key, value| { name: key, value: value } }.compact
end
def attributes_as_yaml
@yaml.reject { |key| ['jobs', 'stage', 'displayName'].include?(key) }.compact.to_yaml.sub("--- {}\n", '').sub("---\n", '')
end
def depends_on
if has_explicit_dependency?
return [@parent.stage(@yaml['dependsOn'])] if @yaml['dependsOn'].is_a? String
return @yaml['dependsOn'].map { |dependency| @parent.stage(dependency) } if @yaml['dependsOn'].is_a? Array
raise "Unexpected type for dependsOn: #{@yaml['dependsOn'].class.name}"
end
unless has_no_dependency?
return [previous_stage] if previous_stage
end
[]
end
def edge_data
depends_on.map { |dependency| {
data: {
id: "stage_#{id}_depends_on_#{dependency.id}",
source: dependency.id,
target: id
}
} }
end
def has_jobs?
@jobs && [email protected]?
end
# When you define multiple stages in a pipeline, by default, they run sequentially in the order in which you define them in the YAML file:
# https://docs.microsoft.com/en-us/azure/devops/pipelines/process/stages?view=azure-devops&tabs=yaml#specify-dependencies
# if dependsOn is [], then the stage has no dependency and it will run in parallel with the first one
# if dependsOn is not specified then the stage will run sequentially
# if two stages depends on the same stage, they will run in parallel
def has_explicit_dependency?
@yaml['dependsOn'] && !@yaml['dependsOn'].empty?
end
def has_no_dependency?
@yaml['dependsOn'] && @yaml['dependsOn'].is_a?(Array) && @yaml['dependsOn'].empty?
end
def has_dependency?
!has_no_dependency?
end
end
class Job < Base
attr_reader :steps
def initialize(yaml, parent, id_key='job')
super(yaml, parent)
@internal_id = yaml[id_key]
@id = "job_#{parent.id}_#{@internal_id}".parameterize
if id_key == 'job'
if yaml['steps']
@steps = yaml['steps']
end
elsif id_key == 'deployment'
# FIXME: there are other strategies
if yaml['strategy']['runOnce']['deploy']['steps']
@steps = yaml['strategy']['runOnce']['deploy']['steps']
end
end
end
def data
{
type: 'Job',
id: id,
internal_id: internal_id,
name: name,
parent: parent.id,
children_type: 'Steps',
children: [], # TODO: continue this: steps.map { |step| step.name },
attributes: attributes,
attributes_as_yaml: attributes_as_yaml,
show_children: true
}
end
def attributes
@yaml.reject { |key| ['steps', 'job', 'strategy', 'deployment', 'displayName'].include?(key) }.map { |key, value| { name: key, value: value } }.compact
end
def attributes_as_yaml
@yaml.reject { |key| ['steps', 'job', 'strategy', 'deployment', 'displayName'].include?(key) }.compact.to_yaml.sub("--- {}\n", '').sub("---\n", '')
end
def has_dependency?
@yaml['dependsOn'] && !@yaml['dependsOn'].empty?
end
def depends_on
if has_dependency?
return [@parent.job(@yaml['dependsOn'])] if @yaml['dependsOn'].is_a? String
return @yaml['dependsOn'].map { |dependency| @parent.job(dependency) } if @yaml['dependsOn'].is_a? Array
raise "Unexpected type for dependsOn: #{@yaml['dependsOn'].class.name}"
end
[]
end
def edge_data
depends_on.map { |dependency| {
data: {
id: "job_#{id}_depends_on_#{dependency.id}",
source: dependency.id,
target: id
}
} }
end
# XXX: duplicated from Stage
def siblings
jobs_from_parent = @parent.jobs.dup
jobs_from_parent.delete_at(my_index)
jobs_from_parent
end
def siblings_and_me
@parent.jobs
end
def dependency_level
return 0 if depends_on.empty?
return 1 + depends_on.map { |dependency| dependency.dependency_level }.max
end
def my_index_at_my_level
my_dependency_level = dependency_level
my_id = id
siblings_and_me.select { |sibling| sibling.dependency_level == my_dependency_level }.index { |item| item.id == my_id }
end
def item_size_for_drawing_purposes
(5 * name.size) + 30
end
end
class DeploymentJob < Job
def initialize(yaml, parent)
super(yaml, parent, 'deployment')
end
end
class Step
# TODO
end
def to_model(raw)
yaml = YAML.load(raw)
return Pipeline.new(yaml)
end