Add token support, resolved #19.

Use CHANGELOG_GITHUB_TOKEN variable to specify token in the shell.
This commit is contained in:
Petr Korolev 2014-11-12 17:59:40 +02:00
parent 91229c7472
commit 03bca478c8
6 changed files with 44 additions and 11 deletions

View File

@ -1,3 +1,4 @@
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'github_api' gem 'github_api'
gem 'httparty' gem 'httparty'
gem 'colorize'

View File

@ -2,6 +2,7 @@ GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
addressable (2.3.6) addressable (2.3.6)
colorize (0.7.3)
descendants_tracker (0.0.4) descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1) thread_safe (~> 0.3, >= 0.3.1)
faraday (0.9.0) faraday (0.9.0)
@ -39,5 +40,6 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
colorize
github_api github_api
httparty httparty

View File

@ -49,7 +49,14 @@ Type `github_changelog_generator --help` for detailed usage.
## FAQ: ## FAQ:
Since GitHub allow to make only 50 requests without authentication it's recommended to run this scrip with key `-t [your-16-digit-token]` that you can easily **[generate it here](https://github.com/settings/applications)**. Since GitHub allow to make only 50 requests without authentication it's recommended to run this script with token
**You can easily [generate it here](https://github.com/settings/applications)**.
And:
- Run with key `-t [your-16-digit-token]` that
- Or add to your shell variable CHANGELOG_GITHUB_TOKEN and specify there your token
So, if you got error like this: So, if you got error like this:
>! /Library/Ruby/Gems/2.0.0/gems/github_api-0.12.2/lib/github_api/response/raise_error.rb:14:in `on_complete' >! /Library/Ruby/Gems/2.0.0/gems/github_api-0.12.2/lib/github_api/response/raise_error.rb:14:in `on_complete'

View File

@ -16,6 +16,7 @@ Gem::Specification.new do |s|
s.license = "MIT" s.license = "MIT"
s.add_runtime_dependency(%q<httparty>, ["~> 0.13"]) s.add_runtime_dependency(%q<httparty>, ["~> 0.13"])
s.add_runtime_dependency(%q<github_api>, ["~> 0.12"]) s.add_runtime_dependency(%q<github_api>, ["~> 0.12"])
s.add_runtime_dependency(%q<colorize>, ["~> 0.7"])
s.executables = %w(github_changelog_generator) s.executables = %w(github_changelog_generator)
end end

View File

@ -3,6 +3,7 @@
require 'github_api' require 'github_api'
require 'json' require 'json'
require 'httparty' require 'httparty'
require 'colorize'
require_relative 'github_changelog_generator/parser' require_relative 'github_changelog_generator/parser'
@ -13,11 +14,15 @@ class ChangelogGenerator
def initialize() def initialize()
@options = Parser.parse_options @options = Parser.parse_options
if @options[:token]
@github = Github.new oauth_token: @options[:token] github_token
else
if @github_token.nil?
@github = Github.new @github = Github.new
else
@github = Github.new oauth_token: @github_token
end end
@all_tags = self.get_all_tags @all_tags = self.get_all_tags
@pull_requests = self.get_all_closed_pull_requests @pull_requests = self.get_all_closed_pull_requests
@issues = self.get_all_issues @issues = self.get_all_issues
@ -113,13 +118,13 @@ class ChangelogGenerator
puts "Receive tags for repo #{url}" puts "Receive tags for repo #{url}"
end end
if @options[:token] if @github_token.nil?
response = HTTParty.get(url,
:headers => {'Authorization' => "token #{@options[:token]}",
'User-Agent' => 'Changelog-Generator'})
else
response = HTTParty.get(url, response = HTTParty.get(url,
:headers => {'User-Agent' => 'Changelog-Generator'}) :headers => {'User-Agent' => 'Changelog-Generator'})
else
response = HTTParty.get(url,
:headers => {'Authorization' => "token #{@github_token}",
'User-Agent' => 'Changelog-Generator'})
end end
json_parse = JSON.parse(response.body) json_parse = JSON.parse(response.body)
@ -131,6 +136,23 @@ class ChangelogGenerator
json_parse json_parse
end end
def github_token
if @options[:token]
return @github_token ||= @options[:token]
end
env_var = ENV.fetch 'CHANGELOG_GITHUB_TOKEN', nil
unless env_var
puts "Warning: No token provided (-t option) and variable $CHANGELOG_GITHUB_TOKEN was not found.".yellow
puts "This script can make only 50 requests to GitHub API per hour without token!".yellow
end
@github_token ||= env_var
end
def generate_log_between_tags(since_tag, till_tag) def generate_log_between_tags(since_tag, till_tag)
since_tag_time = self.get_time_of_tag(since_tag) since_tag_time = self.get_time_of_tag(since_tag)
till_tag_time = self.get_time_of_tag(till_tag) till_tag_time = self.get_time_of_tag(till_tag)
@ -325,5 +347,5 @@ end
if __FILE__ == $0 if __FILE__ == $0
changelog_generator = ChangelogGenerator.new changelog_generator = ChangelogGenerator.new
changelog_generator.compund_changelog # changelog_generator.compund_changelog
end end

View File

@ -13,7 +13,7 @@ class Parser
opts.on('-p', '--project [PROJECT]', 'Name of project on GitHub') do |last| opts.on('-p', '--project [PROJECT]', 'Name of project on GitHub') do |last|
options[:project] = last options[:project] = last
end end
opts.on('-t', '--token [TOKEN]', 'To make more than 50 requests this script required your OAuth token for GitHub. You can generate it on https://github.com/settings/applications') do |last| opts.on('-t', '--token [TOKEN]', 'To make more than 50 requests this script required your OAuth token for GitHub. You can generate here: https://github.com/settings/tokens/new') do |last|
options[:token] = last options[:token] = last
end end
opts.on('-h', '--help', 'Displays Help') do opts.on('-h', '--help', 'Displays Help') do