class GithubFlowCli::API

Public Class Methods

authorize(username, password, two_factor_token: nil) click to toggle source

@return [String] OAuth token

# File lib/github_flow_cli/api.rb, line 8
def authorize(username, password, two_factor_token: nil)
  client = Octokit::Client.new(login: username, password: password)
  auth_config = {
    scopes: ['user', 'repo', 'admin:repo_hook'],
    note: app_name,
  }
  if two_factor_token
    auth_config.merge!(:headers => { "X-GitHub-OTP" => two_factor_token })
  end
  client.create_authorization(auth_config).token
rescue Octokit::UnprocessableEntity => ex
  if ex.message =~ /already_exists/
    id = client.authorizations.find { |auth| auth[:note] == app_name }.id
    client.delete_authorization(id)
    retry
  else
    raise
  end
end
create_issue(title, body = nil, options = {}) click to toggle source

TODO: explicify options :assignee (String) — User login. :milestone (Integer) — Milestone number. :labels TODO: keywordify parameters

# File lib/github_flow_cli/api.rb, line 54
def create_issue(title, body = nil, options = {})
  @client.create_issue(Local.repo, title, body, options)
end
create_pr(base: "master", title: nil, body: nil) click to toggle source

TODO: other options

# File lib/github_flow_cli/api.rb, line 59
def create_pr(base: "master", title: nil, body: nil)
  branch_name = Local.git.current_branch
  issue_number = Config.branch_issue_map[branch_name]
  if issue_number
    issue = API.issue(Local.repo, issue_number)
    title ||= issue.title
    body ||= issue.body
  end
  unless Local.git.branches.remote.find { |b| b.name == branch_name }
    # TODO: custom default remote
    puts "no remote branch found, creating remote branch..."
    Local.git.config("branch.#{branch_name}.remote", 'origin')
    Local.git.config("branch.#{branch_name}.merge", "refs/heads/#{branch_name}")
  end
  # TODO: custom default remote
  puts "git push..."
  Local.git.push('origin', branch_name)
  @client.create_pull_request(Local.repo, base, branch_name, title, body).tap do |pr|
    Config.link_pr_to_branch(pr, branch_name)
  end
end
method_missing(method, *args, &block) click to toggle source

delegate API calls to Octokit::Client

Calls superclass method
# File lib/github_flow_cli/api.rb, line 39
def method_missing(method, *args, &block)
  if @client.respond_to?(method)
    define_singleton_method(method) do |*args, &block|
      @client.send(method, *args, &block)
    end
    return send(method, *args, &block)
  end
  super
end
use_oauth_token(token) click to toggle source
# File lib/github_flow_cli/api.rb, line 28
def use_oauth_token(token)
  @client = Octokit::Client.new(access_token: token)
end
valid?() click to toggle source
# File lib/github_flow_cli/api.rb, line 32
def valid?
  !user[:login].nil?
rescue Octokit::Unauthorized
  false
end

Private Class Methods

app_name() click to toggle source
# File lib/github_flow_cli/api.rb, line 83
def app_name
  "hubflow for #{`whoami`.strip}@#{`hostname`.strip}"
end