summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/parsers/terraform/tfplan.rb
blob: 26a18c6603ec525a6c2bffdf416bb5c7e85f9561 (plain)
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
# 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