class Startling::Github::Api

Attributes

lock[R]
repositories[R]

Public Class Methods

new() click to toggle source
# File lib/startling/github/api.rb, line 8
def initialize
  @lock = Mutex.new
  @repositories = {}
end

Public Instance Methods

labels_for_issue(repo_name: nil, issue_id: nil) click to toggle source
# File lib/startling/github/api.rb, line 40
def labels_for_issue(repo_name: nil, issue_id: nil)
  octokit.labels_for_issue(repo_name, issue_id)
end
open_pull_request(title: nil, body: nil, branch: nil, destination_branch: nil, repo_name: nil) click to toggle source
# File lib/startling/github/api.rb, line 30
def open_pull_request(title: nil, body: nil, branch: nil,
  destination_branch: nil, repo_name: nil)
  response = octokit.create_pull_request(repo_name, destination_branch, branch, title, body)
  response.data
rescue Octokit::UnprocessableEntity => e
  puts "Failed to open pull request, it may be open already"
  p e
  nil
end
pull_request(repo_name, branch) click to toggle source
# File lib/startling/github/api.rb, line 48
def pull_request(repo_name, branch)
  repository(repo_name).pull_request(branch)
end
pull_requests(repo) click to toggle source
# File lib/startling/github/api.rb, line 13
def pull_requests(repo)
  raw = octokit.pull_requests(repo, state: 'open')
  Parallel.map(raw, in_threads: raw.count) do |pull|
    PullRequest.new(pull).tap do |pull_request|
      pull_request.labels = labels_for_issue(repo_name: repo, issue_id: pull_request.id)
    end
  end
end
repository(name) click to toggle source
# File lib/startling/github/api.rb, line 22
def repository(name)
  Repo.new name, self
end
repository_attributes(name) click to toggle source
# File lib/startling/github/api.rb, line 26
def repository_attributes(name)
  octokit.repository(name)
end
set_labels_for_issue(repo_name: nil, issue_id: nil, labels: nil) click to toggle source
# File lib/startling/github/api.rb, line 44
def set_labels_for_issue(repo_name: nil, issue_id: nil, labels: nil)
  octokit.replace_all_labels(repo_name, issue_id, labels)
end

Private Instance Methods

access_token() click to toggle source
# File lib/startling/github/api.rb, line 75
def access_token
  Startling.cache.fetch('.github_access_token') do
    client = Octokit::Client.new(login: prompt_for_login, password: prompt_for_password)
    authorization_opts = {}
    authorization_opts[:scopes] = ["repo"]
    authorization_opts[:note] = "startling on #{`echo $HOSTNAME`}"
    begin
      client.create_authorization(authorization_opts)[:token]
    rescue Octokit::OneTimePasswordRequired
      authorization_opts[:headers] = { "X-GitHub-OTP" => prompt_for_otp }
      retry
    rescue Octokit::Unauthorized
      puts "Invalid username or password, try again."
      retry
    end
  end
end
build_octokit() click to toggle source
# File lib/startling/github/api.rb, line 61
def build_octokit
  stack = faraday_builder_class.new do |builder|
    #builder.response :logger
    builder.use Octokit::Response::RaiseError
    builder.adapter Faraday.default_adapter
  end
  Octokit.middleware = stack
  Octokit::Client.new access_token: access_token
end
faraday_builder_class() click to toggle source
# File lib/startling/github/api.rb, line 71
def faraday_builder_class
  defined?(Faraday::RackBuilder) ? Faraday::RackBuilder : Faraday::Builder
end
octokit() click to toggle source
# File lib/startling/github/api.rb, line 55
def octokit
  lock.synchronize do
    @octokit ||= build_octokit
  end
end
prompt_for_login() click to toggle source
# File lib/startling/github/api.rb, line 93
def prompt_for_login
  ask("Enter your Github username:  ")
end
prompt_for_otp() click to toggle source
# File lib/startling/github/api.rb, line 101
def prompt_for_otp
  ask("Enter your one time password:  ")
end
prompt_for_password() click to toggle source
# File lib/startling/github/api.rb, line 97
def prompt_for_password
  ask("Enter your Github password:  ") { |q| q.echo = false }
end