Class: Danger::RequestSources::GitHub
- Inherits:
-
RequestSource
- Object
- RequestSource
- Danger::RequestSources::GitHub
- Includes:
- Helpers::CommentsHelper
- Defined in:
- lib/danger/request_source/github.rb
Constant Summary
Constants inherited from RequestSource
RequestSource::DANGER_REPO_NAME
Instance Attribute Summary collapse
-
#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.
Attributes inherited from RequestSource
#ci_source, #environment, #ignored_violations
Instance Method Summary collapse
- #client ⇒ Object
-
#danger_repo?(organisation: nil, repository: nil) ⇒ Bool
Is this repo the danger repo of the org?.
-
#delete_old_comments!(except: nil, danger_id: "danger") ⇒ Object
Get rid of the previously posted comment, to only have the latest one.
-
#fetch_danger_repo(organisation: nil) ⇒ Hash
With the information about the repo.
- #fetch_details ⇒ Object
- #fetch_issue_details(pr_json) ⇒ Object
-
#fetch_repository(organisation: nil, repository: nil) ⇒ Hash
With the information about the repo returns nil if the repo is not available.
-
#file_url(organisation: nil, repository: nil, branch: "master", path: nil) ⇒ String
A URL to the specific file, ready to be downloaded.
- #host ⇒ Object
- #ignored_violations_from_pr(pr_json) ⇒ Object
-
#initialize(ci_source, environment) ⇒ GitHub
constructor
A new instance of GitHub.
-
#organisation ⇒ String
The organisation name, is nil if it can’t be detected.
- #pr_diff ⇒ Object
- #scm ⇒ Object
- #setup_danger_branches ⇒ Object
- #submit_pull_request_status!(warnings: [], errors: [], details_url: []) ⇒ Object
-
#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger") ⇒ Object
Sending data to GitHub.
Methods included from Helpers::CommentsHelper
#generate_comment, #generate_description, #markdown_parser, #parse_comment, #parse_tables_from_comment, #process_markdown, #random_compliment, #table, #table_kind_from_title, #violations_from_table
Methods inherited from RequestSource
available_request_sources, inherited, #validates_as_ci?
Constructor Details
#initialize(ci_source, environment) ⇒ GitHub
Returns a new instance of GitHub.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/danger/request_source/github.rb', line 12 def initialize(ci_source, environment) self.ci_source = ci_source 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"] end end |
Instance Attribute Details
#issue_json ⇒ Object
Returns the value of attribute issue_json.
10 11 12 |
# File 'lib/danger/request_source/github.rb', line 10 def issue_json @issue_json end |
#pr_json ⇒ Object
Returns the value of attribute pr_json.
10 11 12 |
# File 'lib/danger/request_source/github.rb', line 10 def pr_json @pr_json end |
#support_tokenless_auth ⇒ Object
Returns the value of attribute support_tokenless_auth.
10 11 12 |
# File 'lib/danger/request_source/github.rb', line 10 def support_tokenless_auth @support_tokenless_auth end |
Instance Method Details
#client ⇒ Object
32 33 34 35 |
# File 'lib/danger/request_source/github.rb', line 32 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 |
#danger_repo?(organisation: nil, repository: nil) ⇒ Bool
Returns is this repo the danger repo of the org?.
185 186 187 188 189 190 |
# File 'lib/danger/request_source/github.rb', line 185 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 |
#delete_old_comments!(except: nil, danger_id: "danger") ⇒ Object
Get rid of the previously posted comment, to only have the latest one
147 148 149 150 151 152 153 154 |
# File 'lib/danger/request_source/github.rb', line 147 def delete_old_comments!(except: nil, danger_id: "danger") issues = client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id) issues.each do |issue| next unless danger_issue?(issue, danger_id) next if issue[:id] == except client.delete_comment(ci_source.repo_slug, issue[:id]) end end |
#fetch_danger_repo(organisation: nil) ⇒ Hash
With the information about the repo. This will automatically detect if the repo is capitalised returns nil if there is no danger repo
177 178 179 180 181 182 |
# File 'lib/danger/request_source/github.rb', line 177 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 |
#fetch_details ⇒ Object
54 55 56 57 58 |
# File 'lib/danger/request_source/github.rb', line 54 def fetch_details self.pr_json = client.pull_request(ci_source.repo_slug, ci_source.pull_request_id) fetch_issue_details(self.pr_json) self.ignored_violations = ignored_violations_from_pr(self.pr_json) end |
#fetch_issue_details(pr_json) ⇒ Object
66 67 68 69 |
# File 'lib/danger/request_source/github.rb', line 66 def fetch_issue_details(pr_json) href = pr_json[:_links][:issue][:href] self.issue_json = client.get(href) end |
#fetch_repository(organisation: nil, repository: nil) ⇒ Hash
With the information about the repo returns nil if the repo is not available
166 167 168 169 170 171 172 |
# File 'lib/danger/request_source/github.rb', line 166 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 |
#file_url(organisation: nil, repository: nil, branch: "master", path: nil) ⇒ String
Returns A URL to the specific file, ready to be downloaded.
193 194 195 196 |
# File 'lib/danger/request_source/github.rb', line 193 def file_url(organisation: nil, repository: nil, branch: "master", path: nil) organisation ||= self.organisation "https://raw.githubusercontent.com/#{organisation}/#{repository}/#{branch}/#{path}" end |
#host ⇒ Object
28 29 30 |
# File 'lib/danger/request_source/github.rb', line 28 def host @host = @environment["DANGER_GITHUB_HOST"] || "github.com" end |
#ignored_violations_from_pr(pr_json) ⇒ Object
60 61 62 63 64 |
# File 'lib/danger/request_source/github.rb', line 60 def ignored_violations_from_pr(pr_json) pr_body = pr_json[:body] return [] if pr_body.nil? pr_body.chomp.scan(/>\s*danger\s*:\s*ignore\s*"(.*)"/i).flatten end |
#organisation ⇒ String
Returns The organisation name, is nil if it can’t be detected.
157 158 159 160 161 162 |
# File 'lib/danger/request_source/github.rb', line 157 def organisation matched = self.issue_json[:repository_url].match(%r{repos\/(.*)\/}) return matched[1] if matched && matched[1] rescue nil end |
#pr_diff ⇒ Object
37 38 39 |
# File 'lib/danger/request_source/github.rb', line 37 def pr_diff @pr_diff ||= client.pull_request(ci_source.repo_slug, ci_source.pull_request_id, accept: "application/vnd.github.v3.diff") end |
#scm ⇒ Object
24 25 26 |
# File 'lib/danger/request_source/github.rb', line 24 def scm @scm ||= GitRepo.new end |
#setup_danger_branches ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/danger/request_source/github.rb', line 41 def setup_danger_branches # we can use a github specific feature here: base_commit = self.pr_json[:base][:sha] 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 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 "branch #{EnvironmentManager.danger_head_branch} #{head_commit}" end |
#submit_pull_request_status!(warnings: [], errors: [], details_url: []) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/danger/request_source/github.rb', line 112 def submit_pull_request_status!(warnings: [], errors: [], details_url: []) 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", 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 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 end end end |
#update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger") ⇒ Object
Sending data to GitHub
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/danger/request_source/github.rb', line 72 def update_pull_request!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger") comment_result = {} issues = client.issue_comments(ci_source.repo_slug, ci_source.pull_request_id) editable_issues = issues.select { |issue| danger_issue?(issue, danger_id) } if editable_issues.empty? previous_violations = {} else comment = editable_issues.first[:body] previous_violations = parse_comment(comment) end if previous_violations.empty? && (warnings + errors + + markdowns).empty? # Just remove the comment, if there's nothing to say. delete_old_comments!(danger_id: danger_id) else body = generate_comment(warnings: warnings, errors: errors, messages: , markdowns: markdowns, previous_violations: previous_violations, danger_id: danger_id, template: "github") if editable_issues.empty? comment_result = client.add_comment(ci_source.repo_slug, ci_source.pull_request_id, body) else original_id = editable_issues.first[:id] comment_result = client.update_comment(ci_source.repo_slug, original_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"]) end |