Class: Makit::Gitlab::PipelineServiceImpl

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/gitlab/pipeline_service_impl.rb

Overview

Implementation of the PipelineService gRPC service Provides methods for parsing, validating, and converting GitLab CI pipelines

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.grpc_available?Boolean

Check if gRPC service is available

Returns:

  • (Boolean)


14
15
16
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 14

def self.grpc_available?
  defined?(Gitlab::Pipeline::PipelineService)
end

Instance Method Details

#compare_pipelines(request, _unused_call = nil) ⇒ Object

Compare two pipelines



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 365

def compare_pipelines(request, _unused_call = nil)
  pipeline1 = request.pipeline1
  pipeline2 = request.pipeline2
  
  differences = []
  added_jobs = []
  removed_jobs = []
  modified_jobs = []
  added_stages = []
  removed_stages = []
  
  # Compare jobs

  pipeline1_jobs = pipeline1.jobs.keys.to_set
  pipeline2_jobs = pipeline2.jobs.keys.to_set
  
  added_jobs = (pipeline2_jobs - pipeline1_jobs).to_a
  removed_jobs = (pipeline1_jobs - pipeline2_jobs).to_a
  
  common_jobs = pipeline1_jobs & pipeline2_jobs
  common_jobs.each do |job_name|
    job1 = pipeline1.jobs[job_name]
    job2 = pipeline2.jobs[job_name]
    unless jobs_equal?(job1, job2)
      modified_jobs << job_name
      differences << "Job '#{job_name}' has been modified"
    end
  end
  
  # Compare stages

  pipeline1_stages = pipeline1.stages.map(&:name).to_set
  pipeline2_stages = pipeline2.stages.map(&:name).to_set
  
  added_stages = (pipeline2_stages - pipeline1_stages).to_a
  removed_stages = (pipeline1_stages - pipeline2_stages).to_a
  
  if grpc_available?
    Gitlab::Pipeline::ComparePipelinesResponse.new(
      are_identical: differences.empty? && added_jobs.empty? && removed_jobs.empty? && added_stages.empty? && removed_stages.empty?,
      differences: differences,
      added_jobs: added_jobs,
      removed_jobs: removed_jobs,
      modified_jobs: modified_jobs,
      added_stages: added_stages,
      removed_stages: removed_stages
    )
  else
    {
      are_identical: differences.empty? && added_jobs.empty? && removed_jobs.empty? && added_stages.empty? && removed_stages.empty?,
      differences: differences,
      added_jobs: added_jobs,
      removed_jobs: removed_jobs,
      modified_jobs: modified_jobs,
      added_stages: added_stages,
      removed_stages: removed_stages
    }
  end
end

#execute_pipeline(request, _unused_call = nil) ⇒ Object

Execute a pipeline using Podman



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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 263

def execute_pipeline(request, _unused_call = nil)
  begin
    execution_id = SecureRandom.uuid
    start_time = Time.now
    
    # Create execution result

    result = create_execution_result(execution_id, start_time)
    
    # Check if Podman is available (cross-platform)

    podman_executable = if request.respond_to?(:podman_executable)
      request.podman_executable || "podman"
    else
      request[:podman_executable] || "podman"
    end
    
    # Find the actual executable path

    found_executable = find_executable(podman_executable)
    unless found_executable
      return create_error_response("Podman executable '#{podman_executable}' not found or not working")
    end
    
    podman_executable = found_executable
    
    # Get Podman version (cross-platform)

    podman_version = capture_command_output("#{podman_executable} --version")
    result.podman_version = podman_version
    result.execution_host = capture_command_output("hostname")
    
    # Set working directory

    working_dir = if request.respond_to?(:working_directory)
      request.working_directory || Dir.pwd
    else
      request[:working_directory] || Dir.pwd
    end
    
    # Execute pipeline stages

    pipeline = if request.respond_to?(:pipeline)
      request.pipeline
    else
      request[:pipeline]
    end
    stage_results = []
    
    pipeline.stages.each do |stage|
      stage_result = execute_stage(stage, pipeline, working_dir, request, execution_id, podman_executable)
      stage_results << stage_result
      result.job_results.concat(stage_result.job_results)
    end
    
    # Determine overall status

    result.status = determine_pipeline_status(result.job_results)
    result.finished_at = Time.now
    result.total_duration_seconds = (result.finished_at - start_time).to_i
    
    # Add execution logs

    result.logs.push("Pipeline execution started at #{start_time}")
    result.logs.push("Pipeline execution completed at #{result.finished_at}")
    result.logs.push("Total duration: #{result.total_duration_seconds} seconds")
    result.logs.push("Podman version: #{podman_version}")
    
    if grpc_available?
      Gitlab::Pipeline::ExecutePipelineResponse.new(
        result: result,
        success: result.status == Gitlab::Pipeline::PipelineStatus::PIPELINE_STATUS_SUCCESS,
        errors: result.errors,
        warnings: result.warnings
      )
    else
      {
        result: result,
        success: result.status == :success,
        errors: result.errors,
        warnings: result.warnings
      }
    end
    
  rescue StandardError => e
    error_result = create_execution_result(SecureRandom.uuid, Time.now)
    error_result.status = grpc_available? ? Gitlab::Pipeline::PipelineStatus::PIPELINE_STATUS_FAILED : :failed
    error_result.errors.push("Pipeline execution failed: #{e.message}")
    error_result.logs.push("Error: #{e.message}")
    error_result.logs.push("Backtrace: #{e.backtrace.join("\n")}")
    
    if grpc_available?
      Gitlab::Pipeline::ExecutePipelineResponse.new(
        result: error_result,
        success: false,
        errors: error_result.errors,
        warnings: error_result.warnings
      )
    else
      {
        result: error_result,
        success: false,
        errors: error_result.errors,
        warnings: error_result.warnings
      }
    end
  end
