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 Attribute Summary collapse

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



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

def initialize(package, user, repo, ref=nil)
  super(package)
  @ref = ref
  @url = "https://github.com/#{user}/#{repo}"
  LicenseAuto.logger.debug(@url)

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

Instance Attribute Details

#urlObject (readonly)

Returns the value of attribute url.



19
20
21
# File 'lib/license_auto/website/github_com.rb', line 19

def url
  @url
end

Instance Method Details

#cloneObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/license_auto/website/github_com.rb', line 150

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.exist?(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



175
176
177
178
179
180
181
182
183
# File 'lib/license_auto/website/github_com.rb', line 175

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)
  Git.clone(clone_url, clone_dir, clone_opts)
end

#filter_gitmodulesObject



189
190
# File 'lib/license_auto/website/github_com.rb', line 189

def filter_gitmodules
end

#get_blobs(sha) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/license_auto/website/github_com.rb', line 193

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



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
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/license_auto/website/github_com.rb', line 47

def get_license_info()
  possible_ref = @ref || match_versioned_ref
  LicenseAuto.logger.debug("possible_ref: #{possible_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
    LicenseAuto::LicenseWrapper.new(
      name: license_name,
      sim_ratio: sim_ratio,
      html_url: obj['html_url'],
      download_url: obj['download_url'],
      text: license_content
    )
  }

  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("readme_content:\n#{license_content}\n")
    if license_content.nil?
      next
    else
      license_name, sim_ratio = LicenseAuto::Similarity.new(license_content).most_license_sim
      LicenseAuto::LicenseWrapper.new(
          name: license_name,
          sim_ratio: sim_ratio,
          html_url: obj['html_url'],
          download_url: obj['download_url'],
          text: license_content
      )
    end
  }.compact

  pack_wrapper = LicenseAuto::PackWrapper.new(
      homepage: nil,
      project_url: nil,
      source_url: @url
  )
  LicenseAuto::LicenseInfoWrapper.new(
      licenses: license_files,
      readmes: readme_files,
      notices: notice_files,
      pack: pack_wrapper
  )
end

#get_ref(ref) ⇒ Object



108
109
110
# File 'lib/license_auto/website/github_com.rb', line 108

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

#latest_commitObject



146
147
148
# File 'lib/license_auto/website/github_com.rb', line 146

def latest_commit
  latest = list_commits.first
end

#list_commitsObject



142
143
144
# File 'lib/license_auto/website/github_com.rb', line 142

def list_commits
  commits = @server.repos.commits.list
end

#list_languagesObject



130
131
132
133
134
# File 'lib/license_auto/website/github_com.rb', line 130

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

#list_tagsObject

Array: [#<Hashie::Mash commit=#<Hashie::Mash sha=“8065e5c64a22bd6d60e4df8d9be46b5805ec9355” url=“api.github.com/repos/bower/bower/commits/8065e5c64a22bd6d60e4df8d9be46b5805ec9355”> name=“v1.7.9” tarball_url=“api.github.com/repos/bower/bower/tarball/v1.7.9” zipball_url=“api.github.com/repos/bower/bower/zipball/v1.7.9”>, #<Hashie::Mash commit=

Returns:



138
139
140
# File 'lib/license_auto/website/github_com.rb', line 138

def list_tags
  @server.repos.tags.body
end

#match_versioned_refObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/license_auto/website/github_com.rb', line 112

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



185
186
187
# File 'lib/license_auto/website/github_com.rb', line 185

def repo_info
  @server.repos.get
end