Class: Makit::Gitlab::Pipeline

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

Overview

Ruby wrapper for GitLab Pipeline operations Provides a convenient interface for working with GitLab CI pipelines

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_content = nil) ⇒ Pipeline

Returns a new instance of Pipeline.



13
14
15
16
17
18
19
# File 'lib/makit/gitlab/pipeline.rb', line 13

def initialize(yaml_content = nil)
  if yaml_content
    @pipeline_data = parse_yaml(yaml_content)
  else
    @pipeline_data = create_empty_pipeline
  end
end

Instance Attribute Details

#pipeline_dataObject

Returns the value of attribute pipeline_data.



11
12
13
# File 'lib/makit/gitlab/pipeline.rb', line 11

def pipeline_data
  @pipeline_data
end

Class Method Details

.parse_file(file_path) ⇒ Object

Parse GitLab CI YAML from file



27
28
29
30
# File 'lib/makit/gitlab/pipeline.rb', line 27

def self.parse_file(file_path)
  content = File.read(file_path)
  new(content)
end

.parse_yaml(yaml_content) ⇒ Object

Parse GitLab CI YAML content



22
23
24
# File 'lib/makit/gitlab/pipeline.rb', line 22

def self.parse_yaml(yaml_content)
  new(yaml_content)
end

Instance Method Details

#add_job(name, job_data) ⇒ Object

Add a job to the pipeline



144
145
146
147
148
149
150
151
# File 'lib/makit/gitlab/pipeline.rb', line 144

def add_job(name, job_data)
  if @pipeline_data.jobs.key?(name)
    raise ArgumentError, "Job '#{name}' already exists"
  end

  job = convert_hash_to_job(job_data)
  @pipeline_data.jobs[name] = job
end

#add_stage(name) ⇒ Object

Add a stage to the pipeline



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/makit/gitlab/pipeline.rb', line 159

def add_stage(name)
  existing_stage = @pipeline_data.stages.find { |s| s.name == name }
  unless existing_stage
    if @grpc_available
      stage = Gitlab::Pipeline::Stage.new
      stage.name = name
      @pipeline_data.stages << stage
    else
      stage = Object.new
      def stage.name
        @name
      end
      def stage.name=(value)
        @name = value
      end
      stage.name = name
      @pipeline_data.stages << stage
    end
  end
end

#compare(other_pipeline) ⇒ Object

Compare with another pipeline



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/makit/gitlab/pipeline.rb', line 95

def compare(other_pipeline)
  if defined?(Gitlab::Pipeline::PipelineService) && @grpc_available
    # Use gRPC service if available
    service = PipelineServiceImpl.new
    request = Gitlab::Pipeline::ComparePipelinesRequest.new(
      pipeline1: @pipeline_data,
      pipeline2: other_pipeline.pipeline_data
    )
    response = service.compare_pipelines(request, nil)
    {
      are_identical: response.are_identical,
      differences: response.differences.to_a,
      added_jobs: response.added_jobs.to_a,
      removed_jobs: response.removed_jobs.to_a,
      modified_jobs: response.modified_jobs.to_a,
      added_stages: response.added_stages.to_a,
      removed_stages: response.removed_stages.to_a
    }
  else
    # Fallback implementation
    compare_pipelines_fallback(other_pipeline)
  end
end

#errorsObject

Get validation errors



204
205
206
207
# File 'lib/makit/gitlab/pipeline.rb', line 204

def errors
  validation_result = validate
  validation_result[:errors]
end

#execute_pipeline(variables: {}, working_directory: nil, podman_executable: "podman", dry_run: false) ⇒ Object

Execute pipeline using Podman



216
217
218
219
220
221
222
# File 'lib/makit/gitlab/pipeline.rb', line 216

def execute_pipeline(variables: {}, working_directory: nil, podman_executable: "podman", dry_run: false)
  if Makit::Gitlab::PipelineServiceImpl.grpc_available?
    execute_pipeline_grpc(variables, working_directory, podman_executable, dry_run)
  else
    execute_pipeline_fallback(variables, working_directory, podman_executable, dry_run)
  end
end

#job_namesObject

Get all job names



188
189
190
# File 'lib/makit/gitlab/pipeline.rb', line 188

def job_names
  @pipeline_data.jobs.keys
end

#merge(other_pipeline, preserve_base = true) ⇒ Object

