Class: JekyllAsciidoctorPdf::GitInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll_asciidoctor_pdf/gitinfo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, repo) ⇒ GitInfo

Returns a new instance of GitInfo.



22
23
24
25
26
27
28
29
30
31
# 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

Instance Attribute Details

#authors_hashObject

Returns the value of attribute authors_hash.



20
21
22
# File 'lib/jekyll_asciidoctor_pdf/gitinfo.rb', line 20

def authors_hash
  @authors_hash
end

#remoteObject

Returns the value of attribute remote.



21
22
23
# File 'lib/jekyll_asciidoctor_pdf/gitinfo.rb', line 21

def remote
  @remote
end

Instance Method Details

#getAuthorsList(file) ⇒ Object

Get authors information



131
132
133
134
135
136
137
# 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) ⇒ Object

List of contributors order by commits

Avoid unexpected terminations / Best Effort approach



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
107
108
109
110
111
112
# 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) ⇒ Object

Get authors information from git log



117
118
119
120
121
122
123
124
125
# 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) ⇒ Object

Get the real user name or return the login name

Avoid unexpected terminations / Best Effort approach



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/jekyll_asciidoctor_pdf/gitinfo.rb', line 39

def getRealUser()
    headers = {Authorization: "token #{@token}"}
    uri = "https://api.github.com/users/#{.to_s}"

    if authors_hash.key?() 
        return authors_hash[]
    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'))
                    authors_hash[] = 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 
end