From 03bca478c8ea777552b23794b88722c9dd471ccd Mon Sep 17 00:00:00 2001 From: Petr Korolev Date: Wed, 12 Nov 2014 17:59:40 +0200 Subject: [PATCH] Add token support, resolved #19. Use CHANGELOG_GITHUB_TOKEN variable to specify token in the shell. --- Gemfile | 1 + Gemfile.lock | 2 ++ README.md | 9 +++++- github_changelog_generator.gemspec | 1 + lib/github_changelog_generator.rb | 40 ++++++++++++++++++------ lib/github_changelog_generator/parser.rb | 2 +- 6 files changed, 44 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index d4c6197..f5094d7 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' gem 'github_api' gem 'httparty' +gem 'colorize' diff --git a/Gemfile.lock b/Gemfile.lock index dc53313..e297bac 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ GEM remote: https://rubygems.org/ specs: addressable (2.3.6) + colorize (0.7.3) descendants_tracker (0.0.4) thread_safe (~> 0.3, >= 0.3.1) faraday (0.9.0) @@ -39,5 +40,6 @@ PLATFORMS ruby DEPENDENCIES + colorize github_api httparty diff --git a/README.md b/README.md index c8ae936..b8c5578 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,14 @@ Type `github_changelog_generator --help` for detailed usage. ## 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: >! /Library/Ruby/Gems/2.0.0/gems/github_api-0.12.2/lib/github_api/response/raise_error.rb:14:in `on_complete' diff --git a/github_changelog_generator.gemspec b/github_changelog_generator.gemspec index 8437934..f1721ad 100644 --- a/github_changelog_generator.gemspec +++ b/github_changelog_generator.gemspec @@ -16,6 +16,7 @@ Gem::Specification.new do |s| s.license = "MIT" s.add_runtime_dependency(%q, ["~> 0.13"]) s.add_runtime_dependency(%q, ["~> 0.12"]) + s.add_runtime_dependency(%q, ["~> 0.7"]) s.executables = %w(github_changelog_generator) end diff --git a/lib/github_changelog_generator.rb b/lib/github_changelog_generator.rb index 3e92000..50e514a 100755 --- a/lib/github_changelog_generator.rb +++ b/lib/github_changelog_generator.rb @@ -3,6 +3,7 @@ require 'github_api' require 'json' require 'httparty' +require 'colorize' require_relative 'github_changelog_generator/parser' @@ -13,11 +14,15 @@ class ChangelogGenerator def initialize() @options = Parser.parse_options - if @options[:token] - @github = Github.new oauth_token: @options[:token] - else + + github_token + + if @github_token.nil? @github = Github.new + else + @github = Github.new oauth_token: @github_token end + @all_tags = self.get_all_tags @pull_requests = self.get_all_closed_pull_requests @issues = self.get_all_issues @@ -113,13 +118,13 @@ class ChangelogGenerator puts "Receive tags for repo #{url}" end - if @options[:token] - response = HTTParty.get(url, - :headers => {'Authorization' => "token #{@options[:token]}", - 'User-Agent' => 'Changelog-Generator'}) - else + if @github_token.nil? response = HTTParty.get(url, :headers => {'User-Agent' => 'Changelog-Generator'}) + else + response = HTTParty.get(url, + :headers => {'Authorization' => "token #{@github_token}", + 'User-Agent' => 'Changelog-Generator'}) end json_parse = JSON.parse(response.body) @@ -131,6 +136,23 @@ class ChangelogGenerator json_parse 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) since_tag_time = self.get_time_of_tag(since_tag) till_tag_time = self.get_time_of_tag(till_tag) @@ -325,5 +347,5 @@ end if __FILE__ == $0 changelog_generator = ChangelogGenerator.new - changelog_generator.compund_changelog + # changelog_generator.compund_changelog end diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index b24889b..fe8f9b4 100644 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -13,7 +13,7 @@ class Parser opts.on('-p', '--project [PROJECT]', 'Name of project on GitHub') do |last| options[:project] = last 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 end opts.on('-h', '--help', 'Displays Help') do