debian-mirror-gitlab/lib/gitlab/ci/parsers/terraform/tfplan.rb

58 lines
1.8 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
module Parsers
module Terraform
class Tfplan
TfplanParserError = Class.new(Gitlab::Ci::Parsers::ParserError)
def parse!(json_data, terraform_reports, artifact:)
2020-07-28 23:09:34 +05:30
job_details = job_details(artifact.job)
job_id = job_details['job_id']
2020-06-23 00:09:42 +05:30
plan_data = Gitlab::Json.parse(json_data)
2020-05-24 23:13:21 +05:30
2020-07-28 23:09:34 +05:30
if has_required_keys?(plan_data)
terraform_reports.add_plan(job_id, valid_tfplan(plan_data, job_details))
else
terraform_reports.add_plan(job_id, invalid_tfplan(:missing_json_keys, job_details))
end
2020-05-24 23:13:21 +05:30
rescue JSON::ParserError
2020-07-28 23:09:34 +05:30
terraform_reports.add_plan(job_id, invalid_tfplan(:invalid_json_format, job_details))
2021-06-08 01:23:25 +05:30
rescue StandardError
2020-07-28 23:09:34 +05:30
details = job_details || {}
plan_name = job_id || 'failed_tf_plan'
terraform_reports.add_plan(plan_name, invalid_tfplan(:unknown_error, details))
2020-05-24 23:13:21 +05:30
end
private
2020-06-23 00:09:42 +05:30
def has_required_keys?(plan_data)
(%w[create update delete] - plan_data.keys).empty?
end
2020-07-28 23:09:34 +05:30
def job_details(job)
2020-06-23 00:09:42 +05:30
{
2020-07-28 23:09:34 +05:30
'job_id' => job.id.to_s,
'job_name' => job.options.dig(:artifacts, :name).to_s,
'job_path' => Gitlab::Routing.url_helpers.project_job_path(job.project, job)
}
end
def invalid_tfplan(error_type, job_details)
job_details.merge('tf_report_error' => error_type)
end
def valid_tfplan(plan_data, job_details)
job_details.merge(
2020-06-23 00:09:42 +05:30
'create' => plan_data['create'].to_i,
'delete' => plan_data['delete'].to_i,
'update' => plan_data['update'].to_i
2020-07-28 23:09:34 +05:30
)
2020-05-24 23:13:21 +05:30
end
end
end
end
end
end