Class: GitHubChangelogGenerator::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/github_changelog_generator/fetcher.rb

Overview

A Fetcher responsible for all requests to GitHub and all basic manipulation with related data (such as filtering, validating, e.t.c)

Example: fetcher = GitHubChangelogGenerator::Fetcher.new options

Constant Summary collapse

PER_PAGE_NUMBER =
30
GH_RATE_LIMIT_EXCEEDED_MSG =
"Warning: GitHub API rate limit (5000 per hour) exceeded, change log may be " \
"missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Fetcher

Returns a new instance of Fetcher.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/github_changelog_generator/fetcher.rb', line 14

def initialize(options = {})
  @options = options

  @user = @options[:user]
  @project = @options[:project]
  @github_token = fetch_github_token
  @tag_times_hash = {}

  @logger = Logger.new(STDOUT)
  @logger.formatter = proc do |_severity, _datetime, _progname, msg|
    "#{msg}\n"
  end
  github_options = { per_page: PER_PAGE_NUMBER }
  github_options[:oauth_token] = @github_token unless @github_token.nil?
  github_options[:endpoint] = options[:github_endpoint] unless options[:github_endpoint].nil?
  github_options[:site] = options[:github_endpoint] unless options[:github_site].nil?

  begin
    @github = Github.new github_options
  rescue
    @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
  end
end

Instance Method Details

#fetch_commit(event) ⇒ Hash

Fetch commit for specifed event

Returns:

  • (Hash)


205
206
207
# File 'lib/github_changelog_generator/fetcher.rb', line 205

def fetch_commit(event)
  @github.git_data.commits.get @options[:user], @options[:project], event[:commit_id]
end

#fetch_events_async(issues) ⇒ Void

Fetch event for all issues and add them to :events

Parameters:

  • issues (Array)

Returns:

  • (Void)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/github_changelog_generator/fetcher.rb', line 150

def fetch_events_async(issues)
  i = 0
  max_thread_number = 50
  threads = []
  issues.each_slice(max_thread_number) { |issues_slice|
    issues_slice.each { |issue|
      threads << Thread.new {
        begin
          obj = @github.issues.events.list user: @options[:user],
                                           repo: @options[:project],
                                           issue_number: issue["number"]
        rescue
          @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
        end
        issue[:events] = obj.body
        print "Fetching events for issues and PR: #{i + 1}/#{issues.count}\r"
        i += 1
      }
    }
    threads.each(&:join)
    threads = []
  }

  # to clear line from prev print
  print "                                                            \r"

  if @options[:verbose]
    @logger.info "Fetching events for issues and PR: #{i} Done!"
  end
end

#fetch_github_tokenString

Returns GitHub token. First try to use variable, provided by –token option, otherwise try to fetch it from CHANGELOG_GITHUB_TOKEN env variable.

Returns:

  • (String)


42
43
44
45
46
47
48
49
50
51
# File 'lib/github_changelog_generator/fetcher.rb', line 42

def fetch_github_token
  env_var = @options[:token] ? @options[:token] : (ENV.fetch "CHANGELOG_GITHUB_TOKEN", nil)

  unless env_var
    @logger.warn "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.".yellow
    @logger.warn "This script can make only 50 requests to GitHub API per hour without token!".yellow
  end

  env_var
end

#fetch_issues_and_pull_requestsTuple

This method fetch all closed issues and separate them to pull requests and pure issues (pull request is kind of issue in term of GitHub)

Returns:

  • (Tuple)

    with issues and pull requests



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/github_changelog_generator/fetcher.rb', line 90

def fetch_issues_and_pull_requests
  if @options[:verbose]
    print "Fetching closed issues...\r"
  end
  issues = []

  begin
    response = @github.issues.list user: @options[:user],
                                   repo: @options[:project],
                                   state: "closed",
                                   filter: "all",
                                   labels: nil
    page_i = 0
    count_pages = response.count_pages
    response.each_page do |page|
      page_i += PER_PAGE_NUMBER
      print "Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
      issues.concat(page)
      break if @options[:max_issues] && issues.length >= @options[:max_issues]
    end
  rescue
    @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
  end

  print "                                                \r"

  if @options[:verbose]
    @logger.info "Received issues: #{issues.count}"
  end

  # remove pull request from issues:
  issues.partition { |x|
    x[:pull_request].nil?
  }
end

#fetch_pull_requestsArray

Fetch all pull requests. We need them to detect :merged_at parameter

Returns:

  • (Array)

    all pull requests



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/github_changelog_generator/fetcher.rb', line 128

def fetch_pull_requests
  pull_requests = []
  begin
    response = @github.pull_requests.list @options[:user], @options[:project], state: "closed"
    page_i = 0
    response.each_page do |page|
      page_i += PER_PAGE_NUMBER
      count_pages = response.count_pages
      print "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
      pull_requests.concat(page)
    end
  rescue
    @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
  end

  print "                                                   \r"
  pull_requests
end

#get_all_tagsArray

Fetch all tags from repo

Returns:

  • (Array)

    array of tags



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
# File 'lib/github_changelog_generator/fetcher.rb', line 55

def get_all_tags
  if @options[:verbose]
    print "Fetching tags...\r"
  end

  tags = []

  begin
    response = @github.repos.tags @options[:user], @options[:project]
    page_i = 0
    count_pages = response.count_pages
    response.each_page do |page|
      page_i += PER_PAGE_NUMBER
      print "Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}\r"
      tags.concat(page)
    end
    print "                               \r"

    if tags.count == 0
      @logger.warn "Warning: Can't find any tags in repo.\
Make sure, that you push tags to remote repo via 'git push --tags'".yellow
    elsif @options[:verbose]
      @logger.info "Found #{tags.count} tags"
    end

  rescue
    @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
  end

  tags
end

#get_time_of_tag(tag_name) ⇒ Time

Try to find tag date in local hash. Otherwise fFetch tag time and put it to local hash file.

Parameters:

  • tag_name (String)

    name of the tag

Returns:

  • (Time)

    time of specified tag



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/github_changelog_generator/fetcher.rb', line 185

def get_time_of_tag(tag_name)
  fail ChangelogGeneratorError, "tag_name is nil".red if tag_name.nil?

  if @tag_times_hash[tag_name["name"]]
    return @tag_times_hash[tag_name["name"]]
  end

  begin
    github_git_data_commits_get = @github.git_data.commits.get @options[:user],
                                                               @options[:project],
                                                               tag_name["commit"]["sha"]
  rescue
    @logger.warn GH_RATE_LIMIT_EXCEEDED_MSG.yellow
  end
  time_string = github_git_data_commits_get["committer"]["date"]
  @tag_times_hash[tag_name["name"]] = Time.parse(time_string)
end