Sha256: e1105031e0b4bb5be1cb1136c4b1d6f33b31c4ec895403d4653959f955bf132b

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

require 'singleton'
require 'octokit'
require 'straight_line/common/user_error'
require 'straight_line/common/git_commands/config'

# Github API wrapper
class Github
  include Singleton

  def initialize
    @client = nil
  end

  def self.make_class(name)
    class << self
      self
    end.instance_eval do
      define_method name do |*args|
        instance.send name, *args
      end
    end
  end

  def login; end

  def list_repos
    @client.repositories query: { type: 'private' }
  end

  def user
    @client.user
  end

  def ensure_logged_in
    raise UserError, 'Must be logged in first' unless @client
  end

  def client
    return @client if @client

    @client = Octokit::Client.new netrc: true, auto_paginate: true
  end

  def create_pull_request(branch, title, body)
    repo = repo_name
    client.create_pull_request repo,
                               'master', "#{github_login}:#{branch}", title, body
  end
  make_class :create_pull_request

  def repo_name
    cmd = GitCommands::Config.new('remote.origin.url')
    remote = cmd.run
    remote.match(%r{(git@github.com:)(.*/.*)\.git})[2]
  end

  def github_login
    client.user.login
  end
  make_class :github_login

  def pull_requests
    client.pull_requests repo_name
  end
  make_class :pull_requests

  def pull_request_for_feature(feature)
    prs = pull_requests
    prs.find do |p|
      p.head.ref == feature &&
        p.head.user.login == Github.github_login &&
        p.base.ref == 'master'
    end
  end
  make_class :pull_request_for_feature
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
straight_line-0.1.4.0 lib/straight_line/common/github.rb
straight_line-0.1.3.0 lib/straight_line/common/github.rb
straight_line-0.1.2.0 lib/straight_line/common/github.rb