Class: Fastlane::Actions::SonarAction

Inherits:
Fastlane::Action show all
Defined in:
fastlane/lib/fastlane/actions/sonar.rb

Constant Summary

Constants inherited from Fastlane::Action

Fastlane::Action::AVAILABLE_CATEGORIES, Fastlane::Action::RETURN_TYPES

Documentation collapse

Class Method Summary collapse

Methods inherited from Fastlane::Action

action_name, author, deprecated_notes, lane_context, method_missing, other_action, output, return_type, sample_return_value, shell_out_should_use_bundle_exec?, step_text

Class Method Details

.authorsObject



146
147
148
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 146

def self.authors
  ["c_gretzki"]
end

.available_optionsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 61

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :project_configuration_path,
                                 env_name: "FL_SONAR_RUNNER_PROPERTIES_PATH",
                                 description: "The path to your sonar project configuration file; defaults to `sonar-project.properties`", # default is enforced by sonar-scanner binary
                                 optional: true,
                                 verify_block: proc do |value|
                                   UI.user_error!("Couldn't find file at path '#{value}'") unless value.nil? || File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :project_key,
                                 env_name: "FL_SONAR_RUNNER_PROJECT_KEY",
                                 description: "The key sonar uses to identify the project, e.g. `name.gretzki.awesomeApp`. Must either be specified here or inside the sonar project configuration file",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :project_name,
                                 env_name: "FL_SONAR_RUNNER_PROJECT_NAME",
                                 description: "The name of the project that gets displayed on the sonar report page. Must either be specified here or inside the sonar project configuration file",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :project_version,
                                 env_name: "FL_SONAR_RUNNER_PROJECT_VERSION",
                                 description: "The project's version that gets displayed on the sonar report page. Must either be specified here or inside the sonar project configuration file",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :sources_path,
                                 env_name: "FL_SONAR_RUNNER_SOURCES_PATH",
                                 description: "Comma-separated paths to directories containing source files. Must either be specified here or inside the sonar project configuration file",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :exclusions,
                                 env_name: "FL_SONAR_RUNNER_EXCLUSIONS",
                                 description: "Comma-separated paths to directories to be excluded from the analysis",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :project_language,
                                 env_name: "FL_SONAR_RUNNER_PROJECT_LANGUAGE",
                                 description: "Language key, e.g. objc",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :source_encoding,
                                 env_name: "FL_SONAR_RUNNER_SOURCE_ENCODING",
                                 description: "Used encoding of source files, e.g., UTF-8",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :sonar_runner_args,
                                 env_name: "FL_SONAR_RUNNER_ARGS",
                                 description: "Pass additional arguments to sonar-scanner. Be sure to provide the arguments with a leading `-D` e.g. FL_SONAR_RUNNER_ARGS=\"-Dsonar.verbose=true\"",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :sonar_login,
                                 env_name: "FL_SONAR_LOGIN",
                                 description: "Pass the Sonar Login Token (e.g: xxxxxxprivate_token_XXXXbXX7e)",
                                 deprecated: "Login and password were deprecated in favor of login token. See https://community.sonarsource.com/t/deprecating-sonar-login-and-sonar-password-in-favor-of-sonar-token/95829 for more details",
                                 optional: true,
                                 sensitive: true,
                                 conflicting_options: [:sonar_token]),
    FastlaneCore::ConfigItem.new(key: :sonar_token,
                                 env_name: "FL_SONAR_TOKEN",
                                 description: "Pass the Sonar Token (e.g: xxxxxxprivate_token_XXXXbXX7e)",
                                 optional: true,
                                 sensitive: true,
                                 conflicting_options: [:sonar_login]),
    FastlaneCore::ConfigItem.new(key: :sonar_url,
                                 env_name: "FL_SONAR_URL",
                                 description: "Pass the url of the Sonar server",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :sonar_organization,
                                 env_name: "FL_SONAR_ORGANIZATION",
                                 description: "Key of the organization on SonarCloud",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :branch_name,
                                 env_name: "FL_SONAR_RUNNER_BRANCH_NAME",
                                 description: "Pass the branch name which is getting scanned",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :pull_request_branch,
                                 env_name: "FL_SONAR_RUNNER_PULL_REQUEST_BRANCH",
                                 description: "The name of the branch that contains the changes to be merged",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :pull_request_base,
                                 env_name: "FL_SONAR_RUNNER_PULL_REQUEST_BASE",
                                 description: "The long-lived branch into which the PR will be merged",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :pull_request_key,
                                 env_name: "FL_SONAR_RUNNER_PULL_REQUEST_KEY",
                                 description: "Unique identifier of your PR. Must correspond to the key of the PR in GitHub or TFS",
                                 optional: true)
  ]
