Module: LicenseAuto

Included in:
Package
Defined in:
lib/license_auto.rb,
lib/license_auto/matcher.rb,
lib/license_auto/package.rb,
lib/license_auto/version.rb,
lib/license_auto/license_info.rb,
lib/license_auto/website/github.rb

Defined Under Namespace

Modules: Matcher, VERSION Classes: LicenseInfo, Package

Constant Summary collapse

GIT_HASH_LENGTH =

@owner: string @repo: string @pack_version: string

Returns:

  • LicenseInfo

40

Class Method Summary collapse

Class Method Details

.gem_versionObject

Returns the version of the currently loaded Rails as a Gem::Version



3
4
5
# File 'lib/license_auto/version.rb', line 3

def self.gem_version
  Gem::Version.new VERSION::STRING
end

.github_get_license_info(owner, repo, package_version = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/license_auto/website/github.rb', line 14

def self.github_get_license_info(owner, repo, package_version=nil)
  github = Github.new(user: owner, repo: repo)

  possible_ref = nil
  # If provided a Git SHA, use it directly
  if package_version.size >= GIT_HASH_LENGTH
    possible_ref = package_version
  else
    matcher = LicenseAuto::Matcher::FilepathName.new(package_version)
    github.repos.tags do |tag|
      matched = matcher.match_the_ref(tag.name)
      if matched
        possible_ref = tag.name
        break
      end
    end
  end

  # If possible_ref is nil, the Github API server will return the default branch contents
  contents = github.repos.contents.get(path: '/', ref: possible_ref)

  license_files = []
  readme_files = []
  notice_files = []
  contents.each {|obj|
    if obj.type == 'file'
      filename_matcher = LicenseAuto::Matcher::FilepathName.new(obj.name)
      license_files.push(obj) if filename_matcher.match_license_file
      readme_files.push(obj) if filename_matcher.match_readme_file
      notice_files.push(obj) if filename_matcher.match_notice_file
    end
  }

  LicenseAuto::LicenseInfo.new(licenses: license_files, readmes: readme_files, notices: notice_files)
end