Sha256: ace90aa5d09e33e2e29e5937a08aa898583b5cb18a77dc16d461f22327e08469

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# GitHub project
#
# Analyses a remote GitHub repository for license information
#
# Only the root directory of a repository will be scanned because every
# `#load_file(..)` call incurs a separate API request.

autoload :Octokit, 'octokit'

module Licensee
  module Projects
    class GitHubProject < Licensee::Projects::Project
      # If there's any trailing data (e.g. `.git`) this pattern will ignore it:
      # we're going to use the API rather than clone the repo.
      GITHUB_REPO_PATTERN =
        %r{https://github.com/([^\/]+\/([^\/]+(?=\.git)|[^\/]+)).*}

      class RepoNotFound < StandardError; end

      def initialize(github_url, **args)
        @repo = github_url[GITHUB_REPO_PATTERN, 1]
        raise ArgumentError, "Not a github URL: #{github_url}" unless @repo
        super(**args)
      end

      attr_reader :repo

      private

      def files
        @files ||= contents.map { |data| { name: data[:name], dir: '/' } }
      rescue Octokit::NotFound
        raise RepoNotFound,
              "Could not load GitHub repo #{repo}, it may be private or deleted"
      end

      def load_file(file)
        Octokit.contents(@repo, path:   file[:name],
                                accept: 'application/vnd.github.v3.raw')
      end

      def contents
        Octokit.contents(@repo).select { |data| data[:type] == 'file' }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
licensee-9.9.3 lib/licensee/projects/github_project.rb
licensee-9.9.2 lib/licensee/projects/github_project.rb
licensee-9.9.1 lib/licensee/projects/github_project.rb
licensee-9.9.0 lib/licensee/projects/github_project.rb
licensee-9.9.0.beta.3 lib/licensee/projects/github_project.rb