Module: Sonarqube::Client::QualityGates

Included in:
Sonarqube::Client
Defined in:
lib/sonarqube/client/quality_gates.rb

Overview

Defines methods related to tokens.

Constant Summary collapse

ALLOWED_OPERATORS =
%w[LT GT].freeze
ALLOWED_METRICS =
%w[
  coverage new_coverage duplicated_lines_density
  new_duplicated_lines_density reliability_rating new_reliability_rating
  maintainability_rating new_maintainability_rating
].freeze

Instance Method Summary collapse

Instance Method Details

#create_quality_gate(name) ⇒ Sonarqube::ObjectifiedHash Also known as: quality_gate_create

Create new quality gate.

Examples:

Sonarqube.create_quality_gate('name_quality_gate')

# Gracefully continue if gate already exists
begin
  Sonarqube.create_quality_gate('name_quality_gate')
rescue Sonarqube::Error::BadRequest => error
  raise unless error.message =~ /Name has already been taken/
end

Parameters:

  • name (String)

    (required) Quality Gate name.

Returns:

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/sonarqube/client/quality_gates.rb', line 27

def create_quality_gate(name)
  raise ArgumentError, 'Missing required parameters' if name.nil?

  body = { name: name }
  post('/api/qualitygates/create', body: body)
end

#create_quality_gate_condition(name, metric, error_threshold, operator = 'LT') ⇒ Sonarqube::ObjectifiedHash Also known as: quality_gate_condition_create

Create condition for a quality gate.

Examples:

# Create a condition that errors when coverage is below 90%
Sonarqube.create_quality_gate_condition('name_quality_gate', 'coverage', '90')

# Create a condition that errors when code duplication is > 3%
Sonarqube.create_quality_gate_condition('name_quality_gate', 'duplicated_lines_density',
                                        '3', 'GT')

Parameters:

  • name (String)

    (required) Quality Gate name.

  • metric (String)

    (required) SonarQube metric name.

  • error_threshold (String)

    (required) Error threshold value

  • operator (String) (defaults to: 'LT')

    (required) Operator - LT for <; GT for >

Returns:

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sonarqube/client/quality_gates.rb', line 50

def create_quality_gate_condition(name, metric, error_threshold, operator = 'LT')
  raise ArgumentError, 'Missing required parameters' if name.nil? || metric.nil? || error_threshold.nil?
  unless ALLOWED_OPERATORS.include?(operator)
    raise ArgumentError, "Operator must be in #{ALLOWED_OPERATORS.join(', ')}"
  end
  raise ArgumentError, "Metric must be in #{ALLOWED_METRICS.join(', ')}" unless ALLOWED_METRICS.include?(metric)

  body = {
    gateName: name,
    metric: metric,
    error: error_threshold,
    op: operator
  }
  post('/api/qualitygates/create_condition', body: body)
end

#list_quality_gatesSonarqube::ObjectifiedHash Also known as: quality_gates_list

List quality gates.

Examples:

Sonarqube.list_quality_gates

Returns:



90
91
92
# File 'lib/sonarqube/client/quality_gates.rb', line 90

def list_quality_gates
  get('/api/qualitygates/list')
end

#set_default_quality_gate(name) ⇒ Sonarqube::ObjectifiedHash Also known as: default_quality_gate_set

Set the default quality gate.

rubocop:disable Naming/AccessorMethodName

Examples:

Sonarqube.set_default_quality_gate('name_quality_gate')

Parameters:

  • name (String)

    (required) Quality Gate name.

Returns:

Raises:

  • (ArgumentError)


75
76
77
78
79
80
# File 'lib/sonarqube/client/quality_gates.rb', line 75

def set_default_quality_gate(name)
  raise ArgumentError, 'Missing required parameters' if name.nil?

  body = { name: name }
  post('/api/qualitygates/set_as_default', body: body)
end