diff options
Diffstat (limited to 'lib/gitlab/ci/parsers/terraform/tfplan.rb')
-rw-r--r-- | lib/gitlab/ci/parsers/terraform/tfplan.rb | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/gitlab/ci/parsers/terraform/tfplan.rb b/lib/gitlab/ci/parsers/terraform/tfplan.rb new file mode 100644 index 00000000000..26a18c6603e --- /dev/null +++ b/lib/gitlab/ci/parsers/terraform/tfplan.rb @@ -0,0 +1,35 @@ +# 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:) + tfplan = Gitlab::Json.parse(json_data).tap do |parsed_data| + parsed_data['job_path'] = Gitlab::Routing.url_helpers.project_job_path( + artifact.job.project, artifact.job + ) + end + + raise TfplanParserError, 'Tfplan missing required key' unless valid_supported_keys?(tfplan) + + terraform_reports.add_plan(artifact.filename, tfplan) + rescue JSON::ParserError + raise TfplanParserError, 'JSON parsing failed' + rescue + raise TfplanParserError, 'Tfplan parsing failed' + end + + private + + def valid_supported_keys?(tfplan) + tfplan.keys == %w[create update delete job_path] + end + end + end + end + end +end |