Class: Pra::GithubPullSource
- Inherits:
-
PullSource
- Object
- PullSource
- Pra::GithubPullSource
- Defined in:
- lib/pra/github_pull_source.rb
Instance Method Summary collapse
- #collect_exclusions ⇒ Object
- #excluded?(org, repository) ⇒ Boolean
- #excluded_repos ⇒ Object
- #extract_repository_from_html_url(html_url) ⇒ Object
- #fetch_pull_requests ⇒ Object
- #get_all_pull_requests ⇒ Object
-
#initialize(config = {}) ⇒ GithubPullSource
constructor
A new instance of GithubPullSource.
- #organizations ⇒ Object
- #pull_requests ⇒ Object
- #repos_for_query ⇒ Object
- #repositories ⇒ Object
- #rest_api_search_issues_url ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ GithubPullSource
Returns a new instance of GithubPullSource.
9 10 11 12 13 14 |
# File 'lib/pra/github_pull_source.rb', line 9 def initialize(config = {}) @ratelimit_remaining = 5000 @ratelimit_limit = 5000 @ratelimit_reset = nil super(config) end |
Instance Method Details
#collect_exclusions ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/pra/github_pull_source.rb', line 92 def collect_exclusions @exclusions = {} organizations.each do |org| @exclusions[org['name'].downcase] = org['exclude'] end @exclusions end |
#excluded?(org, repository) ⇒ Boolean
100 101 102 |
# File 'lib/pra/github_pull_source.rb', line 100 def excluded?(org, repository) excluded_repos[org.downcase] && excluded_repos[org.downcase].include?(repository) end |
#excluded_repos ⇒ Object
88 89 90 |
# File 'lib/pra/github_pull_source.rb', line 88 def excluded_repos @excluded_repos || collect_exclusions end |
#extract_repository_from_html_url(html_url) ⇒ Object
104 105 106 107 |
# File 'lib/pra/github_pull_source.rb', line 104 def extract_repository_from_html_url(html_url) /https:\/\/github.com\/(\w+)\/([\w-]+)/.match(html_url) return $1, $2 end |
#fetch_pull_requests ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/pra/github_pull_source.rb', line 20 def fetch_pull_requests pull_requests_json = "[]" conn = Faraday.new do |faraday| faraday.request :authorization, :basic, @config['username'], @config['password'] end # conn.basic_auth(@config['username'], @config['password']) resp = conn.get do |req| req.url rest_api_search_issues_url req.params['q'] = "is:pr is:open sort:updated-desc #{repos_for_query}" req.params['per_page'] = '300' req.headers['Content-Type'] = 'application/json' req.headers['Accept'] = 'application/json' end @ratelimit_reset = Time.at(resp.headers['x-ratelimit-reset'].to_i) @ratelimit_limit = resp.headers['x-ratelimit-limit'].to_i @ratelimit_remaining = resp.headers['x-ratelimit-remaining'].to_i Pra::Log.debug("Fetched pull requests and updated ratelimit tracking") Pra::Log.debug("Ratelimit Reset: #{@ratelimit_reset}") Pra::Log.debug("Ratelimit Limit: #{@ratelimit_limit}") Pra::Log.debug("Ratelimit Remaining: #{@ratelimit_remaining}") pull_requests_json = resp.body Pra::Log.debug(pull_requests_json) JSON.parse(pull_requests_json) end |
#get_all_pull_requests ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pra/github_pull_source.rb', line 46 def get_all_pull_requests pull_requests = [] pull_requests_hash = fetch_pull_requests pull_requests_hash['items'].each do |request| begin org, repository = extract_repository_from_html_url(request['html_url']) unless excluded?(org, repository) pull_requests << Pra::PullRequest.new(title: request["title"], from_reference: "", to_reference: "", author: request["user"]["login"], assignee: request["assignee"] ? request["assignee"]["login"] : nil, link: request['html_url'], service_id: 'github', repository: repository, updated_at: request["updated_at"], labels: request["labels"].collect{|l| l["name"]}.join(",")) end rescue StandardError => e Pra::Log.error("Error: #{e.to_s}") Pra::Log.error("Request: #{request.inspect}") end end pull_requests end |
#organizations ⇒ Object
117 118 119 |
# File 'lib/pra/github_pull_source.rb', line 117 def organizations @config["organizations"] || [] end |
#pull_requests ⇒ Object
16 17 18 |
# File 'lib/pra/github_pull_source.rb', line 16 def pull_requests return get_all_pull_requests end |
#repos_for_query ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pra/github_pull_source.rb', line 73 def repos_for_query query_params = [] repositories.each do |repo| query_params << "repo:#{repo['owner']}/#{repo['repository']}" end @excluded_repos = {} organizations.each do |org| query_params << "org:#{org['name']}" @excluded_repos[org['name'].downcase] = org['exclude'] end return query_params.join(" ") end |
#repositories ⇒ Object
113 114 115 |
# File 'lib/pra/github_pull_source.rb', line 113 def repositories @config["repositories"] || [] end |
#rest_api_search_issues_url ⇒ Object
109 110 111 |
# File 'lib/pra/github_pull_source.rb', line 109 def rest_api_search_issues_url "#{@config['protocol']}://#{@config['host']}/search/issues" end |