end

.categoryObject



174
175
176
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 174

def self.category
  :testing
end

.descriptionObject



50
51
52
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 50

def self.description
  "Invokes sonar-scanner to programmatically run SonarQube analysis"
end

.detailsObject



54
55
56
57
58
59
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 54

def self.details
  [
    "See [http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner](http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner) for details.",
    "It can process unit test results if formatted as junit report as shown in [xctest](https://docs.fastlane.tools/actions/xctest/) action. It can also integrate coverage reports in Cobertura format, which can be transformed into by the [slather](https://docs.fastlane.tools/actions/slather/) action."
  ].join("\n")
end

.example_codeObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 154

def self.example_code
  [
    'sonar(
      project_key: "name.gretzki.awesomeApp",
      project_version: "1.0",
      project_name: "iOS - AwesomeApp",
      sources_path: File.expand_path("../AwesomeApp")
    )',
    'sonar(
      project_key: "name.gretzki.awesomeApp",
      project_version: "1.0",
      project_name: "iOS - AwesomeApp",
      sources_path: File.expand_path("../AwesomeApp"),
      sonar_organization: "myOrg",
      sonar_token: "123456abcdef",
      sonar_url: "https://sonarcloud.io"
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:



150
151
152
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 150

def self.is_supported?(platform)
  true
end

.return_valueObject



142
143
144
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 142

def self.return_value
  "The exit code of the sonar-scanner binary"
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
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 4

def self.run(params)
  verify_sonar_scanner_binary

  command_prefix = [
    'cd',
    File.expand_path('.').shellescape,
    '&&'
  ].join(' ')

  sonar_scanner_args = []
  sonar_scanner_args << "-Dproject.settings=\"#{params[:project_configuration_path]}\"" if params[:project_configuration_path]
  sonar_scanner_args << "-Dsonar.projectKey=\"#{params[:project_key]}\"" if params[:project_key]
  sonar_scanner_args << "-Dsonar.projectName=\"#{params[:project_name]}\"" if params[:project_name]
  sonar_scanner_args << "-Dsonar.projectVersion=\"#{params[:project_version]}\"" if params[:project_version]
  sonar_scanner_args << "-Dsonar.sources=\"#{params[:sources_path]}\"" if params[:sources_path]
  sonar_scanner_args << "-Dsonar.exclusions=\"#{params[:exclusions]}\"" if params[:exclusions]
  sonar_scanner_args << "-Dsonar.language=\"#{params[:project_language]}\"" if params[:project_language]
  sonar_scanner_args << "-Dsonar.sourceEncoding=\"#{params[:source_encoding]}\"" if params[:source_encoding]
  sonar_scanner_args << "-Dsonar.login=\"#{params[:sonar_login]}\"" if params[:sonar_login]
  sonar_scanner_args << "-Dsonar.token=\"#{params[:sonar_token]}\"" if params[:sonar_token]
  sonar_scanner_args << "-Dsonar.host.url=\"#{params[:sonar_url]}\"" if params[:sonar_url]
  sonar_scanner_args << "-Dsonar.organization=\"#{params[:sonar_organization]}\"" if params[:sonar_organization]
  sonar_scanner_args << "-Dsonar.branch.name=\"#{params[:branch_name]}\"" if params[:branch_name]
  sonar_scanner_args << "-Dsonar.pullrequest.branch=\"#{params[:pull_request_branch]}\"" if params[:pull_request_branch]
  sonar_scanner_args << "-Dsonar.pullrequest.base=\"#{params[:pull_request_base]}\"" if params[:pull_request_base]
  sonar_scanner_args << "-Dsonar.pullrequest.key=\"#{params[:pull_request_key]}\"" if params[:pull_request_key]

  sonar_scanner_args << params[:sonar_runner_args] if params[:sonar_runner_args]

  command = [
    command_prefix,
    'sonar-scanner',
    sonar_scanner_args
  ].join(' ')
  # hide command, as it may contain credentials
  Fastlane::Actions.sh_control_output(command, print_command: false, print_command_output: true)
end

.verify_sonar_scanner_binaryObject



42
43
44
# File 'fastlane/lib/fastlane/actions/sonar.rb', line 42

def self.verify_sonar_scanner_binary
  UI.user_error!("You have to install sonar-scanner using `brew install sonar-scanner`") unless `which sonar-scanner`.to_s.length > 0
end