Class: Fastlane::Actions::EnCreateSonarReportsAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb

Class Method Summary collapse

Class Method Details

.authorsObject



204
205
206
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 204

def self.authors
  ["Nicolae Ghimbovschi"]
end

.available_optionsObject



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 135

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :include_path,
                                 env_name: "FL_EN_ANALYSIS_INCLUDE_SRC",
                                 description: 'One or multiple paths comma separated to include into scan report',
                                 optional: false),

    FastlaneCore::ConfigItem.new(key: :exclude_path,
                                 env_name: "FL_EN_ANALYSIS_EXCLUDE_SRC",
                                 description: 'One or multiple paths comma separated to include into scan report',
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :skip_oclint_analysis,
                                 env_name: "FL_EN_SKIP_OCLINT_ANALYSIS",
                                 description: "Skip running oclint code analysis",
                                 is_string: false,
                                 default_value: false,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :skip_swiftlint_analysis,
                                 env_name: "FL_EN_SKIP_SWIFTLINT_ANALYSIS",
                                 description: "Skip running swiftlint code analysis",
                                 is_string: false,
                                 default_value: false,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :swiftlint_config_path,
                                 env_name: "FL_EN_SWIFTLINT_CONFIG_PATH",
                                 description: 'Path to .swiftlint.yml',
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :swiftlint_extra_args,
                                 env_name: "FL_EN_SWIFTLINT_EXTRA ARGS",
                                 description: 'Extra command line args for swiftlint',
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :skip_lizard_analysis,
                                 env_name: "FL_EN_SKIP_LIZARD_ANALYSIS",
                                 description: "Skip running lizard code analysis",
                                 is_string: false,
                                 default_value: false,
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :lizard_extra_args,
                                 env_name: "FL_EN_LIZARD_EXTRA ARGS",
                                 description: 'Extra command line args for lizard',
                                 optional: true),

    FastlaneCore::ConfigItem.new(key: :lizard_use_relative_paths,
                                 env_name: "FL_EN_LIZARD_USE_RELATIVE_PATHS",
                                 description: "Use relative paths in lizard reports",
                                 is_string: false,
                                 default_value: true,
                                 optional: true)
  ]
end

.descriptionObject



192
193
194
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 192

def self.description
  "Creates swiftlint, lizard and oclint reports and adjusts junit reports for sonarqube (with the open source swift/objc plugin)"
end

.detailsObject



196
197
198
199
200
201
202
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 196

def self.details
  "Ensure before calling this action ensure en_ci_utils_init_action, scan and slather were executed.
This action re-uses values for used for scan and env variables set by en_ci_utils_init_action for reports paths.
The oclint command is executed throug the oclint action. You can change input for that action through oclint action env variables.

The exclude files option doesn't work for swiftlint and oclint. You can exclude files in sonar properties or for swiftlint with .swiftlint.yml"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


208
209
210
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 208

def self.is_supported?(platform)
  platform == :ios
end

.lizard_path_args(params) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 73

def self.lizard_path_args(params)
  include_files = ""
  use_relative_paths = params[:lizard_use_relative_paths]

  if params[:include_path]
    self.normalize_paths(params[:include_path], use_relative_paths).each do |path|
      include_files += " '#{path}' " 
    end
  end

  exclude_files = ""
  if params[:exclude_path]
    self.normalize_paths(params[:exclude_path]).each do |path|
      exclude_files += " -x '#{path}/*' "
    end
  end

  include_files + exclude_files
end

.normalize_paths(paths, use_relative = false) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 63

def self.normalize_paths(paths, use_relative = false)
  paths.split(",").map do |path|
    unless use_relative
      File.expand_path(path.strip)
    else
      path.strip
    end
  end
end

.run(params) ⇒ Object



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
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
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 4

