Sha256: 1511403545b28f9015ae7234921e5c1f10829eae873ec8bd4c0411281da33e6c

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "octokit"
require "json"

module Bundler
  module Alive
    module Client
      #
      # API Client for GitHub API
      #
      module GitHubApi
        ACCESS_TOKEN_ENV_NAME = "BUNDLER_ALIVE_GITHUB_TOKEN"

        #
        # Creates a GitHub client
        #
        # @return [Octokit::Client]
        #
        def create_client
          Octokit::Client.new(access_token: ENV.fetch(ACCESS_TOKEN_ENV_NAME, nil))
        end

        #
        # Returns repository URL is archived?
        #
        # @param [SourceCodeRepositoryUrl] repository_url
        #
        # @raise [ArgumentError]
        #   when repository_uri is not `SourceCodeRepositoryUrl`
        #
        # @raise [Octokit::TooManyRequests]
        #   when too many requested to GitHub.com
        #
        # @raise [SourceCodeClient::SearchRepositoryError]
        #   when Error without `Octokit::TooManyRequests`
        #
        # @return [Boolean]
        #
        # rubocop:disable Metrics/MethodLength
        def archived?(repository_url)
          unless repository_url.instance_of?(SourceCodeRepositoryUrl)
            raise ArgumentError, "UnSupported url: #{repository_url}"
          end

          query = "repo:#{slug(repository_url.url)}"

          begin
            result = @client.search_repositories(query)
            result[:items][0][:archived]
          rescue Octokit::TooManyRequests => e
            raise SourceCodeClient::RateLimitExceededError, e.message
          rescue StandardError => e
            raise SourceCodeClient::SearchRepositoryError, e.message
          end
        end
        # rubocop:enable Metrics/MethodLength

        #
        # Returns slug of repository URL
        #
        # @param [String] repository_url
        #
        # @return [String]
        #
        def slug(repository_url)
          Octokit::Repository.from_url(repository_url).slug
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bundler-alive-0.1.2 lib/bundler/alive/client/git_hub_api.rb
bundler-alive-0.1.1 lib/bundler/alive/client/git_hub_api.rb
bundler-alive-0.1.0 lib/bundler/alive/client/git_hub_api.rb