Merge with another pipeline



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/makit/gitlab/pipeline.rb', line 120

def merge(other_pipeline, preserve_base = true)
  if defined?(Gitlab::Pipeline::PipelineService) && @grpc_available
    # Use gRPC service if available
    service = PipelineServiceImpl.new
    request = Gitlab::Pipeline::MergePipelinesRequest.new(
      base_pipeline: @pipeline_data,
      override_pipeline: other_pipeline.pipeline_data,
      preserve_base: preserve_base
    )
    response = service.merge_pipelines(request, nil)
    merged_pipeline = Pipeline.new
    merged_pipeline.pipeline_data = response.merged_pipeline
    {
      pipeline: merged_pipeline,
      conflicts: response.conflicts.to_a,
      warnings: response.warnings.to_a
    }
  else
    # Fallback implementation
    merge_pipelines_fallback(other_pipeline, preserve_base)
  end
end

#remove_job(name) ⇒ Object

Remove a job from the pipeline



154
155
156
# File 'lib/makit/gitlab/pipeline.rb', line 154

def remove_job(name)
  @pipeline_data.jobs.delete(name)
end

#remove_stage(name) ⇒ Object

Remove a stage from the pipeline



181
182
183
184
185
# File 'lib/makit/gitlab/pipeline.rb', line 181

def remove_stage(name)
  @pipeline_data.stages.reject! { |s| s.name == name }
  # Also remove jobs in this stage
  @pipeline_data.jobs.reject! { |_name, job| job.stage == name }
end

#stage_namesObject

Get all stage names



193
194
195
# File 'lib/makit/gitlab/pipeline.rb', line 193

def stage_names
  @pipeline_data.stages.map(&:name)
end

#statsObject

Get pipeline statistics



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/makit/gitlab/pipeline.rb', line 73

def stats
  if defined?(Gitlab::Pipeline::PipelineService) && @grpc_available
    # Use gRPC service if available
    service = PipelineServiceImpl.new
    request = Gitlab::Pipeline::GetPipelineStatsRequest.new(pipeline: @pipeline_data)
    response = service.get_pipeline_stats(request, nil)
    {
      total_jobs: response.total_jobs,
      total_stages: response.total_stages,
      total_artifacts: response.total_artifacts,
      total_services: response.total_services,
      total_variables: response.total_variables,
      stage_names: response.stage_names.to_a,
      job_names: response.job_names.to_a
    }
  else
    # Fallback implementation
    calculate_stats_fallback
  end
end

#to_yaml(pretty_format = true) ⇒ Object

Convert pipeline to YAML



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/makit/gitlab/pipeline.rb', line 33

def to_yaml(pretty_format = true)
  if defined?(Gitlab::Pipeline::PipelineService) && @grpc_available
    # Use gRPC service if available
    service = PipelineServiceImpl.new
    request = Gitlab::Pipeline::ToYamlRequest.new(
      pipeline: @pipeline_data,
      pretty_format: pretty_format,
      indent_size: 2
    )
    response = service.to_yaml(request, nil)
    response.yaml_content
  else
    # Fallback implementation
    convert_pipeline_to_yaml(@pipeline_data, pretty_format)
  end
end

#valid?Boolean

Check if pipeline is valid

Returns:

  • (Boolean)


198
199
200
201
# File 'lib/makit/gitlab/pipeline.rb', line 198

def valid?
  validation_result = validate
  validation_result[:is_valid]
end

#validateObject

Validate pipeline



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/makit/gitlab/pipeline.rb', line 51

def validate
  if defined?(Gitlab::Pipeline::PipelineService) && @grpc_available
    # Use gRPC service if available
    service = PipelineServiceImpl.new
    request = Gitlab::Pipeline::ValidatePipelineRequest.new(
      pipeline: @pipeline_data,
      check_gitlab_compatibility: true
    )
    response = service.validate_pipeline(request, nil)
    {
      is_valid: response.is_valid,
      errors: response.errors.to_a,
      warnings: response.warnings.to_a,
      suggestions: response.suggestions.to_a
    }
  else
    # Fallback implementation
    validate_pipeline_fallback
  end
end

#warningsObject

Get validation warnings



210
211
212
213
# File 'lib/makit/gitlab/pipeline.rb', line 210

def warnings
  validation_result = validate
  validation_result[:warnings]
end