end

#get_pipeline_stats(request, _unused_call = nil) ⇒ Object

Get pipeline statistics



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
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 232

def get_pipeline_stats(request, _unused_call = nil)
  pipeline = request.pipeline
  
  total_artifacts = pipeline.jobs.values.sum { |job| job.artifacts_paths.length }
  total_services = pipeline.jobs.values.sum { |job| job.services.length }
  total_variables = pipeline.variables.length + pipeline.jobs.values.sum { |job| job.variables.length }
  
  if grpc_available?
    Gitlab::Pipeline::GetPipelineStatsResponse.new(
      total_jobs: pipeline.jobs.length,
      total_stages: pipeline.stages.length,
      total_artifacts: total_artifacts,
      total_services: total_services,
      total_variables: total_variables,
      stage_names: pipeline.stages.map(&:name),
      job_names: pipeline.jobs.keys
    )
  else
    {
      total_jobs: pipeline.jobs.length,
      total_stages: pipeline.stages.length,
      total_artifacts: total_artifacts,
      total_services: total_services,
      total_variables: total_variables,
      stage_names: pipeline.stages.map(&:name),
      job_names: pipeline.jobs.keys
    }
  end
end

#merge_pipelines(request, _unused_call = nil) ⇒ Object

Merge multiple pipelines



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
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 167

def merge_pipelines(request, _unused_call = nil)
  base = request.base_pipeline
  override = request.override_pipeline
  conflicts = []
  warnings = []
  
  # Create merged pipeline

  merged = create_empty_pipeline
  
  # Merge basic fields

  merged.image = override.image.empty? ? base.image : override.image
  merged.timeout = override.timeout == 0 ? base.timeout : override.timeout
  
  # Merge variables

  merged.variables.merge!(base.variables)
  override.variables.each do |key, value|
    if merged.variables.key?(key) && merged.variables[key] != value
      conflicts << "Variable '#{key}' has different values: '#{merged.variables[key]}' vs '#{value}'"
    end
    merged.variables[key] = value
  end
  
  # Merge cache

  if override.cache && !override.cache.key.empty?
    merged.cache = override.cache
  elsif base.cache && !base.cache.key.empty?
    merged.cache = base.cache
  end
  
  # Merge stages

  merged.stages.concat(base.stages)
  override.stages.each do |override_stage|
    existing_stage = merged.stages.find { |s| s.name == override_stage.name }
    if existing_stage
      existing_stage.jobs.concat(override_stage.jobs)
    else
      merged.stages << override_stage
    end
  end
  
  # Merge jobs

  merged.jobs.merge!(base.jobs)
  override.jobs.each do |job_name, job|
    if merged.jobs.key?(job_name)
      conflicts << "Job '#{job_name}' exists in both pipelines"
    end
    merged.jobs[job_name] = job
  end
  
  if grpc_available?
    Gitlab::Pipeline::MergePipelinesResponse.new(
      merged_pipeline: merged,
      conflicts: conflicts,
      warnings: warnings
    )
  else
    {
      merged_pipeline: merged,
      conflicts: conflicts,
      warnings: warnings
    }
  end
