Class: Fastlane::Actions::SwiftlintCodequalityAction

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

Defined Under Namespace

Classes: Severity

Class Method Summary collapse

Class Method Details

.authorsObject



91
92
93
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 91

def self.authors
  ["madsbogeskov"]
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 99

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :path,
                            env_name: "SWIFTLINT_CODEQUALITY_PATH",
                         description: "The path to the SwiftLint results file",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :output,
                            env_name: "SWIFTLINT_CODEQUALITY_OUTPUT",
                         description: "The path to the generated output report",
                            optional: false,
                                type: String),
    FastlaneCore::ConfigItem.new(key: :prefix,
                            env_name: "SWIFTLINT_CODEQUALITY_PREFIX_PATH",
                         description: "Used to prefix the path of a file. Usefull in e.g. React Native projects where the iOS project is in a subfolder",
                            optional: true,
                       default_value: '',
                                type: String),
    FastlaneCore::ConfigItem.new(key: :fail_build_conditions,
                            env_name: "SWIFTLINT_CODEQUALITY_FAIL_BUILD_CONDITIONS",
                         description: "A hash with severities and their limits, that if exceeded should result in an exception. Supported severities: critical, minor and info.",
                           is_string: false,
                       default_value: {},
                            optional: true)
  ]
end

.descriptionObject



87
88
89
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 87

def self.description
  "Converts SwiftLint reports into GitLab support CodeQuality reports"
end

.detailsObject



95
96
97
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 95

def self.details
  "Converts SwiftLint reports into GitLab support CodeQuality reports"
end

.handle_result(result, fail_build_conditions) ⇒ Object



58
59
60
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
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 58

def self.handle_result(result, fail_build_conditions)
  critical_limit = fail_build_conditions.fetch(Severity::CRITICAL.to_sym, 0)
  minor_limit = fail_build_conditions.fetch(Severity::MINOR.to_sym, 0)
  info_limit = fail_build_conditions.fetch(Severity::INFO.to_sym, 0)

  critical_count = result.select { |issue| issue[:severity] == Severity::CRITICAL }.length
  minor_count = result.select { |issue| issue[:severity] == Severity::MINOR }.length
  info_count = result.select { |issue| issue[:severity] == Severity::INFO }.length

  UI.important("")
  violations = false
  if critical_count > critical_limit
    UI.important("Critical issue limit (#{critical_limit}) exceeded: #{critical_count}")
    violations = true
  end
  if minor_count > minor_limit
    UI.important("Minor issue limit (#{minor_limit}) exceeded: #{minor_count}")
    violations = true
  end
  if info_count > info_limit
    UI.important("Info issue limit (#{info_limit}) exceeded: #{info_count}")
    violations = true
  end

  UI.important("")

  UI.user_error!("Severity limits where exceeded.") if violations
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 126

def self.is_supported?(platform)
  true
end

.line_to_code_climate_object(line, prefix, pwd) ⇒ Object



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
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 26

def self.line_to_code_climate_object(line, prefix, pwd)
  filename, start, reason = line.match(/(.*\.swift):(\d+):\d+:\s*(.*)/).captures

  # example: error: Type Name Violation: Type name should only contain alphanumeric characters: 'FILE' (type_name)

  issue_type, failure_type, description, _rule = reason.match(/(.*?):?\s(.*?):\s(.*)\((.*)\)/).captures

  case issue_type
  when 'error'
    severity = Severity::CRITICAL
  when 'warning'
    severity = Severity::MINOR
  else
    severity = Severity::INFO
  end

  {
    type: "issue",
    check_name: failure_type.strip,
    description: description.strip,
    fingerprint: Digest::MD5.hexdigest(line),
    severity: severity,
    location: {
      path: prefix + filename.sub(pwd, ''),
      lines: {
        begin: start.to_i,
        end: start.to_i
      }
    }
  }
end

.run(params) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fastlane/plugin/swiftlint_codequality/actions/swiftlint_codequality_action.rb', line 7

def self.run(params)
  UI.message("Parsing SwiftLint report at #{params[:path]}")

  pwd = Fastlane::Actions.sh("pwd", log: false).strip

  report = File.open(params[:path])
  result = report
           .each
           .select { |l| l.include?("Warning Threshold Violation") == false }
           .map { |line| self.line_to_code_climate_object(line, params[:prefix], pwd) }
           .to_a

  IO.write(params[:output], result.to_json)

  UI.success("🚀 Generated Code Quality report at #{params[:output]} 🚀")

  handle_result(result, params[:fail_build_conditions])
end