Class: GitlabQuality::TestTooling::GitlabClient::GitlabClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_quality/test_tooling/gitlab_client/gitlab_client.rb

Constant Summary collapse

RETRY_BACK_OFF_DELAY =
60
MAX_RETRY_ATTEMPTS =
3

Instance Method Summary collapse

Constructor Details

#initialize(token:, project:, endpoint: nil, **_kwargs) ⇒ GitlabClient

Returns a new instance of GitlabClient.



12
13
14
15
16
17
# File 'lib/gitlab_quality/test_tooling/gitlab_client/gitlab_client.rb', line 12

def initialize(token:, project:, endpoint: nil, **_kwargs)
  @token = token
  @project = project
  @retry_backoff = 0
  @endpoint = endpoint
end

Instance Method Details

#handle_gitlab_client_exceptionsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab_quality/test_tooling/gitlab_client/gitlab_client.rb', line 19

def handle_gitlab_client_exceptions
  yield
rescue Gitlab::Error::NotFound
  # This error could be raised in assert_user_permission!
  # If so, we want it to terminate at that point
  raise
rescue SystemCallError, OpenSSL::SSL::SSLError, Net::OpenTimeout, Net::ReadTimeout,
  Gitlab::Error::InternalServerError, Gitlab::Error::BadRequest, Gitlab::Error::ResponseError, Gitlab::Error::Parsing => e
  @retry_backoff += RETRY_BACK_OFF_DELAY

  raise if @retry_backoff > RETRY_BACK_OFF_DELAY * MAX_RETRY_ATTEMPTS

  warn("#{e.class.name} #{e.message}")
  warn("Sleeping for #{@retry_backoff} seconds before retrying...")
  sleep @retry_backoff

  retry
rescue StandardError => e
  post_exception_to_slack(e) if Runtime::Env.ci_commit_ref_name == Runtime::Env.default_branch

  raise e
end

#ignore_gitlab_client_exceptionsObject



64
65
66
67
68
69
# File 'lib/gitlab_quality/test_tooling/gitlab_client/gitlab_client.rb', line 64

def ignore_gitlab_client_exceptions
  yield
rescue StandardError, SystemCallError, OpenSSL::SSL::SSLError, Net::OpenTimeout, Net::ReadTimeout,
  Gitlab::Error::Error => e
  puts "Ignoring the following error: #{e}"
end

#post_exception_to_slack(error) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gitlab_quality/test_tooling/gitlab_client/gitlab_client.rb', line 42

def post_exception_to_slack(error)
  return unless ENV['CI_SLACK_WEBHOOK_URL']

  pipeline = Runtime::Env.pipeline_from_project_name
  channel = Runtime::Env.slack_alerts_channel

  slack_options = {
    slack_webhook_url: ENV.fetch('CI_SLACK_WEBHOOK_URL', nil),
    channel: channel,
    username: "GitLab Quality Test Tooling",
    icon_emoji: ':ci_failing:',
    message: <<~MSG
      Env: #{pipeline}. An unexpected error occurred while reporting test results in issues.
      The error occurred in job: #{Runtime::Env.ci_job_url}
      `#{error.class.name} #{error.message}`
    MSG
  }
  puts "Posting Slack message to channel: #{channel}"

  GitlabQuality::TestTooling::Slack::PostToSlack.new(**slack_options).invoke!
end