Class: Danger::RequestSources::GitHub
- Inherits:
-
RequestSource
- Object
- RequestSource
- Danger::RequestSources::GitHub
- Includes:
- Helpers::CommentsHelper
- Defined in:
- lib/danger/request_sources/github/github.rb
Defined Under Namespace
Classes: DiffLineReference
Constant Summary
Constants inherited from RequestSource
RequestSource::DANGER_REPO_NAME
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#dismiss_out_of_range_messages ⇒ Object
Returns the value of attribute dismiss_out_of_range_messages.
-
#host ⇒ Object
Returns the value of attribute host.
-
#issue_json ⇒ Object
Returns the value of attribute issue_json.
-
#pr_json ⇒ Object
Returns the value of attribute pr_json.
-
#support_tokenless_auth ⇒ Object
Returns the value of attribute support_tokenless_auth.
-
#use_local_git ⇒ Object
Returns the value of attribute use_local_git.
-
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
Attributes inherited from RequestSource
#ci_source, #ignored_violations
Class Method Summary collapse
Instance Method Summary collapse
- #client ⇒ Object
- #create_inline_comment(body, head_ref, message, position) ⇒ Object
-
#delete_old_comments!(except: nil, danger_id: "danger") ⇒ Object
Get rid of the previously posted comment, to only have the latest one.
- #delete_old_inline_violations(danger_comments: [], non_danger_comments: []) ⇒ Object
- #dismiss_out_of_range_messages_for(kind) ⇒ Object
- #fetch_details ⇒ Object
- #fetch_issue_details(pr_json) ⇒ Object
-
#file_url(organisation: nil, repository: nil, ref: nil, branch: nil, path: nil) ⇒ String
A URL to the specific file, ready to be downloaded.
- #find_position_in_diff(diff_lines, message, kind) ⇒ Object
- #get_pr_from_branch(repo_name, branch_name, owner) ⇒ Object
- #ignored_violations_from_pr ⇒ Object
-
#initialize(ci_source, environment) ⇒ GitHub
constructor
A new instance of GitHub.
- #inline_comment_matches?(comment_data, message, position) ⇒ Boolean
- #issue_comments ⇒ Object
- #markdown_link_to_message(message, hide_link) ⇒ Object
- #messages_are_equivalent(m1, m2) ⇒ Object
-
#organisation ⇒ String
The organisation name, is nil if it can’t be detected.
-
#parse_message_from_row(row) ⇒ Object
See the tests for examples of data coming in looks like.
- #pr_diff ⇒ Object
- #ranged_inline_comment?(message) ⇒ Boolean
- #ranged_inline_comment_lines_match?(comment_data, message) ⇒ Boolean
- #review ⇒ Object
- #scm ⇒ Object
- #setup_danger_branches ⇒ Object
- #start_position_in_diff(diff_lines, message, kind) ⇒ Object
- #submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger") ⇒ Object
- #submit_inline_comments_for_kind!(kind, messages, diff_lines, danger_comments, previous_violations, danger_id: "danger") ⇒ Object
- #submit_pull_request_status!(warnings: [], errors: [], details_url: [], danger_id: "danger") ⇒ Object
-
#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false) ⇒ Object
Sending data to GitHub.
- #validates_as_api_source? ⇒ Boolean
- #validates_as_ci? ⇒ Boolean
Methods included from Helpers::CommentsHelper
#apply_template, #generate_comment, #generate_description, #generate_inline_comment_body, #generate_inline_markdown_body, #generate_message_group_comment, #markdown_parser, #process_markdown, #random_compliment, #table
Methods included from Helpers::CommentsParsingHelper
#parse_comment, #parse_tables_from_comment, #table_kind_from_title, #violations_from_table
Methods inherited from RequestSource
available_request_sources, available_source_names_and_envs, inherited, #inspect, source_name, #update_build_status
Constructor Details
#initialize(ci_source, environment) ⇒ GitHub
Returns a new instance of GitHub.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/danger/request_sources/github/github.rb', line 29 def initialize(ci_source, environment) self.ci_source = ci_source self.use_local_git = environment["DANGER_USE_LOCAL_GIT"] self.support_tokenless_auth = false self. = false self.host = environment.fetch("DANGER_GITHUB_HOST", "github.com") # `DANGER_GITHUB_API_HOST` is the old name kept for legacy reasons and # backwards compatibility. `DANGER_GITHUB_API_BASE_URL` is the new # correctly named variable. self.api_url = environment.fetch("DANGER_GITHUB_API_HOST") do environment.fetch("DANGER_GITHUB_API_BASE_URL", "https://api.github.com/") end self.verify_ssl = environment["DANGER_OCTOKIT_VERIFY_SSL"] != "false" @access_token = environment["DANGER_GITHUB_API_TOKEN"] @bearer_token = environment["DANGER_GITHUB_BEARER_TOKEN"] end |
Instance Attribute Details
#api_url ⇒ Object
Returns the value of attribute api_url.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def api_url @api_url end |
#dismiss_out_of_range_messages ⇒ Object
Returns the value of attribute dismiss_out_of_range_messages.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def @dismiss_out_of_range_messages end |
#host ⇒ Object
Returns the value of attribute host.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def host @host end |
#issue_json ⇒ Object
Returns the value of attribute issue_json.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def issue_json @issue_json end |
#pr_json ⇒ Object
Returns the value of attribute pr_json.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def pr_json @pr_json end |
#support_tokenless_auth ⇒ Object
Returns the value of attribute support_tokenless_auth.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def support_tokenless_auth @support_tokenless_auth end |
#use_local_git ⇒ Object
Returns the value of attribute use_local_git.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def use_local_git @use_local_git end |
#verify_ssl ⇒ Object
Returns the value of attribute verify_ssl.
19 20 21 |
# File 'lib/danger/request_sources/github/github.rb', line 19 def verify_ssl @verify_ssl end |
Class Method Details
.env_vars ⇒ Object
21 22 23 |
# File 'lib/danger/request_sources/github/github.rb', line 21 def self.env_vars ["DANGER_GITHUB_API_TOKEN", "DANGER_GITHUB_BEARER_TOKEN"] end |
.optional_env_vars ⇒ Object
25 26 27 |
# File 'lib/danger/request_sources/github/github.rb', line 25 def self.optional_env_vars ["DANGER_GITHUB_HOST", "DANGER_GITHUB_API_BASE_URL", "DANGER_OCTOKIT_VERIFY_SSL"] end |
Instance Method Details
#client ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/danger/request_sources/github/github.rb', line 66 def client raise "No API token given, please provide one using `DANGER_GITHUB_API_TOKEN` or `DANGER_GITHUB_BEARER_TOKEN`" if !valid_access_token? && !valid_bearer_token? && !support_tokenless_auth @client ||= begin Octokit.configure do |config| config.[:ssl] = { verify: verify_ssl } end if valid_bearer_token? Octokit::Client.new(bearer_token: @bearer_token, auto_paginate: true, api_endpoint: api_url) elsif valid_access_token? Octokit::Client.new(access_token: @access_token, auto_paginate: true, api_endpoint: api_url) end end end |
#create_inline_comment(body, head_ref, message, position) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/danger/request_sources/github/github.rb', line 412 def create_inline_comment(body, head_ref, , position) # Since Octokit v8, the signature of create_pull_request_comment has been changed. # See https://github.com/danger/danger/issues/1475 for detailed information. if ranged_inline_comment?() && Octokit::MAJOR >= 8 # GitHub ranges are required for multi-line suggestion blocks that # replace an existing added translator comment plus the added string. client.create_pull_request_comment( ci_source.repo_slug, ci_source.pull_request_id, body, head_ref, .file, .line, start_line: .start_line, side: .side || "RIGHT", start_side: .start_side || "RIGHT" ) else # Octokit v7 only supports diff positions, so ranged metadata is ignored. client.create_pull_request_comment( ci_source.repo_slug, ci_source.pull_request_id, body, head_ref, .file, (Octokit::MAJOR >= 8 ? .line : position) ) end end |
#delete_old_comments!(except: nil, danger_id: "danger") ⇒ Object
Get rid of the previously posted comment, to only have the latest one
264 265 266 267 268 269 270 271 |
# File 'lib/danger/request_sources/github/github.rb', line 264 def delete_old_comments!(except: nil, danger_id: "danger") issue_comments.each do |comment| next unless comment.generated_by_danger?(danger_id) next if comment.id == except client.delete_comment(ci_source.repo_slug, comment.id) end end |
#delete_old_inline_violations(danger_comments: [], non_danger_comments: []) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/danger/request_sources/github/github.rb', line 301 def delete_old_inline_violations(danger_comments: [], non_danger_comments: []) danger_comments.each do |comment| violation = violations_from_table(comment["body"]).first if !violation.nil? && violation.sticky body = generate_inline_comment_body("white_check_mark", violation, danger_id: danger_id, resolved: true, template: "github") client.update_pull_request_comment(ci_source.repo_slug, comment["id"], body) else # We remove non-sticky violations that have no replies # Since there's no direct concept of a reply in GH, we simply consider # the existence of non-danger comments in that line as replies replies = non_danger_comments.select do |potential| potential["path"] == comment["path"] && potential["position"] == comment["position"] && potential["commit_id"] == comment["commit_id"] end client.delete_pull_request_comment(ci_source.repo_slug, comment["id"]) if replies.empty? end end end |
#dismiss_out_of_range_messages_for(kind) ⇒ Object
549 550 551 552 553 554 555 556 557 |
# File 'lib/danger/request_sources/github/github.rb', line 549 def (kind) if self..kind_of?(Hash) && self.[kind] self.[kind] elsif self. == true self. else false end end |
#fetch_details ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/danger/request_sources/github/github.rb', line 135 def fetch_details self.pr_json = client.pull_request(ci_source.repo_slug, ci_source.pull_request_id) if self.pr_json["message"] == "Moved Permanently" raise "Repo moved or renamed, make sure to update the git remote".red end fetch_issue_details(self.pr_json) self.ignored_violations = ignored_violations_from_pr end |
#fetch_issue_details(pr_json) ⇒ Object
149 150 151 152 |
# File 'lib/danger/request_sources/github/github.rb', line 149 def fetch_issue_details(pr_json) href = pr_json["_links"]["issue"]["href"] self.issue_json = client.get(href) end |
#file_url(organisation: nil, repository: nil, ref: nil, branch: nil, path: nil) ⇒ String
Returns A URL to the specific file, ready to be downloaded.
560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/danger/request_sources/github/github.rb', line 560 def file_url(organisation: nil, repository: nil, ref: nil, branch: nil, path: nil) organisation ||= self.organisation ref ||= branch begin # Retrieve the download URL (default ref on nil param) contents = client.contents("#{organisation}/#{repository}", path: path, ref: ref) @download_url = contents["download_url"] rescue Octokit::ClientError # Fallback to github.com ref ||= "master" @download_url = "https://raw.githubusercontent.com/#{organisation}/#{repository}/#{ref}/#{path}" end end |
#find_position_in_diff(diff_lines, message, kind) ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/danger/request_sources/github/github.rb', line 455 def find_position_in_diff(diff_lines, , kind) range_header_regexp = /@@ -([0-9]+)(,([0-9]+))? \+(?<start>[0-9]+)(,(?<end>[0-9]+))? @@.*/ file_header_regexp = %r{^diff --git a/.*} pattern = "+++ b/#{.file}\n" file_start = diff_lines.index(pattern) # Files containing spaces sometimes have a trailing tab if file_start.nil? pattern = "+++ b/#{.file}\t\n" file_start = diff_lines.index(pattern) end return nil if file_start.nil? position = -1 file_line = nil diff_lines.drop(file_start).each do |line| # If the line has `No newline` annotation, position need increment if line.eql?("\\ No newline at end of file\n") position += 1 next end # If we found the start of another file diff, we went too far break if line.match file_header_regexp match = line.match range_header_regexp # file_line is set once we find the hunk the line is in # we need to count how many lines in new file we have # so we do it one by one ignoring the deleted lines if !file_line.nil? && !line.start_with?("-") if file_line == .line file_line = nil if (kind) && !line.start_with?("+") break end file_line += 1 end # We need to count how many diff lines are between us and # the line we're looking for position += 1 next unless match range_start = match[:start].to_i if match[:end] range_end = match[:end].to_i + range_start else range_end = range_start end # We are past the line position, just abort break if .line.to_i < range_start next unless .line.to_i >= range_start && .line.to_i < range_end file_line = range_start end position unless file_line.nil? end |
#get_pr_from_branch(repo_name, branch_name, owner) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/danger/request_sources/github/github.rb', line 47 def get_pr_from_branch(repo_name, branch_name, owner) prs = client.pull_requests(repo_name, head: "#{owner}:#{branch_name}") unless prs.empty? prs.first.number end end |
#ignored_violations_from_pr ⇒ Object
145 146 147 |
# File 'lib/danger/request_sources/github/github.rb', line 145 def ignored_violations_from_pr GetIgnoredViolation.new(self.pr_json["body"]).call end |
#inline_comment_matches?(comment_data, message, position) ⇒ Boolean
402 403 404 405 406 407 408 409 410 |
# File 'lib/danger/request_sources/github/github.rb', line 402 def inline_comment_matches?(comment_data, , position) return false unless comment_data["path"] == .file if ranged_inline_comment?() ranged_inline_comment_lines_match?(comment_data, ) else comment_data["position"] == position end end |
#issue_comments ⇒ Object
154 155 156 157 |
# File 'lib/danger/request_sources/github/github.rb', line 154 def issue_comments @comments ||= client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id) .map { |comment| Comment.from_github(comment) } end |
#markdown_link_to_message(message, hide_link) ⇒ Object
531 532 533 534 535 536 537 538 539 |
# File 'lib/danger/request_sources/github/github.rb', line 531 def (, hide_link) url = "https://#{host}/#{ci_source.repo_slug}/blob/#{pr_json['head']['sha']}/#{.file}#L#{.line}" if hide_link "<span data-href=\"#{url}\"/>" else "[#{.file}#L#{.line}](#{url}) - " end end |
#messages_are_equivalent(m1, m2) ⇒ Object
322 323 324 325 326 |
# File 'lib/danger/request_sources/github/github.rb', line 322 def (m1, m2) blob_regexp = %r{blob/[0-9a-z]+/} m1.file == m2.file && m1.line == m2.line && m1..sub(blob_regexp, "") == m2..sub(blob_regexp, "") end |
#organisation ⇒ String
Returns The organisation name, is nil if it can’t be detected.
542 543 544 545 546 547 |
# File 'lib/danger/request_sources/github/github.rb', line 542 def organisation matched = self.issue_json["repository_url"].match(%r{repos/(.*)/}) return matched[1] if matched && matched[1] rescue StandardError nil end |
#parse_message_from_row(row) ⇒ Object
See the tests for examples of data coming in looks like
519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/danger/request_sources/github/github.rb', line 519 def (row) = %r{(<(a |span data-)href="https://#{host}/#{ci_source.repo_slug}/blob/[0-9a-z]+/(?<file>[^#]+)#L(?<line>[0-9]+)"(>[^<]*</a> - |/>))?(?<message>.*?)}im match = .match(row) if match[:line] line = match[:line].to_i else line = nil end Violation.new(row, true, match[:file], line) end |
#pr_diff ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/danger/request_sources/github/github.rb', line 81 def pr_diff # This is a hack to get the file patch into a format that parse-diff accepts # as the GitHub API for listing pull request files is missing file names in the patch. prefixed_patch = lambda do |file:| <<~PATCH diff --git a/#{file['filename']} b/#{file['filename']} --- a/#{file['filename']} +++ b/#{file['filename']} #{file['patch']} PATCH end files = client.pull_request_files( ci_source.repo_slug, ci_source.pull_request_id, accept: "application/vnd.github.v3.diff" ) @pr_diff ||= files.map { |file| prefixed_patch.call(file: file) }.join("\n") end |
#ranged_inline_comment?(message) ⇒ Boolean
442 443 444 |
# File 'lib/danger/request_sources/github/github.rb', line 442 def ranged_inline_comment?() !.start_line.nil? end |
#ranged_inline_comment_lines_match?(comment_data, message) ⇒ Boolean
446 447 448 449 450 451 452 453 |
# File 'lib/danger/request_sources/github/github.rb', line 446 def ranged_inline_comment_lines_match?(comment_data, ) comment_line = comment_data["line"] comment_start_line = comment_data["start_line"] return false if comment_line.nil? || comment_start_line.nil? comment_line.to_i == .line && comment_start_line.to_i == .start_line end |
#review ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/danger/request_sources/github/github.rb', line 102 def review return @review unless @review.nil? begin @review = client.pull_request_reviews(ci_source.repo_slug, ci_source.pull_request_id) .map { |review_json| Danger::RequestSources::GitHubSource::Review.new(client, ci_source, review_json) } .select(&:generated_by_danger?) .last @review ||= Danger::RequestSources::GitHubSource::Review.new(client, ci_source) @review rescue Octokit::NotFound @review = Danger::RequestSources::GitHubSource::ReviewUnsupported.new @review end end |
#scm ⇒ Object
62 63 64 |
# File 'lib/danger/request_sources/github/github.rb', line 62 def scm @scm ||= GitRepo.new end |
#setup_danger_branches ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/danger/request_sources/github/github.rb', line 118 def setup_danger_branches # we can use a github specific feature here: base_branch = self.pr_json["base"]["ref"] base_commit = self.pr_json["base"]["sha"] head_branch = self.pr_json["head"]["ref"] head_commit = self.pr_json["head"]["sha"] # Next, we want to ensure that we have a version of the current branch at a known location scm.ensure_commitish_exists_on_branch! base_branch, base_commit self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}" # OK, so we want to ensure that we have a known head branch, this will always represent # the head of the PR ( e.g. the most recent commit that will be merged. ) scm.ensure_commitish_exists_on_branch! head_branch, head_commit self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}" end |
#start_position_in_diff(diff_lines, message, kind) ⇒ Object
395 396 397 398 399 400 |
# File 'lib/danger/request_sources/github/github.rb', line 395 def start_position_in_diff(diff_lines, , kind) return nil unless ranged_inline_comment?() = DiffLineReference.new(.file, .start_line) find_position_in_diff(diff_lines, , kind) || :out_of_range end |
#submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger") ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/danger/request_sources/github/github.rb', line 273 def submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger") pr_comments = client.pull_request_comments(ci_source.repo_slug, ci_source.pull_request_id) danger_comments = pr_comments.select { |comment| Comment.from_github(comment).generated_by_danger?(danger_id) } non_danger_comments = pr_comments - danger_comments if (warnings + errors + + markdowns).select(&:inline?).empty? delete_old_inline_violations(danger_comments: danger_comments, non_danger_comments: non_danger_comments) return {} end diff_lines = self.pr_diff.lines warnings = submit_inline_comments_for_kind!(:warning, warnings, diff_lines, danger_comments, previous_violations["warning"], danger_id: danger_id) errors = submit_inline_comments_for_kind!(:error, errors, diff_lines, danger_comments, previous_violations["error"], danger_id: danger_id) = submit_inline_comments_for_kind!(:message, , diff_lines, danger_comments, previous_violations["message"], danger_id: danger_id) markdowns = submit_inline_comments_for_kind!(:markdown, markdowns, diff_lines, danger_comments, [], danger_id: danger_id) # submit removes from the array all comments that are still in force # so we strike out all remaining ones delete_old_inline_violations(danger_comments: danger_comments, non_danger_comments: non_danger_comments) { warnings: warnings, errors: errors, messages: , markdowns: markdowns } end |
#submit_inline_comments_for_kind!(kind, messages, diff_lines, danger_comments, previous_violations, danger_id: "danger") ⇒ Object
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/danger/request_sources/github/github.rb', line 328 def submit_inline_comments_for_kind!(kind, , diff_lines, danger_comments, previous_violations, danger_id: "danger") head_ref = pr_json["head"]["sha"] previous_violations ||= [] is_markdown_content = kind == :markdown emoji = { warning: "warning", error: "no_entry_sign", message: "book" }[kind] .reject do |m| # rubocop:todo Metrics/BlockLength next false unless m.file && m.line position = find_position_in_diff diff_lines, m, kind start_position = start_position_in_diff(diff_lines, m, kind) # Keep the change if it's line is not in the diff and not in dismiss mode next (kind) if position.nil? || start_position == :out_of_range # Once we know we're gonna submit it, we format it if is_markdown_content body = generate_inline_markdown_body(m, danger_id: danger_id, template: "github") else # Hide the inline link behind a span m = process_markdown(m, true) body = generate_inline_comment_body(emoji, m, danger_id: danger_id, template: "github") # A comment might be in previous_violations because only now it's part of the unified diff # We remove from the array since it won't have a place in the table anymore previous_violations.reject! { |v| (v, m) } end matching_comments = danger_comments.select do |comment_data| if inline_comment_matches?(comment_data, m, position) # Parse it to avoid problems with strikethrough violation = violations_from_table(comment_data["body"]).first if violation (violation, m) else blob_regexp = %r{blob/[0-9a-z]+/} comment_data["body"].sub(blob_regexp, "") == body.sub(blob_regexp, "") end else false end end if matching_comments.empty? begin create_inline_comment(body, head_ref, m, position) rescue Octokit::UnprocessableEntity => e # Show more detail for UnprocessableEntity error = [e, "body: #{body}", "head_ref: #{head_ref}", "filename: #{m.file}", "position: #{position}"].join("\n") puts # Not reject because this comment has not completed next false end else # Remove the surviving comment so we don't strike it out danger_comments.reject! { |c| matching_comments.include? c } # Update the comment to remove the strikethrough if present comment = matching_comments.first client.update_pull_request_comment(ci_source.repo_slug, comment["id"], body) end # Remove this element from the array next true end end |
#submit_pull_request_status!(warnings: [], errors: [], details_url: [], danger_id: "danger") ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/danger/request_sources/github/github.rb', line 229 def submit_pull_request_status!(warnings: [], errors: [], details_url: [], danger_id: "danger") status = (errors.count.zero? ? "success" : "failure") = generate_description(warnings: warnings, errors: errors) latest_pr_commit_ref = self.pr_json["head"]["sha"] if latest_pr_commit_ref.empty? || latest_pr_commit_ref.nil? raise "Couldn't find a commit to update its status".red end begin client.create_status(ci_source.repo_slug, latest_pr_commit_ref, status, { description: , context: "danger/#{danger_id}", target_url: details_url }) rescue StandardError # This usually means the user has no commit access to this repo # That's always the case for open source projects where you can only # use a read-only GitHub account if errors.count > 0 # We need to fail the actual build here is_private = pr_json["base"]["repo"]["private"] if is_private abort("\nDanger has failed this build. \nFound #{'error'.danger_pluralize(errors.count)} and I don't have write access to the PR to set a PR status.") else abort("\nDanger has failed this build. \nFound #{'error'.danger_pluralize(errors.count)}.") end else puts puts "\nDanger does not have write access to the PR to set a PR status.".yellow end end end |
#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false) ⇒ Object
Sending data to GitHub
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/danger/request_sources/github/github.rb', line 160 def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false) comment_result = {} editable_comments = issue_comments.select { |comment| comment.generated_by_danger?(danger_id) } last_comment = editable_comments.last should_create_new_comment = new_comment || last_comment.nil? || remove_previous_comments previous_violations = if should_create_new_comment {} else parse_comment(last_comment.body) end regular_violations = regular_violations_group( warnings: warnings, errors: errors, messages: , markdowns: markdowns ) inline_violations = inline_violations_group( warnings: warnings, errors: errors, messages: , markdowns: markdowns ) rest_inline_violations = submit_inline_comments!(**{ danger_id: danger_id, previous_violations: previous_violations }.merge(inline_violations)) main_violations = merge_violations( regular_violations, rest_inline_violations ) main_violations_sum = main_violations.values.inject(:+) if (previous_violations.empty? && main_violations_sum.empty?) || remove_previous_comments # Just remove the comment, if there's nothing to say or --remove-previous-comments CLI was set. delete_old_comments!(danger_id: danger_id) end # If there are still violations to show if main_violations_sum.any? body = generate_comment(**{ template: "github", danger_id: danger_id, previous_violations: previous_violations }.merge(main_violations)) comment_result = if should_create_new_comment client.add_comment(ci_source.repo_slug, ci_source.pull_request_id, body) else client.update_comment(ci_source.repo_slug, last_comment.id, body) end end # Now, set the pull request status. # Note: this can terminate the entire process. submit_pull_request_status!( warnings: warnings, errors: errors, details_url: comment_result["html_url"], danger_id: danger_id ) end |
#validates_as_api_source? ⇒ Boolean
58 59 60 |
# File 'lib/danger/request_sources/github/github.rb', line 58 def validates_as_api_source? valid_bearer_token? || valid_access_token? || use_local_git end |
#validates_as_ci? ⇒ Boolean
54 55 56 |
# File 'lib/danger/request_sources/github/github.rb', line 54 def validates_as_ci? true end |