lib/danger/request_source/github.rb in danger-2.1.6 vs lib/danger/request_source/github.rb in danger-3.0.0
- old
+ new
@@ -14,23 +14,34 @@
self.environment = environment
self.support_tokenless_auth = false
Octokit.auto_paginate = true
@token = @environment["DANGER_GITHUB_API_TOKEN"]
- if @environment["DANGER_GITHUB_API_HOST"]
- Octokit.api_endpoint = @environment["DANGER_GITHUB_API_HOST"]
+ if api_url
+ Octokit.api_endpoint = api_url
end
end
+ def validates_as_api_source?
+ @token && !@token.empty?
+ end
+
def scm
@scm ||= GitRepo.new
end
def host
@host = @environment["DANGER_GITHUB_HOST"] || "github.com"
end
+ def api_url
+ # `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.
+ @environment["DANGER_GITHUB_API_HOST"] || @environment["DANGER_GITHUB_API_BASE_URL"]
+ end
+
def client
raise "No API token given, please provide one using `DANGER_GITHUB_API_TOKEN`" if !@token && !support_tokenless_auth
@client ||= Octokit::Client.new(access_token: @token)
end
@@ -66,21 +77,24 @@
def fetch_issue_details(pr_json)
href = pr_json[:_links][:issue][:href]
self.issue_json = client.get(href)
end
+ def issue_comments
+ @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")
comment_result = {}
+ editable_comments = issue_comments.select { |comment| comment.generated_by_danger?(danger_id) }
- comments = client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id)
- editable_comments = comments.select { |comment| danger_comment?(comment, danger_id) }
-
if editable_comments.empty?
previous_violations = {}
else
- comment = editable_comments.first[:body]
+ comment = editable_comments.first.body
previous_violations = parse_comment(comment)
end
if previous_violations.empty? && (warnings + errors + messages + markdowns).empty?
# Just remove the comment, if there's nothing to say.
@@ -95,11 +109,11 @@
template: "github")
if editable_comments.empty?
comment_result = client.add_comment(ci_source.repo_slug, ci_source.pull_request_id, body)
else
- original_id = editable_comments.first[:id]
+ original_id = editable_comments.first.id
comment_result = client.update_comment(ci_source.repo_slug, original_id, body)
end
end
# Now, set the pull request status.
@@ -143,15 +157,14 @@
end
end
# Get rid of the previously posted comment, to only have the latest one
def delete_old_comments!(except: nil, danger_id: "danger")
- comments = client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id)
- comments.each do |comment|
- next unless danger_comment?(comment, danger_id)
- next if comment[:id] == except
- client.delete_comment(ci_source.repo_slug, comment[:id])
+ 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
# @return [String] The organisation name, is nil if it can't be detected
def organisation
@@ -159,47 +172,13 @@
return matched[1] if matched && matched[1]
rescue
nil
end
- # @return [Hash] with the information about the repo
- # returns nil if the repo is not available
- def fetch_repository(organisation: nil, repository: nil)
- organisation ||= self.organisation
- repository ||= self.ci_source.repo_slug.split("/").last
- self.client.repo("#{organisation}/#{repository}")
- rescue Octokit::NotFound
- nil # repo doesn't exist
- end
-
- # @return [Hash] with the information about the repo.
- # This will automatically detect if the repo is capitalised
- # returns nil if there is no danger repo
- def fetch_danger_repo(organisation: nil)
- data = nil
- data ||= fetch_repository(organisation: organisation, repository: DANGER_REPO_NAME.downcase)
- data ||= fetch_repository(organisation: organisation, repository: DANGER_REPO_NAME.capitalize)
- data
- end
-
- # @return [Bool] is this repo the danger repo of the org?
- def danger_repo?(organisation: nil, repository: nil)
- repo = fetch_repository(organisation: organisation, repository: repository)
- repo[:name].casecmp(DANGER_REPO_NAME).zero? || repo[:parent] && repo[:parent][:full_name] == "danger/danger"
- rescue
- false
- end
-
# @return [String] A URL to the specific file, ready to be downloaded
def file_url(organisation: nil, repository: nil, branch: "master", path: nil)
organisation ||= self.organisation
"https://raw.githubusercontent.com/#{organisation}/#{repository}/#{branch}/#{path}"
- end
-
- private
-
- def danger_comment?(comment, danger_id)
- comment[:body].include?("generated_by_#{danger_id}")
end
end
end
end