Sha256: 5e711f0ac6aeb2451ec6f721f631dde4a93e90f173056c4a724ebbf84271364b

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

require 'pra/pull_source'
require 'pra/pull_request'
require 'json'
require 'faraday'

module Pra
  class GithubPullSource < Pra::PullSource
    def pull_requests
      requests = []
      repositories.each do |repo_config|
        requests.concat(get_repo_pull_requests(repo_config))
      end
      return requests
    end

    def repositories
      @config["repositories"]
    end

    def get_repo_pull_requests(repository_config)
      requests = []
      JSON.parse(rest_api_pull_request_resource(repository_config)).each do |request|
        requests << Pra::PullRequest.new(title: request["title"], from_reference: request["head"]["label"], to_reference: request["base"]["label"], author: request["user"]["login"], assignee: request["assignee"] ? request["assignee"]["login"] : nil, link: request['html_url'], service_id: 'github', repository: repository_config["repository"])
      end
      return requests
    end

    def rest_api_pull_request_url(repository_config)
      "#{@config['protocol']}://#{@config['host']}/repos/#{repository_config["owner"]}/#{repository_config["repository"]}/pulls"
    end

    def rest_api_pull_request_resource(repository_config)
      conn = Faraday.new
      conn.basic_auth(@config['username'], @config['password'])
      resp = conn.get do |req|
        req.url rest_api_pull_request_url(repository_config)
        req.headers['Content-Type'] = 'application/json'
        req.headers['Accept'] = 'application/json'
      end
      resp.body
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pra-1.7.2 lib/pra/github_pull_source.rb
pra-1.7.1 lib/pra/github_pull_source.rb
pra-1.7.0 lib/pra/github_pull_source.rb
pra-1.6.0 lib/pra/github_pull_source.rb