Class: Hubstats::GithubAPI
- Inherits:
-
Object
- Object
- Hubstats::GithubAPI
- Includes:
- HubHelper
- Defined in:
- lib/hubstats/github_api.rb
Class Method Summary collapse
-
.add_labels(repo) ⇒ Object
Public: Gets all the labels for a repo, adds all labels to a pull.
- .client(options = {}) ⇒ Object
- .configure(options = {}) ⇒ Object
- .create_hook(repo) ⇒ Object
-
.delete_hook(repo, old_endpoint) ⇒ Object
Public: Delete webhook from github repository.
-
.get_labels(repo) ⇒ Object
Public: gets labels for a particular label.
-
.get_labels_for_pull(repo_name, pull_request_number) ⇒ Object
Public: gets the label for a given pull request.
-
.get_repos ⇒ Object
Public: Gets repos found in configuration file.
-
.inline(repo_name, kind, options = {}) ⇒ Object
Public: Gets all of a specific kind from a repo, and processes them.
- .route(object, kind, repo_name = nil) ⇒ Object
-
.update_hook(repo, old_endpoint = nil) ⇒ Object
Public: updates a hook if it exists, otherwise creates one.
-
.update_pulls ⇒ Object
Public: Gets extra information on pull requests, e.g size, additions …
- .wait_limit(grab_size, rate_limit) ⇒ Object
Methods included from HubHelper
comment_setup, get_pull_number, pull_setup
Class Method Details
.add_labels(repo) ⇒ Object
Public: Gets all the labels for a repo, adds all labels to a pull
repo - the particular repository, you want to add labels to
170 171 172 173 174 |
# File 'lib/hubstats/github_api.rb', line 170 def self.add_labels(repo) get_labels(repo).each do |label| inline(repo.full_name,'issues', labels: label.name, state: 'all') end end |
.client(options = {}) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/hubstats/github_api.rb', line 18 def self.client(={}) configure(Hubstats.config.github_auth) if @@auth_info.nil? ent = Octokit::Client.new(@@auth_info.merge()) ent.user #making sure it was configured properly return ent end |
.configure(options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/hubstats/github_api.rb', line 7 def self.configure(={}) @@auth_info = {} if access_token = ENV['GITHUB_API_TOKEN'] || ["access_token"] @@auth_info[:access_token] = access_token else @@auth_info[:client_id] = ENV['CLIENT_ID'] || ["client_id"] @@auth_info[:client_secret] = ENV['CLIENT_SECRET'] || ["client_secret"] end @@auth_info end |
.create_hook(repo) ⇒ Object
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 |
# File 'lib/hubstats/github_api.rb', line 79 def self.create_hook(repo) begin client.create_hook( repo.full_name, 'web', { :url => Hubstats.config.webhook_endpoint, :content_type => 'json', :secret => Hubstats.config.webhook_secret }, { :events => [ 'pull_request', 'pull_request_review_comment', 'commit_comment', 'issues', 'issue_comment', 'member' ], :active => true } ) puts "Hook on #{repo.full_name} successfully created" rescue Octokit::UnprocessableEntity puts "Hook on #{repo.full_name} already existed" rescue Octokit::NotFound puts "You don't have sufficient privileges to add an event hook to #{repo.full_name}" end end |
.delete_hook(repo, old_endpoint) ⇒ Object
Public: Delete webhook from github repository
repo - a repo github object old_endpoint - A string of the previous endpoint
Return - nothing
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/hubstats/github_api.rb', line 115 def self.delete_hook(repo, old_endpoint) begin client.hooks(repo.full_name).each do |hook| if hook[:config][:url] == old_endpoint Hubstats::GithubAPI.client.remove_hook(repo.full_name, hook[:id]) puts "successfully deleted hook with id #{hook[:id]} and url #{hook[:config][:url]} from #{repo.full_name}" end end rescue Octokit::NotFound puts "You don't have sufficient privileges to remove an event hook to #{repo.full_name}" end end |
.get_labels(repo) ⇒ Object
Public: gets labels for a particular label
repo - a repo github object
Returns - all the labels for a given repo
144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/hubstats/github_api.rb', line 144 def self.get_labels(repo) labels = [] octo = Hubstats::GithubAPI.client({:auto_paginate => true}) github_labels = octo.labels(repo.full_name) github_labels.each do |label| label_hash = label.to_h if label.respond_to? :to_h label_data = label_hash.slice(*Hubstats::Label.column_names.map(&:to_sym)) labels << Hubstats::Label.where(:name => label_data[:name]).first_or_create(label_data) end labels end |
.get_labels_for_pull(repo_name, pull_request_number) ⇒ Object
Public: gets the label for a given pull request
repo_name - a the repo_name pull_request_number - the number of the pull_request you want labels of
Returns - the issue
162 163 164 165 |
# File 'lib/hubstats/github_api.rb', line 162 def self.get_labels_for_pull(repo_name, pull_request_number) issue = client.issue(repo_name, pull_request_number) issue[:labels] end |
.get_repos ⇒ Object
Public: Gets repos found in configuration file
Returns - an array of github repo objects
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hubstats/github_api.rb', line 44 def self.get_repos if Hubstats.config.github_config.has_key?("org_name") repos = client.organization_repositories(Hubstats.config.github_config["org_name"]) else repos = [] Hubstats.config.github_config["repo_list"].each do |repo| repos << client.repository(repo) end end repos end |
.inline(repo_name, kind, options = {}) ⇒ Object
Public: Gets all of a specific kind from a repo, and processes them.
repo_name - the name of a repo kind - a kind of object (pull,comment) options - any possible option a particular kind of object
Returns an array of that particular kind
32 33 34 35 36 37 38 39 |
# File 'lib/hubstats/github_api.rb', line 32 def self.inline(repo_name, kind, ={}) path = ["repos",repo_name,kind].join('/') octo = client({:auto_paginate => true }) octo.paginate(path, ) do |data, last_response| last_response.data.each{|v| route(v,kind,repo_name)}.clear wait_limit(1,octo.rate_limit) end.each{|v| route(v,kind,repo_name)}.clear end |
.route(object, kind, repo_name = nil) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/hubstats/github_api.rb', line 183 def self.route(object, kind, repo_name = nil) if kind == "pulls/comments" repo = Hubstats::Repo.where(full_name: repo_name).first Hubstats::Comment.create_or_update(HubHelper.comment_setup(object,repo.id,"PullRequest")) elsif kind == "issues/comments" repo = Hubstats::Repo.where(full_name: repo_name).first Hubstats::Comment.create_or_update(HubHelper.comment_setup(object,repo.id,"Issue")) elsif kind == "comments" repo = Hubstats::Repo.where(full_name: repo_name).first Hubstats::Comment.create_or_update(HubHelper.comment_setup(object,repo.id,"Commit")) elsif kind == "pulls" Hubstats::PullRequest.create_or_update(HubHelper.pull_setup(object)) elsif kind == "issues" if object[:pull_request] repo = Hubstats::Repo.where(full_name: repo_name).first pull_request = Hubstats::PullRequest.where(repo_id: repo.id).where(number: object[:number]).first pull_request.add_labels(object[:labels]) end end end |
.update_hook(repo, old_endpoint = nil) ⇒ Object
Public: updates a hook if it exists, otherwise creates one
repo - a repo github object old_endpoint - A string of the previous endpoint
Returns - the new hook
134 135 136 137 |
# File 'lib/hubstats/github_api.rb', line 134 def self.update_hook(repo, old_endpoint = nil) delete_hook(repo, old_endpoint) unless old_endpoint == nil create_hook(repo) end |
.update_pulls ⇒ Object
Public: Gets extra information on pull requests, e.g size, additions …
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/hubstats/github_api.rb', line 57 def self.update_pulls grab_size = 250 begin while Hubstats::PullRequest.where(deletions: nil).where(additions: nil).count > 0 client = Hubstats::GithubAPI.client incomplete = Hubstats::PullRequest.where(deletions: nil).where(additions: nil).limit(grab_size) incomplete.each do |pull| repo = Hubstats::Repo.where(id: pull.repo_id).first pr = client.pull_request(repo.full_name, pull.number) Hubstats::PullRequest.create_or_update(HubHelper.pull_setup(pr)) end wait_limit(grab_size,client.rate_limit) end puts "All Pull Requests are up to date" rescue Faraday::ConnectionFailed puts "Connection Timeout, restarting pull request updating" update_pulls end end |
.wait_limit(grab_size, rate_limit) ⇒ Object
176 177 178 179 180 181 |
# File 'lib/hubstats/github_api.rb', line 176 def self.wait_limit(grab_size,rate_limit) if rate_limit.remaining < grab_size puts "Hit Github rate limit, waiting #{Time.at(rate_limit.resets_in).utc.strftime("%H:%M:%S")} to get more" sleep(rate_limit.resets_in) end end |