lib/danger/request_source/github.rb in danger-0.8.2 vs lib/danger/request_source/github.rb in danger-0.8.3
- old
+ new
@@ -36,19 +36,19 @@
@markdown_parser ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, no_intra_emphasis: true)
end
def setup_danger_branches
# we can use a github specific feature here:
- pull_id = self.ci_source.pull_request_id
- test_branch = self.pr_json[:base][:sha]
+ base_commit = self.pr_json[:base][:sha]
+ head_commit = self.scm.head_commit
# Next, we want to ensure that we have a version of the current branch at a known location
- self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{test_branch}"
+ 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. )
- self.scm.exec "fetch origin +refs/pull/#{pull_id}/merge:#{EnvironmentManager.danger_head_branch}"
+ self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
end
def fetch_details
self.pr_json = client.pull_request(ci_source.repo_slug, ci_source.pull_request_id)
fetch_issue_details(self.pr_json)
@@ -104,26 +104,35 @@
submit_pull_request_status!(warnings: warnings,
errors: errors,
details_url: comment_result['html_url'])
end
- def submit_pull_request_status!(warnings: nil, errors: nil, details_url: nil)
+ def submit_pull_request_status!(warnings: [], errors: [], details_url: [])
status = (errors.count == 0 ? 'success' : 'failure')
message = generate_github_description(warnings: warnings, errors: errors)
- client.create_status(ci_source.repo_slug, latest_pr_commit_ref, status, {
- description: message,
- context: "danger/danger",
- target_url: details_url
- })
- rescue
- # 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
- abort("\nDanger has failed this build. \nFound #{'error'.danger_pluralize(errors.count)} and I don't have write access to the PR set a PR status.")
- else
- puts message
+
+ 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: message,
+ context: "danger/danger",
+ target_url: details_url
+ })
+ rescue
+ # 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
+ abort("\nDanger has failed this build. \nFound #{'error'.danger_pluralize(errors.count)} and I don't have write access to the PR set a PR status.")
+ else
+ puts message
+ end
end
end
# Get rid of the previously posted comment, to only have the latest one
def delete_old_comments!(except: nil, danger_id: 'danger')