lib/danger/request_sources/github/github.rb in danger-9.1.0 vs lib/danger/request_sources/github/github.rb in danger-9.2.0

- old
+ new

@@ -1,7 +1,5 @@ -# coding: utf-8 - # rubocop:disable Metrics/ClassLength require "octokit" require "danger/helpers/comments_helper" require "danger/helpers/comment" @@ -63,10 +61,11 @@ @scm ||= GitRepo.new end 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.connection_options[:ssl] = { verify: verify_ssl } end if valid_bearer_token? @@ -81,10 +80,11 @@ @pr_diff ||= client.pull_request(ci_source.repo_slug, ci_source.pull_request_id, accept: "application/vnd.github.v3.diff") end 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 @@ -131,14 +131,12 @@ href = pr_json["_links"]["issue"]["href"] self.issue_json = client.get(href) end def issue_comments - @comments ||= begin - client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id) - .map { |comment| Comment.from_github(comment) } - end + @comments ||= client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id) + .map { |comment| Comment.from_github(comment) } end # Sending data to GitHub def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false) comment_result = {} @@ -222,11 +220,11 @@ client.create_status(ci_source.repo_slug, latest_pr_commit_ref, status, { description: message, context: "danger/#{danger_id}", target_url: details_url }) - rescue + 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 @@ -246,10 +244,11 @@ # Get rid of the previously posted comment, to only have the latest one 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 def submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger") @@ -454,13 +453,13 @@ end end # @return [String] The organisation name, is nil if it can't be detected def organisation - matched = self.issue_json["repository_url"].match(%r{repos\/(.*)\/}) + matched = self.issue_json["repository_url"].match(%r{repos/(.*)/}) return matched[1] if matched && matched[1] - rescue + rescue StandardError nil end def dismiss_out_of_range_messages_for(kind) if self.dismiss_out_of_range_messages.kind_of?(Hash) && self.dismiss_out_of_range_messages[kind] @@ -471,21 +470,22 @@ false end end # @return [String] A URL to the specific file, ready to be downloaded - def file_url(organisation: nil, repository: nil, branch: nil, path: nil) + def file_url(organisation: nil, repository: nil, ref: nil, branch: nil, path: nil) organisation ||= self.organisation + ref ||= branch begin - # Retrieve the download URL (default branch on nil param) - contents = client.contents("#{organisation}/#{repository}", path: path, ref: branch) + # 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 - branch ||= "master" - @download_url = "https://raw.githubusercontent.com/#{organisation}/#{repository}/#{branch}/#{path}" + ref ||= "master" + @download_url = "https://raw.githubusercontent.com/#{organisation}/#{repository}/#{ref}/#{path}" end end private @@ -510,9 +510,10 @@ cmp = proc do |a, b| next -1 unless a.file && a.line next 1 unless b.file && b.line next a.line <=> b.line if a.file == b.file + next a.file <=> b.file end # Sort to group inline comments by file {