Class: Dri::ApiClient

Inherits:
Object
  • Object
show all
Includes:
Utils::Constants::ProjectIDs, Utils::Constants::Triage::Labels
Defined in:
lib/dri/api_client.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

API_URL =
"https://gitlab.com/api/v4"
OPS_API_URL =
"https://ops.gitlab.net/api/v4"

Constants included from Utils::Constants::Triage::Labels

Utils::Constants::Triage::Labels::FAILURE, Utils::Constants::Triage::Labels::FAILURE_NEW, Utils::Constants::Triage::Labels::FOUND, Utils::Constants::Triage::Labels::INCIDENT, Utils::Constants::Triage::Labels::QA, Utils::Constants::Triage::Labels::QUALITY, Utils::Constants::Triage::Labels::QUARANTINE, Utils::Constants::Triage::Labels::SERVICE

Constants included from Utils::Constants::ProjectIDs

Utils::Constants::ProjectIDs::CUSTOMERSDOT_PROJECT_ID, Utils::Constants::ProjectIDs::FAST_QUARANTINE_PROJECT_ID, Utils::Constants::ProjectIDs::FEATURE_FLAG_LOG_PROJECT_ID, Utils::Constants::ProjectIDs::GITLAB_PROJECT_ID, Utils::Constants::ProjectIDs::INFRA_TEAM_PROD_PROJECT_ID, Utils::Constants::ProjectIDs::RUNBOOKS_PROJECT_ID, Utils::Constants::ProjectIDs::TESTCASES_PROJECT_ID, Utils::Constants::ProjectIDs::TRIAGE_PROJECT_ID

Instance Method Summary collapse

Constructor Details

#initialize(config, ops = false) ⇒ ApiClient

Returns a new instance of ApiClient.



20
21
22
23
24
25
26
27
28
29
# File 'lib/dri/api_client.rb', line 20

def initialize(config, ops = false)
  @token = config.read.dig("settings", "token")
  @ops_token = config.read.dig("settings", "ops_token")
  if @token.nil? || @ops_token.nil?
    raise TokenNotProvidedError, "Gitlab API client cannot be initialized without both access tokens. " \
                                 "Run `dri init` again or `dri profile --edit` to add an ops_token entry."
  end

  @ops_instance = ops
end

Instance Method Details

#add_note_failure_issue(issue_id, merge_request) ⇒ Object



354
355
356
357
358
359
360
# File 'lib/dri/api_client.rb', line 354

def add_note_failure_issue(issue_id, merge_request)
  gitlab.create_issue_note(
    GITLAB_PROJECT_ID,
    issue_id,
    "Applying fast quarantine at #{merge_request.web_url}."
  )
end

#add_test_to_fast_quarantine(failure_url, new_branch, test_path, new_file_content) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/dri/api_client.rb', line 329

def add_test_to_fast_quarantine(failure_url, new_branch, test_path, new_file_content)
  gitlab.create_branch(
    FAST_QUARANTINE_PROJECT_ID,
    new_branch,
    'main'
  )

  gitlab.edit_file(
    FAST_QUARANTINE_PROJECT_ID,
    'rspec/fast_quarantine-gitlab.txt',
    new_branch,
    new_file_content,
    'Add test to quarantine'
  )

  gitlab.create_merge_request(
    FAST_QUARANTINE_PROJECT_ID,
    "Quarantine #{test_path}", {
      source_branch: new_branch,
      target_branch: 'main',
      description: "Fast quarantine for `#{test_path}`. Failure: #{failure_url}."
    }
  )
end

#delete_award_emoji(issue_iid, emoji_id:, project_id: GITLAB_PROJECT_ID) ⇒ Gitlab::ObjectifiedHash

Delete award emoji

Parameters:

  • issue_iid (Integer)
  • emoji_id (Integer)
  • project_id (Integer) (defaults to: GITLAB_PROJECT_ID)

Returns:

  • (Gitlab::ObjectifiedHash)


