Class: Gitlab::QA::Report::GitlabIssueClient

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/qa/report/gitlab_issue_client.rb

Overview

The GitLab client is used for API access: github.com/NARKOZ/gitlab

Constant Summary collapse

MAINTAINER_ACCESS_LEVEL =
40
RETRY_BACK_OFF_DELAY =
60
MAX_RETRY_ATTEMPTS =
3

Instance Method Summary collapse

Constructor Details

#initialize(token:, project:) ⇒ GitlabIssueClient



29
30
31
32
33
34
35
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 29

def initialize(token:, project:)
  @token = token
  @project = project
  @retry_backoff = 0

  configure_gitlab_client
end

Instance Method Details

#assert_user_permission!Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 37

def assert_user_permission!
  handle_gitlab_client_exceptions do
    user = Gitlab.user
    member = Gitlab.team_member(project, user.id)

    abort_not_permitted if member.access_level < MAINTAINER_ACCESS_LEVEL
  end
rescue Gitlab::Error::NotFound
  abort_not_permitted
end

#create_issue(title:, description:, labels:) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 60

def create_issue(title:, description:, labels:)
  puts "Creating issue..."

  handle_gitlab_client_exceptions do
    Gitlab.create_issue(
      project,
      title,
      { description: description, labels: labels }
    )
  end
end

#edit_issue(iid:, options: {}) ⇒ Object



72
73
74
75
76
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 72

def edit_issue(iid:, options: {})
  handle_gitlab_client_exceptions do
    Gitlab.edit_issue(project, iid, options)
  end
end

#find_issues(iid:, options: {}, &select) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 48

def find_issues(iid:, options: {}, &select)
  select ||= :itself

  handle_gitlab_client_exceptions do
    return [Gitlab.issue(project, iid)] if iid

    Gitlab.issues(project, options)
      .auto_paginate
      .select(&select)
  end
end

#handle_gitlab_client_exceptionsObject



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
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 78

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::Parsing => e
  @retry_backoff += RETRY_BACK_OFF_DELAY

  raise if @retry_backoff > RETRY_BACK_OFF_DELAY * MAX_RETRY_ATTEMPTS

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

  retry
rescue StandardError => e
  pipeline = QA::Runtime::Env.pipeline_from_project_name
  channel = pipeline == "canary" ? "qa-production" : "qa-#{pipeline}"
  error_msg = warn_exception(e)
  slack_options = {
    channel: channel,
    icon_emoji: ':ci_failing:',
    message: <<~MSG
      An unexpected error occurred while reporting test results in issues.
      The error occurred in job: #{QA::Runtime::Env.ci_job_url}
      `#{error_msg}`
    MSG
  }
  puts "Posting Slack message to channel: #{channel}"

  Gitlab::QA::Slack::PostToSlack.new(**slack_options).invoke!
end