class JekyllAsciidoctorPdf::GitInfo
Attributes
remote[RW]
Public Class Methods
new(token, repo)
click to toggle source
# File lib/jekyll_asciidoctor_pdf/gitinfo.rb, line 22 def initialize(token, repo) @authors_hash = Hash.new @token = token @repo = repo if (token.empty? || token.nil?) @remote = false else @remote = true end end
Public Instance Methods
getAuthorsList(file)
click to toggle source
Get authors information
# File lib/jekyll_asciidoctor_pdf/gitinfo.rb, line 131 def getAuthorsList(file) if (remote) return getContributors(file); else return getContributorsWithoutGithubToken(file); end end
getContributors(file)
click to toggle source
List of contributors order by commits
Avoid unexpected terminations / Best Effort approach
# File lib/jekyll_asciidoctor_pdf/gitinfo.rb, line 72 def getContributors(file) headers = {Authorization: "token #{@token}"} uri = "https://api.github.com/repos/#{@repo}/commits?path=#{file.to_s}" contributors = Hash.new; begin response = RestClient.get(uri, headers) if response.code == 200 responseBody = response.body; unless responseBody.nil? || responseBody.empty? commits = JSON.parse(responseBody); if commits.nil? || commits.empty? puts "Warning: Unable to find contributors for page in github repository."; else commits.each do |commit| author = commit['author'] name = getRealUser(author["login"]) if contributors.key?(name) contributors[name] = contributors[name] + 1; else contributors[name] = 1; end end end end else puts "Warning: Invalid Response code received for page: #{uri.to_s}. Response headers set to #{response.headers.to_str}"; end rescue => e puts "Error Message: #{e.to_s}"; end authors = contributors.sort_by{|name, commits| [-commits, name]}.transpose[0] if (authors.nil? || authors.empty? ) return 'NetApp' end return authors.join(', ') end
getContributorsWithoutGithubToken(file)
click to toggle source
Get authors information from git log
# File lib/jekyll_asciidoctor_pdf/gitinfo.rb, line 117 def getContributorsWithoutGithubToken(file) names = %x[ git log --pretty=format:"%an" #{file} | sort | uniq ] # last_commit_date can be nil iff the file was not committed. if (names.nil? || names.empty?) return 'NetApp' end return names.split(/\n+/).join(', ') end
getRealUser(login_name)
click to toggle source
Get the real user name or return the login name
Avoid unexpected terminations / Best Effort approach
# File lib/jekyll_asciidoctor_pdf/gitinfo.rb, line 39 def getRealUser(login_name) headers = {Authorization: "token #{@token}"} uri = "https://api.github.com/users/#{login_name.to_s}" if authors_hash.key?(login_name) return authors_hash[login_name] end begin response = RestClient.get(uri, headers) if response.code == 200 userResponse = response.body unless userResponse.nil? || userResponse.empty? hash = JSON.parse(userResponse) if (hash.key?('name') and (not hash['name'].nil?)) authors_hash[login_name] = hash['name'] return hash['name'] end end else puts "Error Code: #{response.code.to_s}" end rescue => e puts "Error Message: #{e.to_s}" end return login_name end