222
223
224
225
226
227
228
229
# File 'lib/dri/api_client.rb', line 222

def delete_award_emoji(issue_iid, emoji_id:, project_id: GITLAB_PROJECT_ID)
  gitlab.delete_award_emoji(
    project_id,
    issue_iid,
    "issue",
    emoji_id
  )
end

#fetch_all_new_failures(start_date:, end_date:, state:) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch all new failures

Parameters:

  • start_date (String)
  • end_date (String)
  • state (String)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dri/api_client.rb', line 167

def fetch_all_new_failures(start_date:, end_date:, state:)
  project_ids = [GITLAB_PROJECT_ID, CUSTOMERSDOT_PROJECT_ID]
  failures = []

  start_date_iso = start_date.strftime('%Y-%m-%dT00:00:00Z')
  end_date_iso = end_date.strftime('%Y-%m-%dT23:59:59Z')

  project_ids.each do |project_id|
    failures += fetch_new_failures(
      project_id: project_id,
      start_date: start_date_iso,
      end_date: end_date_iso,
      state: state
    )
  end

  failures
end

#fetch_all_triaged_failures(emoji:, state:) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch all triaged failures

Parameters:

  • emoji (String)
  • state (String)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


36
37
38
39
40
41
42
43
44
45
# File 'lib/dri/api_client.rb', line 36

def fetch_all_triaged_failures(emoji:, state:)
  project_ids = [GITLAB_PROJECT_ID, CUSTOMERSDOT_PROJECT_ID]
  failures = []

  project_ids.each do |project_id|
    failures += fetch_triaged_failures(project_id: project_id, emoji: emoji, state: state)
  end

  failures
end

#fetch_awarded_emojis(issue_iid, project_id:) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch award emojis

Parameters:

  • issue_iid (Integer)
  • project_id (Integer)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


81
82
83
# File 'lib/dri/api_client.rb', line 81

def fetch_awarded_emojis(issue_iid, project_id:)
  gitlab.award_emojis(project_id, issue_iid, "issue")
end

#fetch_current_triage_issueGitlab::ObjectifiedHash

Fetch current triage issue

Returns:

  • (Gitlab::ObjectifiedHash)


138
139
140
# File 'lib/dri/api_client.rb', line 138

def fetch_current_triage_issue
  gitlab.issues(TRIAGE_PROJECT_ID, state: "opened", order_by: "updated_at")
end

#fetch_failing_testcases(pipeline, state:) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch failing testcases

Parameters:

  • pipeline (String)
  • state (String)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


90
91
92
93
94
95
96
97
98
# File 'lib/dri/api_client.rb', line 90

def fetch_failing_testcases(pipeline, state:)
  gitlab.issues(
    TESTCASES_PROJECT_ID,
    labels: "#{pipeline}::failed",
    state: state,
    scope: "all",
    'not[labels]': QUARANTINE
  ).auto_paginate
end

#fetch_failure_notes(project_id, issue_iid) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch failure notes

Parameters:

  • project_id (Integer)
  • issue_iid (Integer)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


212
213
214
# File 'lib/dri/api_client.rb', line 212

def fetch_failure_notes(project_id, issue_iid)
  gitlab.issue_notes(project_id, issue_iid, per_page: 100).auto_paginate
end

#fetch_feature_flag_logs(date) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch feature flag log issues

Parameters:

  • date (String)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


235
236
237
# File 'lib/dri/api_client.rb', line 235

def fetch_feature_flag_logs(date)
  gitlab.issues(FEATURE_FLAG_LOG_PROJECT_ID, created_after: date, per_page: 100).auto_paginate
end

#fetch_mrs(project_id:, **options) ⇒ Object

Fetch MRs

Parameters:

  • options (Hash<String>)

Options Hash (**options):

  • state (String)
  • order_by (String)
  • sort (String)
  • milestone (String)
  • labels (String)

