Class: GitHubChangelogGenerator::OctoFetcher
- Inherits:
-
Object
- Object
- GitHubChangelogGenerator::OctoFetcher
- Defined in:
- lib/github_changelog_generator/octo_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::OctoFetcher.new(options)
Constant Summary collapse
- PER_PAGE_NUMBER =
100- MAXIMUM_CONNECTIONS =
50- MAX_FORBIDDEN_RETRIES =
100- CHANGELOG_GITHUB_TOKEN =
"CHANGELOG_GITHUB_TOKEN"- GH_RATE_LIMIT_EXCEEDED_MSG =
"Warning: Can't finish operation: GitHub API rate limit exceeded, changelog may be " \ "missing some issues. You can limit the number of issues fetched using the `--max-issues NUM` argument."
- NO_TOKEN_PROVIDED =
"Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found. " \ "This script can make only 50 requests to GitHub API per hour without a token!"
- DEFAULT_REQUEST_OPTIONS =
{ per_page: PER_PAGE_NUMBER }
Instance Method Summary collapse
-
#calculate_pages(client, method, request_options) ⇒ Integer
Returns the number of pages for a API call.
- #client ⇒ Object
- #client_options ⇒ Object
- #closed_pr_options ⇒ Object
-
#commits ⇒ Array
Fetch all commits.
- #commits_in_branch(name) ⇒ Array<String>
- #connection_options ⇒ Object
-
#default_branch ⇒ String
Default branch of the repo.
-
#fetch_all_tags ⇒ Array <Hash>
Fetch all tags from repo.
-
#fetch_closed_issues_and_pr ⇒ Tuple
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).
-
#fetch_closed_pull_requests ⇒ Array <Hash>
Fetch all pull requests.
-
#fetch_comments_async(prs) ⇒ Void
Fetch comments for PRs and add them to "comments".
-
#fetch_commit(commit_id) ⇒ Hash
Fetch commit for specified event.
-
#fetch_date_of_tag(tag) ⇒ Time
Fetch tag time from repo.
-
#fetch_events_async(issues) ⇒ Void
Fetch event for all issues and add them to 'events'.
-
#fetch_tag_shas(tags) ⇒ Object
Fetch all SHAs occurring in or before a given tag and add them to "shas_in_tag".
-
#get_all_tags ⇒ Object
rubocop:disable Naming/AccessorMethodName.
-
#github_fetch_tags ⇒ Array <Hash>
Fill input array with tags.
-
#initialize(options = {}) ⇒ OctoFetcher
constructor
A new instance of OctoFetcher.
- #middleware ⇒ Object
-
#oldest_commit ⇒ Hash
Return the oldest commit in a repo.
Constructor Details
#initialize(options = {}) ⇒ OctoFetcher
Returns a new instance of OctoFetcher.
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 32 def initialize( = {}) @options = || {} @user = @options[:user] @project = @options[:project] @since = @options[:since] @http_cache = @options[:http_cache] @commits = [] @branches = nil @graph = nil @client = nil @commits_in_tag_cache = {} end |
Instance Method Details
#calculate_pages(client, method, request_options) ⇒ Integer
Returns the number of pages for a API call
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 114 def calculate_pages(client, method, ) # Makes the first API call so that we can call last_response check_github_response do client.send(method, user_project, DEFAULT_REQUEST_OPTIONS.merge()) end last_response = client.last_response if (last_pg = last_response.rels[:last]) querystring_as_hash(last_pg.href)["page"].to_i else 1 end end |
#client ⇒ Object
88 89 90 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 88 def client @client ||= Octokit::Client.new() end |
#client_options ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 71 def = { middleware: middleware, connection_options: } if (github_token = fetch_github_token) [:access_token] = github_token end if (endpoint = @options[:github_endpoint]) [:api_endpoint] = endpoint end end |
#closed_pr_options ⇒ Object
154 155 156 157 158 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 154 def @closed_pr_options ||= { filter: "all", labels: nil, state: "closed" }.tap { || [:since] = @since if @since } end |
#commits ⇒ Array
Fetch all commits
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 307 def commits if @commits.empty? Sync do = Async::Barrier.new semaphore = Async::Semaphore.new(MAXIMUM_CONNECTIONS, parent: ) branch = @options[:release_branch] || default_branch if (since_commit = @options[:since_commit]) iterate_pages(client, "commits_since", since_commit, branch, parent: semaphore) do |new_commits| @commits.concat(new_commits) end else iterate_pages(client, "commits", branch, parent: semaphore) do |new_commits| @commits.concat(new_commits) end end .wait @commits.sort! do |b, a| a[:commit][:author][:date] <=> b[:commit][:author][:date] end end end @commits end |
#commits_in_branch(name) ⇒ Array<String>
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 348 def commits_in_branch(name) @branches ||= begin all_branches = {} iterate_pages(client, "branches") do |branches| branches.each { |branch| all_branches[branch[:name]] = branch } end all_branches end if (branch = @branches[name]) commits_in_tag(branch[:commit][:sha]) else [] end end |
#connection_options ⇒ Object
65 66 67 68 69 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 65 def ca_file = @options[:ssl_ca_file] || ENV["SSL_CA_FILE"] || File.("ssl_certs/cacert.pem", __dir__) Octokit..merge({ ssl: { ca_file: ca_file } }) end |
#default_branch ⇒ String
Returns Default branch of the repo.
342 343 344 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 342 def default_branch @default_branch ||= client.repository(user_project)[:default_branch] end |
#fetch_all_tags ⇒ Array <Hash>
Fetch all tags from repo
97 98 99 100 101 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 97 def print "Fetching tags...\r" if @options[:verbose] check_github_response { } end |
#fetch_closed_issues_and_pr ⇒ Tuple
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)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 164 def fetch_closed_issues_and_pr print "Fetching closed issues...\r" if @options[:verbose] issues = [] page_i = 0 count_pages = calculate_pages(client, "issues", ) iterate_pages(client, "issues", **) do |new_issues| page_i += PER_PAGE_NUMBER print_in_same_line("Fetching issues... #{page_i}/#{count_pages * PER_PAGE_NUMBER}") issues.concat(new_issues) break if @options[:max_issues] && issues.length >= @options[:max_issues] end print_empty_line Helper.log.info "Received issues: #{issues.count}" # separate arrays of issues and pull requests: issues.map { |issue| stringify_keys_deep(issue.to_hash) } .partition { |issue_or_pr| issue_or_pr["pull_request"].nil? } end |
#fetch_closed_pull_requests ⇒ Array <Hash>
Fetch all pull requests. We need them to detect :merged_at parameter
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 187 def fetch_closed_pull_requests pull_requests = [] = { state: "closed" } page_i = 0 count_pages = calculate_pages(client, "pull_requests", ) iterate_pages(client, "pull_requests", **) do |new_pr| page_i += PER_PAGE_NUMBER log_string = "Fetching merged dates... #{page_i}/#{count_pages * PER_PAGE_NUMBER}" print_in_same_line(log_string) pull_requests.concat(new_pr) end print_empty_line Helper.log.info "Pull Request count: #{pull_requests.count}" pull_requests.map { |pull_request| stringify_keys_deep(pull_request.to_hash) } end |
#fetch_comments_async(prs) ⇒ Void
Fetch comments for PRs and add them to "comments"
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 247 def fetch_comments_async(prs) = Async::Barrier.new semaphore = Async::Semaphore.new(MAXIMUM_CONNECTIONS, parent: ) Sync do client = self.client prs.each do |pr| semaphore.async do pr["comments"] = [] iterate_pages(client, "issue_comments", pr["number"]) do |new_comment| pr["comments"].concat(new_comment) end pr["comments"] = pr["comments"].map { |comment| stringify_keys_deep(comment.to_hash) } end end .wait end nil end |
#fetch_commit(commit_id) ⇒ Hash
Fetch commit for specified event
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 286 def fetch_commit(commit_id) found = commits.find do |commit| commit["sha"] == commit_id end if found stringify_keys_deep(found.to_hash) else client = self.client # cache miss; don't add to @commits because unsure of order. check_github_response do commit = client.commit(user_project, commit_id) commit = stringify_keys_deep(commit.to_hash) commit end end end |
#fetch_date_of_tag(tag) ⇒ Time
Fetch tag time from repo
275 276 277 278 279 280 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 275 def fetch_date_of_tag(tag) commit_data = fetch_commit(tag["commit"]["sha"]) commit_data = stringify_keys_deep(commit_data.to_hash) commit_data["commit"]["committer"]["date"] end |
#fetch_events_async(issues) ⇒ Void
Fetch event for all issues and add them to 'events'
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 210 def fetch_events_async(issues) fetched_count = 0 mutex = Mutex.new # Add accept option explicitly for disabling the warning of preview API. preview = { accept: Octokit::Preview::PREVIEW_TYPES[:project_card_events] } = Async::Barrier.new semaphore = Async::Semaphore.new(MAXIMUM_CONNECTIONS, parent: ) Sync do client = self.client issues.each do |issue| semaphore.async do issue["events"] = [] iterate_pages(client, "issue_events", issue["number"], **preview) do |new_event| issue["events"].concat(new_event) end issue["events"] = issue["events"].map { |event| stringify_keys_deep(event.to_hash) } current = mutex.synchronize { fetched_count += 1 } print_in_same_line("Fetching events for issues and PR: #{current}/#{issues.count}") end end .wait # to clear line from prev print print_empty_line end Helper.log.info "Fetching events for issues and PR: #{fetched_count}" end |
#fetch_tag_shas(tags) ⇒ Object
Fetch all SHAs occurring in or before a given tag and add them to "shas_in_tag"
369 370 371 372 373 374 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 369 def fetch_tag_shas() # Reverse the tags array to gain max benefit from the @commits_in_tag_cache .reverse_each do |tag| tag["shas_in_tag"] = commits_in_tag(tag["commit"]["sha"]) end end |
#get_all_tags ⇒ Object
rubocop:disable Naming/AccessorMethodName
103 104 105 106 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 103 def # rubocop:disable Naming/AccessorMethodName warn("[DEPRECATED] GitHubChangelogGenerator::OctoFetcher#get_all_tags is deprecated; use fetch_all_tags instead.") end |
#github_fetch_tags ⇒ Array <Hash>
Fill input array with tags
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 132 def = [] page_i = 0 count_pages = calculate_pages(client, "tags", {}) iterate_pages(client, "tags") do || page_i += PER_PAGE_NUMBER print_in_same_line("Fetching tags... #{page_i}/#{count_pages * PER_PAGE_NUMBER}") .concat() end print_empty_line if .count == 0 Helper.log.warn "Warning: Can't find any tags in repo. \ Make sure, that you push tags to remote repo via 'git push --tags'" else Helper.log.info "Found #{.count} tags" end # tags are a Sawyer::Resource. Convert to hash .map { |resource| stringify_keys_deep(resource.to_hash) } end |
#middleware ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 45 def middleware Faraday::RackBuilder.new do |builder| if @http_cache cache_file = @options.fetch(:cache_file) { File.join(Dir.tmpdir, "github-changelog-http-cache") } cache_log = @options.fetch(:cache_log) { File.join(Dir.tmpdir, "github-changelog-logger.log") } builder.use( Faraday::HttpCache, serializer: Marshal, store: ActiveSupport::Cache::FileStore.new(cache_file), logger: Logger.new(cache_log), shared_cache: false ) end builder.use Octokit::Response::RaiseError builder.adapter Faraday.default_adapter end end |
#oldest_commit ⇒ Hash
Return the oldest commit in a repo
337 338 339 |
# File 'lib/github_changelog_generator/octo_fetcher.rb', line 337 def oldest_commit commits.last end |