lib/danger/request_source/github.rb in danger-0.9.1 vs lib/danger/request_source/github.rb in danger-0.10.0

- old
+ new

@@ -126,11 +126,16 @@ # 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.") + 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 set a PR status.") + else + abort("\nDanger has failed this build. \nFound #{'error'.danger_pluralize(errors.count)}.") + end else puts message end end end @@ -220,17 +225,59 @@ :message end end def parse_tables_from_comment(comment) - comment.split('</table>') + comment.split("</table>") end def process_markdown(violation) html = markdown_parser.render(violation.message) - match = html.match(%r{^<p>(.*)</p>$}) - message = match.nil? ? html : match.captures.first - Violation.new(message, violation.sticky) + # Remove the outer `<p>`, the -5 represents a newline + `</p>` + html = html[3...-5] if html.start_with? "<p>" + Violation.new(html, violation.sticky) + 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\/(.*)\/}) + 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? + 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 end end end