Class: Dri::ApiClient
- Inherits:
-
Object
- Object
- Dri::ApiClient
- 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
- #add_note_failure_issue(issue_id, merge_request) ⇒ Object
- #add_test_to_fast_quarantine(failure_url, new_branch, test_path, new_file_content) ⇒ Object
-
#delete_award_emoji(issue_iid, emoji_id:, project_id: GITLAB_PROJECT_ID) ⇒ Gitlab::ObjectifiedHash
Delete award emoji.
-
#fetch_all_new_failures(start_date:, end_date:, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch all new failures.
-
#fetch_all_triaged_failures(emoji:, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch all triaged failures.
-
#fetch_awarded_emojis(issue_iid, project_id:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch award emojis.
-
#fetch_current_triage_issue ⇒ Gitlab::ObjectifiedHash
Fetch current triage issue.
-
#fetch_failing_testcases(pipeline, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch failing testcases.
-
#fetch_failure_notes(project_id, issue_iid) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch failure notes.
-
#fetch_feature_flag_logs(date) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch feature flag log issues.
-
#fetch_mrs(project_id:, **options) ⇒ Object
Fetch MRs.
-
#fetch_new_failures(project_id:, start_date:, end_date:, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch new failures for a given project.
-
#fetch_related_mrs(project_id, issue_iid) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch related issue mrs.
-
#fetch_test_failure_issues(labels: FAILURE_NEW) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch issues related to failing test cases.
-
#fetch_triaged_failures(project_id:, emoji:, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch triaged failures for a given project.
-
#fetch_triaged_incidents(emoji:) ⇒ Object
Fetch triaged incidents.
- #get_failure_issue_title(issue_id) ⇒ Object
- #get_fast_quarantine_tests ⇒ Object
-
#get_file(path, ref:, project_id:) ⇒ Gitlab::ObjectifiedHash
Fetches file contents at <path>.
-
#incidents ⇒ Array<Gitlab::ObjectifiedHash>
Fetch ongoing incidents.
-
#initialize(config, ops = false) ⇒ ApiClient
constructor
A new instance of ApiClient.
-
#list_runbooks(project_id = RUNBOOKS_PROJECT_ID) ⇒ Array<Gitlab::ObjectifedHash>
Fetch runbooks from the runbooks project.
-
#pipeline(project_id, pipeline_id) ⇒ <Gitlab::ObjectifiedHash>
Fetch single pipeline.
-
#pipeline_bridges(project_id, pipeline_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch pipeline bridges/downstream pipelines.
-
#pipeline_jobs(project_id, pipeline_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch jobs from a pipeline.
-
#pipeline_test_report(project_id, pipeline_id) ⇒ <Gitlab::ObjectifiedHash>
Fetch test report from a pipeline.
-
#pipelines(project_id:, options:, auto_paginate: false) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch pipelines.
-
#post_triage_report_note(iid:, body:) ⇒ Gitlab::ObjectifiedHash
Create triage report note.
-
#update_triage_report_note(iid:, note_id:, body:) ⇒ Gitlab::ObjectifiedHash
Update triage report note.
Constructor Details
#initialize(config, ops = false) ⇒ ApiClient
Returns a new instance of ApiClient.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dri/api_client.rb', line 19 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
349 350 351 352 353 354 355 |
# File 'lib/dri/api_client.rb', line 349 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
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/dri/api_client.rb', line 324 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
217 218 219 220 221 222 223 224 |
# File 'lib/dri/api_client.rb', line 217 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
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/dri/api_client.rb', line 170 def fetch_all_new_failures(start_date:, end_date:, state:) [GITLAB_PROJECT_ID, CUSTOMERSDOT_PROJECT_ID].flat_map do |project_id| fetch_new_failures( project_id: project_id, start_date: start_date, end_date: end_date, state: state ) end end |
#fetch_all_triaged_failures(emoji:, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch all triaged failures
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dri/api_client.rb', line 35 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
80 81 82 |
# File 'lib/dri/api_client.rb', line 80 def fetch_awarded_emojis(issue_iid, project_id:) gitlab.award_emojis(project_id, issue_iid, "issue") end |
#fetch_current_triage_issue ⇒ Gitlab::ObjectifiedHash
Fetch current triage issue
137 138 139 140 141 142 143 |
# File 'lib/dri/api_client.rb', line 137 def fetch_current_triage_issue issues = gitlab.issues(TRIAGE_PROJECT_ID, labels: "triage report", state: "opened") issues.find do |issue| issue.title.include?('Pipeline Triage Report') end end |
#fetch_failing_testcases(pipeline, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch failing testcases
89 90 91 92 93 94 95 96 97 |
# File 'lib/dri/api_client.rb', line 89 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
207 208 209 |
# File 'lib/dri/api_client.rb', line 207 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
230 231 232 |
# File 'lib/dri/api_client.rb', line 230 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
130 131 132 |
# File 'lib/dri/api_client.rb', line 130 def fetch_mrs(project_id:, **) gitlab.merge_requests(project_id, per_page: 100, **).auto_paginate end |
#fetch_new_failures(project_id:, start_date:, end_date:, state:) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch new failures for a given project
188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/dri/api_client.rb', line 188 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.dup.utc.iso8601, # utc modifies the receiver, so making a copy created_before: end_date.dup.utc.iso8601, per_page: 100 ) end |
#fetch_related_mrs(project_id, issue_iid) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch related issue mrs
116 117 118 |
# File 'lib/dri/api_client.rb', line 116 def (project_id, issue_iid) gitlab.(project_id, issue_iid) end |
#fetch_test_failure_issues(labels: FAILURE_NEW) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch issues related to failing test cases
102 103 104 105 106 107 108 109 |
# File 'lib/dri/api_client.rb', line 102 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
52 53 54 55 56 57 58 59 60 |
# File 'lib/dri/api_client.rb', line 52 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
65 66 67 68 69 70 71 72 73 |
# File 'lib/dri/api_client.rb', line 65 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
319 320 321 322 |
# File 'lib/dri/api_client.rb', line 319 def get_failure_issue_title(issue_id) issue = gitlab.issue(GITLAB_PROJECT_ID, issue_id) issue.title end |
#get_fast_quarantine_tests ⇒ Object
311 312 313 314 315 316 317 |
# File 'lib/dri/api_client.rb', line 311 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>
307 308 309 |
# File 'lib/dri/api_client.rb', line 307 def get_file(path, ref:, project_id:) gitlab.get_file(project_id, path, ref) end |
#incidents ⇒ Array<Gitlab::ObjectifiedHash>
Fetch ongoing incidents
237 238 239 |
# File 'lib/dri/api_client.rb', line 237 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
293 294 295 296 297 298 299 |
# File 'lib/dri/api_client.rb', line 293 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
258 259 260 |
# File 'lib/dri/api_client.rb', line 258 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
276 277 278 |
# File 'lib/dri/api_client.rb', line 276 def pipeline_bridges(project_id, pipeline_id, = {}) gitlab.pipeline_bridges(project_id, pipeline_id, ).auto_paginate end |
#pipeline_jobs(project_id, pipeline_id, options = {}) ⇒ Array<Gitlab::ObjectifiedHash>
Fetch jobs from a pipeline
285 286 287 |
# File 'lib/dri/api_client.rb', line 285 def pipeline_jobs(project_id, pipeline_id, = {}) gitlab.pipeline_jobs(project_id, pipeline_id, ).auto_paginate end |
#pipeline_test_report(project_id, pipeline_id) ⇒ <Gitlab::ObjectifiedHash>
Fetch test report from a pipeline
267 268 269 |
# File 'lib/dri/api_client.rb', line 267 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
245 246 247 248 249 250 251 |
# File 'lib/dri/api_client.rb', line 245 def pipelines(project_id:, options:, auto_paginate: false) if auto_paginate gitlab.pipelines(project_id, ).auto_paginate else gitlab.pipelines(project_id, ) end end |
#post_triage_report_note(iid:, body:) ⇒ Gitlab::ObjectifiedHash
Create triage report note
150 151 152 |
# File 'lib/dri/api_client.rb', line 150 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
160 161 162 |
# File 'lib/dri/api_client.rb', line 160 def update_triage_report_note(iid:, note_id:, body:) gitlab.edit_issue_note(TRIAGE_PROJECT_ID, iid, note_id, body) end |