def self.run(params)
  build_path = ENV['ENCI_OUTPUT_PATH']
  reports_path = ENV['ENCI_REPORTS_PATH']
  logs_path = ENV['ENCI_LOGS_PATH']
  scan_logs_path = ENV['SCAN_BUILDLOG_PATH']
  scan_reports_path = ENV['SCAN_OUTPUT_DIRECTORY']

  lizard_output_path = ENV['FL_LIZARD_OUTPUT']
  swiftlint_output_path = ENV['FL_SWIFTLINT_OUTPUT']

  lizard_extra_args = params[:lizard_extra_args]
  swiftlint_extra_args = params[:swiftlint_extra_args]
  swiftlint_config_arg = swiftlint_config_path_arg(params)

  swiftlint_path_args = self.swiftlint_path_args(params)
  lizard_path_args = self.lizard_path_args(params)

  unless File.exist?(scan_reports_path)
    UI.user_error!("Please run scan and slather first.")
    return
  end

  FileUtils::mkdir_p(reports_path) unless File.exists?(reports_path)

  # create the junit reports again in such way that can be used by sonar
  sh("cat '#{scan_logs_path}'/*.log | bundle exec ocunit2junit > /dev/null 2>&1 || exit 0")

  # ocunit2junit creates the reports in local test-reports, it needs to be moved
  FileUtils::rm_rf("#{scan_reports_path}/test-reports")
  FileUtils::mv("test-reports", "#{scan_reports_path}")

  if Dir.glob("#{scan_reports_path}/test-reports/*.xml").empty?
    # ocunit2junit failed?
    UI.message("ocunit2junit failed, copying the original junit report to test-reports")
    FileUtils::cp("#{scan_reports_path}/report.junit", "#{scan_reports_path}/test-reports/report.xml")
  end

  # run static code analysis
  unless params[:skip_swiftlint_analysis]
    sh("swiftlint lint #{swiftlint_path_args} #{swiftlint_config_arg} #{swiftlint_extra_args} --quiet > '#{swiftlint_output_path}' || exit 0")
  end

  unless params[:skip_lizard_analysis]
    sh("lizard #{lizard_path_args} -l swift -l objectivec --xml #{lizard_extra_args} > '#{lizard_output_path}' || exit 0")
  end

  # workaround sonar plugin, it is not picking correctly the path to lizard report
  # and it is looking at the default path
  FileUtils::ln_sf(reports_path, "sonar-reports")

  unless params[:skip_oclint_analysis]
    scan_json_compilation_database = File.expand_path(scan_reports_path) + '/report.json-compilation-database'
    json_compilation_database = File.expand_path(scan_reports_path) + '/compile_commands.json'
    FileUtils::cp("#{scan_reports_path}/report.json-compilation-database", json_compilation_database)
    other_action.oclint(compile_commands: scan_json_compilation_database)
    FileUtils::mv("./fastlane/oclint_report.pmd", "#{reports_path}/oclint.xml")
  end
end

.swiftlint_config_path_arg(params) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 93

def self.swiftlint_config_path_arg(params)
  file_path = params[:swiftlint_config_path]
  config_path_arg = ""

  if File.exist?('.swiftlint.yml')
    config_path_arg = File.expand_path('.swiftlint.yml')
  end

  if file_path != nil && !file_path.strip.empty?
    config_path_arg = File.expand_path(file_path.strip)
    if !File.exist?(config_path_arg)
      config_path_arg = ""
    end
  end

  if !config_path_arg.strip.empty?
    return "--config '#{config_path_arg}'"
  else
    return ""
  end
end

.swiftlint_path_args(params) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/fastlane/plugin/ciutils/actions/en_create_sonar_reports.rb', line 115

def self.swiftlint_path_args(params)
  include_files = ""

  if params[:include_path]
    self.normalize_paths(params[:include_path]).each do |path|
      include_files += " --path '#{path}'"
    end
  end

  # exclude_files = ""
  # if params[:exclude_path]
  #   self.normalize_paths(params[:exclude_path]).each do |path|
  #     exclude_files += " --force-exclude '#{path}' "
  #   end
  # end

  # include_files + exclude_files
  include_files
end