See Also:



131
132
133
# File 'lib/dri/api_client.rb', line 131

def fetch_mrs(project_id:, **options)
  gitlab.merge_requests(project_id, per_page: 100, **options).auto_paginate
end

#fetch_new_failures(project_id:, start_date:, end_date:, state:) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch new failures for a given project

Parameters:

  • project_id (Integer)
  • start_date (String)
  • end_date (String)
  • state (String)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/dri/api_client.rb', line 193

def fetch_new_failures(project_id:, start_date:, end_date:, state:)
  gitlab.issues(
    project_id,
    labels: [FAILURE_NEW, QA].join(','),
    order_by: "created_at",
    sort: 'desc',
    state: state,
    scope: "all",
    created_after: start_date,
    created_before: end_date,
    per_page: 100
  )
end

Fetch related issue mrs

Parameters:

  • project_id (Integer)
  • issue_iid (Integer)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


117
118
119
# File 'lib/dri/api_client.rb', line 117

def fetch_related_mrs(project_id, issue_iid)
  gitlab.related_issue_merge_requests(project_id, issue_iid)
end

#fetch_test_failure_issues(labels: FAILURE_NEW) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch issues related to failing test cases

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


103
104
105
106
107
108
109
110
# File 'lib/dri/api_client.rb', line 103

def fetch_test_failure_issues(labels: FAILURE_NEW)
  gitlab.issues(
    GITLAB_PROJECT_ID,
    labels: labels,
    state: 'opened',
    scope: "all"
  ).auto_paginate
end

#fetch_triaged_failures(project_id:, emoji:, state:) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch triaged failures for a given project

Parameters:

  • project_id (Integer)
  • emoji (String)
  • state (String)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


53
54
55
56
57
58
59
60
61
# File 'lib/dri/api_client.rb', line 53

def fetch_triaged_failures(project_id:, emoji:, state:)
  gitlab.issues(
    project_id,
    order_by: "updated_at",
    my_reaction_emoji: emoji,
    scope: "all",
    state: state
  )
end

#fetch_triaged_incidents(emoji:) ⇒ Object

Fetch triaged incidents

Parameters:

  • emoji (String)


66
67
68
69
70
71
72
73
74
# File 'lib/dri/api_client.rb', line 66

def fetch_triaged_incidents(emoji:)
  gitlab.issues(
    INFRA_TEAM_PROD_PROJECT_ID,
    order_by: "updated_at",
    my_reaction_emoji: emoji,
    state: "all",
    labels: "incident"
  )
end

#get_failure_issue_title(issue_id) ⇒ Object



324
325
326
327
# File 'lib/dri/api_client.rb', line 324

def get_failure_issue_title(issue_id)
  issue = gitlab.issue(GITLAB_PROJECT_ID, issue_id)
  issue.title
end

#get_fast_quarantine_testsObject



316
317
318
319
320
321
322
# File 'lib/dri/api_client.rb', line 316

def get_fast_quarantine_tests
  gitlab.file_contents(
    FAST_QUARANTINE_PROJECT_ID,
    'rspec/fast_quarantine-gitlab.txt',
    'main'
  )
end

#get_file(path, ref:, project_id:) ⇒ Gitlab::ObjectifiedHash

Fetches file contents at <path>

Parameters:

  • path (String)
  • ref (String)
  • project_id (Integer)

Returns:

  • (Gitlab::ObjectifiedHash)


312
313
314
# File 'lib/dri/api_client.rb', line 312

def get_file(path, ref:, project_id:)
  gitlab.get_file(project_id, path, ref)
end

#incidentsArray<Gitlab::ObjectifiedHash>

Fetch ongoing incidents

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


242
243
244
# File 'lib/dri/api_client.rb', line 242

def incidents
  gitlab.issues(INFRA_TEAM_PROD_PROJECT_ID, order_by: "updated_at", state: "opened", labels: "incident")
end