end

#parse_yaml(request, _unused_call = nil) ⇒ Object

Parse GitLab CI YAML content into Pipeline message



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
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 19

def parse_yaml(request, _unused_call = nil)
  begin
    # Parse YAML content

    yaml_data = YAML.safe_load(request.yaml_content, permitted_classes: [Symbol], aliases: true)
    
    # Convert to protobuf Pipeline message

    pipeline = convert_yaml_to_pipeline(yaml_data)
    
    # Validate the pipeline

    errors, warnings = validate_pipeline_data(yaml_data)
    
    if grpc_available?
      Gitlab::Pipeline::ParseYamlResponse.new(
        pipeline: pipeline,
        errors: errors,
        warnings: warnings,
        success: errors.empty?
      )
    else
      # Fallback response structure

      {
        pipeline: pipeline,
        errors: errors,
        warnings: warnings,
        success: errors.empty?
      }
    end
  rescue Psych::SyntaxError => e
    if grpc_available?
      Gitlab::Pipeline::ParseYamlResponse.new(
        pipeline: Gitlab::Pipeline::Pipeline.new,
        errors: ["YAML syntax error: #{e.message}"],
        warnings: [],
        success: false
      )
    else
      {
        pipeline: create_empty_pipeline,
        errors: ["YAML syntax error: #{e.message}"],
        warnings: [],
        success: false
      }
    end
  rescue StandardError => e
    if grpc_available?
      Gitlab::Pipeline::ParseYamlResponse.new(
        pipeline: Gitlab::Pipeline::Pipeline.new,
        errors: ["Parse error: #{e.message}"],
        warnings: [],
        success: false
      )
    else
      {
        pipeline: create_empty_pipeline,
        errors: ["Parse error: #{e.message}"],
        warnings: [],
        success: false
      }
    end
  end
end

#to_yaml(request, _unused_call = nil) ⇒ Object

Convert Pipeline message back to YAML



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
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 82

def to_yaml(request, _unused_call = nil)
  begin
    # Convert protobuf Pipeline to YAML

    yaml_content = convert_pipeline_to_yaml(request.pipeline, request.pretty_format, request.indent_size)
    
    if grpc_available?
      Gitlab::Pipeline::ToYamlResponse.new(
        yaml_content: yaml_content,
        errors: [],
        success: true
      )
    else
      {
        yaml_content: yaml_content,
        errors: [],
        success: true
      }
    end
  rescue StandardError => e
    if grpc_available?
      Gitlab::Pipeline::ToYamlResponse.new(
        yaml_content: "",
        errors: ["YAML generation error: #{e.message}"],
        success: false
      )
    else
      {
        yaml_content: "",
        errors: ["YAML generation error: #{e.message}"],
        success: false
      }
    end
  end
end

#validate_pipeline(request, _unused_call = nil) ⇒ Object

Validate pipeline structure



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
# File 'lib/makit/gitlab/pipeline_service_impl.rb', line 118

def validate_pipeline(request, _unused_call = nil)
  errors = []
  warnings = []
  suggestions = []
  
  pipeline = request.pipeline
  
  # Basic validation

  if pipeline.jobs.empty?
    warnings << "Pipeline has no jobs defined"
  end
  
  if pipeline.stages.empty?
    warnings << "Pipeline has no stages defined"
  end
  
  # Validate jobs

  pipeline.jobs.each do |job_name, job|
    job_errors, job_warnings, job_suggestions = validate_job(job_name, job, pipeline)
    errors.concat(job_errors)
    warnings.concat(job_warnings)
    suggestions.concat(job_suggestions)
  end
  
  # Validate stages

  pipeline.stages.each do |stage|
    stage_errors, stage_warnings = validate_stage(stage, pipeline)
    errors.concat(stage_errors)
    warnings.concat(stage_warnings)
  end
  
  if grpc_available?
    Gitlab::Pipeline::ValidatePipelineResponse.new(
      is_valid: errors.empty?,
      errors: errors,
      warnings: warnings,
      suggestions: suggestions
    )
  else
    {
      is_valid: errors.empty?,
      errors: errors,
      warnings: warnings,
      suggestions: suggestions
    }
  end
end