Class: GithubCom

Inherits:
Website show all
Defined in:
lib/license_auto/website/github_com.rb

Constant Summary collapse

HOST =
'github.com'
LANGUAGE =
nil
GIT_HASH_LENGTH =
40

Instance Method Summary collapse

Methods inherited from Website

#to_s

Constructor Details

#initialize(package, user, repo, ref = nil) ⇒ GithubCom

package: Hashie::Mash user: string repo: string ref: string



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/license_auto/website/github_com.rb', line 24

def initialize(package, user, repo, ref=nil)
  super(package)
  @ref = ref

  LicenseAuto.logger.debug("#{user}/#{repo}, #{@ref}")

  @server =
    begin
      eval('WebMock')
      LicenseAuto.logger.debug("LicenseAuto under RSpec mode")
      Github.new(user: user, repo: repo)
    rescue NameError => e
      LicenseAuto.logger.debug("LicenseAuto under running mode")
      basic_auth = "#{LUTO_CONF.github.username}:#{LUTO_CONF.github.access_token}"
      Github.new(user: user, repo: repo, basic_auth: basic_auth)
    end
end

Instance Method Details

#cloneObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/license_auto/website/github_com.rb', line 125

def clone
  info = repo_info

  clone_url = info.body.fetch('clone_url')
  LicenseAuto.logger.debug(clone_url)

  trimmed_url = clone_url.gsub(/^http[s]?:\/\//, '')
  clone_dir = "#{LUTO_CACHE_DIR}/#{trimmed_url}"
  LicenseAuto.logger.debug(clone_dir)

  if Dir.exists?(clone_dir)
    git = Git.open(clone_dir, :log => LicenseAuto.logger)
    local_branch = git.branches.local[0].full
    if local_branch == @ref
      git.pull(remote='origin', branch=local_branch)
    else
      FileUtils::rm_rf(clone_dir)
      do_clone(clone_url, clone_dir)
    end
  else
    do_clone(clone_url, clone_dir)
  end
  clone_dir
end

#do_clone(clone_url, clone_dir) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/license_auto/website/github_com.rb', line 150

def do_clone(clone_url, clone_dir)
  LicenseAuto.logger.debug(@ref)
  clone_opts = {
      :depth => 1, # Only last commit history for fast
      :branch => @ref
  }
  LicenseAuto.logger.debug(clone_url)
  cloned_repo = Git.clone(clone_url, clone_dir, clone_opts)
end

#filter_gitmodulesObject



164
165
# File 'lib/license_auto/website/github_com.rb', line 164

def filter_gitmodules
end

#get_blobs(sha) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/license_auto/website/github_com.rb', line 168

def get_blobs(sha)
  response_wrapper = @server.git_data.blobs.get(@server.user, @server.repo, sha)
  LicenseAuto.logger.debug(response_wrapper)
  content = response_wrapper.body.content
  encoding = response_wrapper.body.encoding
  if encoding == 'base64'
    Base64.decode64(content)
  else
    LicenseAuto.logger.error("Unknown encoding: #{encoding}")
  end
end

#get_license_infoObject

Returns LicenseInfoWrapper.

Returns:

  • LicenseInfoWrapper



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/license_auto/website/github_com.rb', line 45

def get_license_info()
  possible_ref = @ref || match_versioned_ref
  # If possible_ref is nil, the Github API server will return the default branch contents
  contents = @server.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
  }

  license_files = license_files.map {|obj|
    license_content = get_blobs(obj['sha'])
    license_name, sim_ratio = LicenseAuto::Similarity.new(license_content).most_license_sim
    _hash = {
      name: license_name,
      sim_ratio: sim_ratio,
      html_url: obj['html_url'],
      download_url: obj['download_url'],
      text: license_content
    }
    LicenseAuto::LicenseWrapper.new(_hash)
  }

  readme_files = readme_files.map {|obj|
    readme_content = get_blobs(obj['sha'])
    license_content = LicenseAuto::Readme.new(obj['download_url'], readme_content).license_content
    LicenseAuto.logger.debug(license_content)
    if license_content.nil?
      next
    else
      license_name, sim_ratio = LicenseAuto::Similarity.new(license_content).most_license_sim
      _hash = {
          name: license_name,
          sim_ratio: sim_ratio,
          html_url: obj['html_url'],
          download_url: obj['download_url'],
          text: license_content
      }
      LicenseAuto::LicenseWrapper.new(_hash)
    end
  }.compact!

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

#get_ref(ref) ⇒ Object



97
98
99
# File 'lib/license_auto/website/github_com.rb', line 97

def get_ref(ref)
  @server.git_data.references.get(ref: ref)
end

#list_languagesObject



119
120
121
122
123
# File 'lib/license_auto/website/github_com.rb', line 119

def list_languages
  langs = @server.repos.languages
  LicenseAuto.logger.debug("All languaegs: #{langs}")
  langs
end

#match_versioned_refObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/license_auto/website/github_com.rb', line 101

def match_versioned_ref()
  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)
    @server.repos.tags do |tag|
      matched = matcher.match_the_ref(tag.name)
      if matched
        possible_ref = tag.name
        break
      end
    end
  end
  possible_ref
end

#repo_infoObject



160
161
162
# File 'lib/license_auto/website/github_com.rb', line 160

def repo_info
  @server.repos.get
end