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

Direct Known Subclasses

GitlabIssueDryClient

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

Returns a new instance of 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

#add_note_to_issue_discussion_as_thread(iid:, discussion_id:, body:) ⇒ Object



86
87
88
89
90
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 86

def add_note_to_issue_discussion_as_thread(iid:, discussion_id:, body:)
  handle_gitlab_client_exceptions do
    Gitlab.add_note_to_issue_discussion_as_thread(project, iid, discussion_id, body: body)
  end
end

#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:, issue_type: 'issue') ⇒ Object



66
67
68
69
70
71
72
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 66

def create_issue(title:, description:, labels:, issue_type: 'issue')
  attrs = { issue_type: issue_type, description: description, labels: labels }

  handle_gitlab_client_exceptions do
    Gitlab.create_issue(project, title, attrs)
  end
end

#create_issue_note(iid:, note:) ⇒ Object



80
81
82
83
84
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 80

def create_issue_note(iid:, note:)
  handle_gitlab_client_exceptions do
    Gitlab.create_issue_note(project, iid, note)
  end
end

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



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

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

#find_issue_discussions(iid:) ⇒ Object



60
61
62
63
64
# File 'lib/gitlab/qa/report/gitlab_issue_client.rb', line 60

def find_issue_discussions(iid:)
  handle_gitlab_client_exceptions do
    Gitlab.issue_discussions(project, iid, order_by: 'created_at', sort: 'asc').auto_paginate
  end
end

#find_issues(iid: nil, 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: nil, options: {}, &select)
  select ||= :itself

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

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

#handle_gitlab_client_exceptionsObject



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

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)

  return unless QA::Runtime::Env.ci_commit_ref_name == QA::Runtime::Env.default_branch

  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