#list_runbooks(project_id = RUNBOOKS_PROJECT_ID) ⇒ Array<Gitlab::ObjectifedHash>

Fetch runbooks from the runbooks project

Parameters:

  • project_id (Integer) (defaults to: RUNBOOKS_PROJECT_ID)

Returns:

  • (Array<Gitlab::ObjectifedHash>)


298
299
300
301
302
303
304
# File 'lib/dri/api_client.rb', line 298

def list_runbooks(project_id = RUNBOOKS_PROJECT_ID)
  tree = gitlab.tree(project_id, { recursive: true, ref: 'main' }).auto_paginate

  tree.select do |node|
    node.type == 'tree' && !node.name.start_with?('_')
  end
end

#pipeline(project_id, pipeline_id) ⇒ <Gitlab::ObjectifiedHash>

Fetch single pipeline

Parameters:

  • project_id (Integer)
  • pipeline_id (Integer)

Returns:

  • (<Gitlab::ObjectifiedHash>)


263
264
265
# File 'lib/dri/api_client.rb', line 263

def pipeline(project_id, pipeline_id)
  gitlab.pipeline(project_id, pipeline_id)
end

#pipeline_bridges(project_id, pipeline_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch pipeline bridges/downstream pipelines

Parameters:

  • project_id (Integer)
  • pipeline_id (Integer)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


281
282
283
# File 'lib/dri/api_client.rb', line 281

def pipeline_bridges(project_id, pipeline_id, options = {})
  gitlab.pipeline_bridges(project_id, pipeline_id, options).auto_paginate
end

#pipeline_jobs(project_id, pipeline_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch jobs from a pipeline

Parameters:

  • project_id (Integer)
  • pipeline_id (Integer)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


290
291
292
# File 'lib/dri/api_client.rb', line 290

def pipeline_jobs(project_id, pipeline_id, options = {})
  gitlab.pipeline_jobs(project_id, pipeline_id, options).auto_paginate
end

#pipeline_test_report(project_id, pipeline_id) ⇒ <Gitlab::ObjectifiedHash>

Fetch test report from a pipeline

Parameters:

  • project_id (Integer)
  • pipeline_id (Integer)

Returns:

  • (<Gitlab::ObjectifiedHash>)


272
273
274
# File 'lib/dri/api_client.rb', line 272

def pipeline_test_report(project_id, pipeline_id)
  gitlab.pipeline_test_report(project_id, pipeline_id)
end

#pipelines(project_id:, options:, auto_paginate: false) ⇒ Array<Gitlab::ObjectifiedHash>

Fetch pipelines

Parameters:

  • project_id (Integer)

Returns:

  • (Array<Gitlab::ObjectifiedHash>)


250
251
252
253
254
255
256
# File 'lib/dri/api_client.rb', line 250

def pipelines(project_id:, options:, auto_paginate: false)
  if auto_paginate
    gitlab.pipelines(project_id, options).auto_paginate
  else
    gitlab.pipelines(project_id, options)
  end
end

#post_triage_report_note(iid:, body:) ⇒ Gitlab::ObjectifiedHash

Create triage report note

Parameters:

  • iid (Integer)
  • body (String)

Returns:

  • (Gitlab::ObjectifiedHash)


147
148
149
# File 'lib/dri/api_client.rb', line 147

def post_triage_report_note(iid:, body:)
  gitlab.create_issue_note(TRIAGE_PROJECT_ID, iid, body)
end

#update_triage_report_note(iid:, note_id:, body:) ⇒ Gitlab::ObjectifiedHash

Update triage report note

Parameters:

  • iid (Integer)
  • note_id (Integer)
  • body (String)

Returns:

  • (Gitlab::ObjectifiedHash)


157
158
159
# File 'lib/dri/api_client.rb', line 157

def update_triage_report_note(iid:, note_id:, body:)
  gitlab.edit_issue_note(TRIAGE_PROJECT_ID, iid, note_id, body)
end