Sha256: ac22ed436d929685c742718f1d8ba47be91a66822346cff05efad8346290a14b

Contents?: true

Size: 1.6 KB

Versions: 11

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

require 'json'
require 'net/http'
require 'uri'

module Buildkite
  module Builder
    class Github
      BASE_URI = URI('https://api.github.com').freeze
      ACCEPT_HEADER = 'application/vnd.github.v3+json'
      LINK_HEADER = 'link'
      NEXT_LINK_REGEX = /<(?<uri>.+)>; rel="next"/.freeze
      REPO_REGEX = /github\.com(?::|\/)(.*)\.git\z/.freeze
      PER_PAGE = 100

      def self.pull_request_files
        new.pull_request_files
      end

      def initialize(env = ENV)
        @env = env
      end

      def pull_request_files
        files = []
        next_uri = URI.join(BASE_URI, "repos/#{repo}/pulls/#{pull_request_number}/files?per_page=#{PER_PAGE}")

        while next_uri
          response = request(next_uri)
          files.concat(JSON.parse(response.body))
          next_uri = parse_next_uri(response)
        end

        files
      end

      private

      def repo
        Buildkite.env.repo[REPO_REGEX, 1]
      end

      def token
        @env.fetch('GITHUB_API_TOKEN')
      end

      def pull_request_number
        Buildkite.env.pull_request
      end

      def request(uri)
        Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
          request = Net::HTTP::Get.new(uri)
          request['Authorization'] = "token #{token}"
          request['Accept'] = ACCEPT_HEADER

          http.request(request)
        end
      end

      def parse_next_uri(response)
        links = response[LINK_HEADER]
        return unless links

        matches = links.match(NEXT_LINK_REGEX)
        URI.parse(matches[:uri]) if matches
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
buildkite-builder-1.4.0 lib/buildkite/builder/github.rb
buildkite-builder-1.3.1 lib/buildkite/builder/github.rb
buildkite-builder-1.3.0 lib/buildkite/builder/github.rb
buildkite-builder-1.2.0 lib/buildkite/builder/github.rb
buildkite-builder-1.1.0 lib/buildkite/builder/github.rb
buildkite-builder-1.0.0 lib/buildkite/builder/github.rb
buildkite-builder-1.0.0.beta.5 lib/buildkite/builder/github.rb
buildkite-builder-1.0.0.beta.4 lib/buildkite/builder/github.rb
buildkite-builder-1.0.0.beta.3 lib/buildkite/builder/github.rb
buildkite-builder-1.0.0.beta.2 lib/buildkite/builder/github.rb
buildkite-builder-1.0.0.beta.1 lib/buildkite/builder/github.rb