lib/change_log.rb in openstudio-extension-0.2.1 vs lib/change_log.rb in openstudio-extension-0.2.2
- old
+ new
@@ -1,148 +1,132 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
-require 'github_api'
+require 'octokit'
require 'date'
require 'optparse'
require 'optparse/date'
# Instructions:
-# Get a token from github's settings (https://github.com/settings/tokens)
#
# Example:
# ruby change_log.rb -t abcdefghijklmnopqrstuvwxyz -s 2017-09-06
#
+#
-options = {}
-OptionParser.new do |opts|
- opts.banner = "Usage: change_log.rb [options]\n" \
- 'Prints New, Open, Closed Issues, and number of accepted PRs'
- opts.separator ''
+class ChangeLog
+ def initialize(user_and_repo, start_date = Date.today - 90, end_date = Date.today, apikey = nil)
+ @user_and_repo = user_and_repo
+ @apikey = apikey
+ @start_date = start_date
+ @end_date = end_date
- # defaults, go back 90 days
- options[:start_date] = Date.today - 90
- options[:end_date] = Date.today
+ # Convert dates to time objects
+ @start_date = Time.parse(@start_date.to_s) if start_date.is_a? String
+ @end_date = Time.parse(@end_date.to_s) if end_date.is_a? String
+ # GitHub API uses Time and not the Date class, so ensure that we have Time
+ @start_date = Time.parse(@start_date.to_s)
+ @end_date = Time.parse(@end_date.to_s)
- opts.on('-s', '--start-date [DATE]', Date, 'Start of data (e.g. 2017-09-06)') do |v|
- options[:start_date] = v
+ @total_open_issues = []
+ @total_open_pull_requests = []
+ @new_issues = []
+ @closed_issues = []
+ @accepted_pull_requests = []
+
+ begin
+ @github = Octokit::Client.new
+ if apikey
+ @github = Octokit::Client.new(access_token: apikey)
+ end
+ @github.auto_paginate = true
+ rescue StandardError => e
+ puts e.message
+ # write out the help message
+ ChangeLog.help
+ exit(1)
+ end
end
- opts.on('-e', '--end-date [DATE]', Date, 'End of data (e.g. 2017-09-13)') do |v|
- options[:end_date] = v
- end
- opts.on('-t', '--token [String]', String, 'Github API Token') do |v|
- options[:token] = v
- end
-end.parse!
-# Convert dates to time objects
-options[:start_date] = Time.parse(options[:start_date].to_s)
-options[:end_date] = Time.parse(options[:end_date].to_s)
-puts options
-
-### Repository options
-repo_owner = 'NREL'
-repo = 'openstudio-extension-gem'
-
-github = Github.new
-if options[:token]
- puts 'Using github token'
- github = Github.new oauth_token: options[:token]
-end
-
-total_open_issues = []
-total_open_pull_requests = []
-new_issues = []
-closed_issues = []
-accepted_pull_requests = []
-
-def get_num(issue)
- issue.html_url.split('/')[-1].to_i
-end
-
-def get_issue_num(issue)
- "\##{get_num(issue)}"
-end
-
-def get_html_url(issue)
- issue.html_url
-end
-
-def get_title(issue)
- issue.title
-end
-
-def print_issue(issue)
- is_feature = false
- issue.labels.each { |label| is_feature = true if label.name == 'Feature Request' }
-
- if is_feature
- "- Improved [#{get_issue_num(issue)}]( #{get_html_url(issue)} ), #{get_title(issue)}"
- else
- "- Fixed [#{get_issue_num(issue)}]( #{get_html_url(issue)} ), #{get_title(issue)}"
+ # Class method to show how to use the API through Rake.
+ def self.help
+ puts 'Usage: bundle exec rake openstudio:change_log[<start_date>,<end_date>,<apikey>]'
+ puts ' <start_date> = [Optional] Start of data (e.g., 2020-09-06), defaults to 90 days before today'
+ puts ' <end_date> = [Optional] End of data (e.g., 2020-10-06), default to today'
+ puts ' <apikey> = [Optional] GitHub API Key (used for private repos)'
+ puts
+ puts ' Ensure that the GitHub user/repo is set in your Rakefile, for example, '
+ puts " rake_task.set_extension_class(OpenStudio::Extension::Extension, 'nrel/openstudio-extension-gem')"
+ puts
+ puts ' Example usages:'
+ puts ' bundle exec rake openstudio:change_log[2020-01-01]'
+ puts ' bundle exec rake openstudio:change_log[2020-01-01,2020-06-30]'
+ puts ' bundle exec rake openstudio:change_log[2020-01-01,2020-01-10,<private_api_key>]'
+ puts
+ puts ' Notes:'
+ puts ' For creating token, see https://github.com/settings/tokens.'
+ puts ' Note that if passing apikey, then you must pass start_date and end_date as well. There must be no spaces'
+ puts ' between the arguments (see examples above).'
end
-end
-# Process Open Issues
-results = -1
-page = 1
-while results != 0
- resp = github.issues.list user: repo_owner, repo: repo, sort: 'created', direction: 'asc',
- state: 'open', per_page: 100, page: page
- results = resp.length
- resp.env[:body].each do |issue, _index|
- created = Time.parse(issue.created_at)
- if !issue.key?(:pull_request)
- total_open_issues << issue
- if created >= options[:start_date] && created <= options[:end_date]
- new_issues << issue
+ # Process Open Issues
+ def process
+ @github.list_issues(@user_and_repo, state: 'all').each do |issue|
+ if issue.state == 'open'
+ if issue.pull_request
+ if issue.created_at >= @start_date && issue.created_at <= @end_date
+ @total_open_pull_requests << issue
+ end
+ else
+ @total_open_issues << issue
+ if issue.created_at >= @start_date && issue.created_at <= @end_date
+ @new_issues << issue
+ end
+ end
+ else
+ # the issue is closed
+ if issue.closed_at >= @start_date && issue.closed_at <= @end_date
+ if issue.pull_request
+ @accepted_pull_requests << issue
+ else
+ @closed_issues << issue
+ end
+ end
end
- else
- total_open_pull_requests << issue
end
+
+ @closed_issues.sort! { |x, y| x.number <=> y.number }
+ @new_issues.sort! { |x, y| x.number <=> y.number }
+ @accepted_pull_requests.sort! { |x, y| x.number <=> y.number }
+ @total_open_pull_requests.sort! { |x, y| x.number <=> y.number }
+ rescue StandardError => e
+ puts e.message
+ ChangeLog.help
+ exit(1)
end
- page += 1
-end
+ def print_issue(issue)
+ is_feature = false
+ issue.labels.each { |label| is_feature = true if label.name == 'Feature Request' }
-# Process Closed Issues
-results = -1
-page = 1
-while results != 0
- resp = github.issues.list user: repo_owner, repo: repo, sort: 'created', direction: 'asc',
- state: 'closed', per_page: 100, page: page
- results = resp.length
- resp.env[:body].each do |issue, _index|
- created = Time.parse(issue.created_at)
- closed = Time.parse(issue.closed_at)
- if !issue.key?(:pull_request)
- if created >= options[:start_date] && created <= options[:end_date]
- new_issues << issue
- end
- if closed >= options[:start_date] && closed <= options[:end_date]
- closed_issues << issue
- end
- elsif closed >= options[:start_date] && closed <= options[:end_date]
- accepted_pull_requests << issue
+ if is_feature
+ "- Improved [##{issue.number}]( #{issue.html_url} ), #{issue.title}"
+ else
+ "- Fixed [\##{issue.number}]( #{issue.html_url} ), #{issue.title}"
end
end
- page += 1
-end
+ def print_issues
+ puts "Total Open Issues: #{@total_open_issues.length}"
+ puts "Total Open Pull Requests: #{@total_open_pull_requests.length}"
+ puts "\nDate Range: #{@start_date.strftime('%m/%d/%y')} - #{@end_date.strftime('%m/%d/%y')}:"
+ puts "\nNew Issues: #{@new_issues.length} (" + @new_issues.map { |issue| issue.number }.join(', ') + ')'
-closed_issues.sort! { |x, y| get_num(x) <=> get_num(y) }
-new_issues.sort! { |x, y| get_num(x) <=> get_num(y) }
-accepted_pull_requests.sort! { |x, y| get_num(x) <=> get_num(y) }
-total_open_pull_requests.sort! { |x, y| get_num(x) <=> get_num(y) }
+ puts "\nClosed Issues: #{@closed_issues.length}"
+ @closed_issues.each { |issue| puts print_issue(issue) }
-puts "Total Open Issues: #{total_open_issues.length}"
-puts "Total Open Pull Requests: #{total_open_pull_requests.length}"
-puts "\nDate Range: #{options[:start_date].strftime('%m/%d/%y')} - #{options[:end_date].strftime('%m/%d/%y')}:"
-puts "\nNew Issues: #{new_issues.length} (" + new_issues.map { |issue| get_issue_num(issue) }.join(', ') + ')'
+ puts "\nAccepted Pull Requests: #{@accepted_pull_requests.length}"
+ @accepted_pull_requests.each { |issue| puts print_issue(issue) }
-puts "\nClosed Issues: #{closed_issues.length}"
-closed_issues.each { |issue| puts print_issue(issue) }
-
-puts "\nAccepted Pull Requests: #{accepted_pull_requests.length}"
-accepted_pull_requests.each { |issue| puts print_issue(issue) }
-
-puts "\nAll Open Issues: #{total_open_issues.length} (" + total_open_issues.map { |issue| get_issue_num(issue) }.join(', ') + ')'
+ puts "\nAll Open Issues: #{@total_open_issues.length} (" + @total_open_issues.map { |issue| "\##{issue.number}" }.join(', ') + ')'
+ end
+end