Sha256: b3f8a1d8d0e097ef5fd9c6ce5eab893cb77d546d4add0a6b04d578dada37211e

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require 'octokit'

module Git
  class Pr
    class GitHub
      class Failed < StandardError; end

      def initialize(git_properties)
        @git = git_properties

        Octokit.configure do |c|
          c.login = @git.login
          c.password = @git.api_token
          c.api_endpoint = @git.api_endpoint unless @git.api_endpoint.empty?
        end
      end

      def list_pull_requests(profile, mine)
        if profile
          repositories = profile == :all ? all_repositories
                                         : @git.repository_profile(profile)
        else
          repositories = [@git.repository]
        end

        [].tap do |prs|
          repositories.flatten.uniq.each do |repo|
            pull_requests = Octokit.pull_requests(repo, 'open')
            if mine
              prs << pull_requests.select { |pr| pr.user.login == @git.login }
            else
              prs << pull_requests
            end
          end
        end.flatten
      end

      def submit_pull_request(title, message)
        response = Octokit.create_pull_request(@git.repository,
                                               @git.base_branch,
                                               @git.current_branch,
                                               title,
                                               message)
        response.state == 'open' ? response : raise(Failed, response.state)
      rescue Octokit::UnprocessableEntity => e
        message = e.message.match(/message: (.*)/)[1].sub(/ \/\/.*/, '')
        raise Failed, message
      end

      private
      def all_repositories
        repositories = []

        Octokit.organizations.map(&:login).each do |org|
          repositories << Octokit.organization_repositories(org).map(&:full_name)
        end
        repositories << Octokit.repositories.map(&:full_name)

        repositories
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git-pr-0.0.3 lib/git/pr/github.rb