diff --git a/lib/issue_exporter/export.rb b/lib/issue_exporter/export.rb index 9ca8b51..40b4e76 100644 --- a/lib/issue_exporter/export.rb +++ b/lib/issue_exporter/export.rb @@ -7,7 +7,7 @@ class Exporter attr_accessor :outputter - def initialize(owner, repo, token = nil, options = {}) + def initialize(owner, repo, token = nil, options = { page_size: true }) @owner = owner @repo = repo @token = token @@ -19,7 +19,8 @@ def initialize(owner, repo, token = nil, options = {}) def export error_handler = ErrorHandler.new - url = IssueExporting.make_uri @owner, @repo, @token, @options + url = IssueExporting.make_uri @owner, @repo, @token + url.query += build_query_string @options response = Net::HTTP::get url if err = error_handler.error_message(response) error_handler.handle_error err @@ -28,7 +29,24 @@ def export end end - private + def build_query_string(options) + values = { + include_closed_issues: 'state=all', + page_size: 'per_page=100' + } + qs = options.reduce([]) do |arr, kv| + key, val = kv + query_value = values[key] if val + if query_value.nil? + arr + else + arr.push(query_value) unless query_value.nil? + end + end + if qs == [] then return '' end + return '&' + qs.join('&') + end + def build_outputter(outputter_options) output_type = outputter_options[:output_type] || 'file' case output_type diff --git a/lib/issue_exporter/github.rb b/lib/issue_exporter/github.rb index a27726e..cce5c2c 100644 --- a/lib/issue_exporter/github.rb +++ b/lib/issue_exporter/github.rb @@ -3,15 +3,14 @@ def self.api_url "https://api.github.com/repos/%s/%s/issues?access_token=%s" end - def self.make_url(owner, repo, token, options = {}) + def self.make_url(owner, repo, token) url_format = IssueExporting.api_url root_url = url_format % [owner, repo, token] - return root_url unless options[:include_closed_issues] == true - root_url + "&state=all" + return root_url end - def self.make_uri(owner, repo, token, options = {}) - URI(IssueExporting.make_url(owner, repo, token, options)) + def self.make_uri(owner, repo, token) + URI(IssueExporting.make_url(owner, repo, token)) end def self.turn_options_into_querystring(options) diff --git a/spec/export_spec.rb b/spec/export_spec.rb index a66d95a..865f1ae 100644 --- a/spec/export_spec.rb +++ b/spec/export_spec.rb @@ -5,6 +5,7 @@ let (:repo) { "test-repo" } let (:token) { "abcdef" } + describe 'file output' do let (:output_filename) { File.expand_path('../../issues.json', __FILE__) @@ -32,9 +33,21 @@ end end + it 'builds URLs' do + exporter = IssueExporting::Exporter.new(owner, repo, token) + qs = exporter.build_query_string({}) + expect(qs).to eq '' + qs = exporter.build_query_string({ include_closed_issues: true }) + expect(qs).to eq '&state=all' + qs = exporter.build_query_string({ page_size: true }) + expect(qs).to eq '&per_page=100' + qs = exporter.build_query_string({ include_closed_issues: true, page_size: true }) + expect(qs).to eq '&state=all&per_page=100' + end + it 'puts it all in one file by default' do exporter = IssueExporting::Exporter.new(owner, repo, token) - stub_request(:any, "https://api.github.com/repos/swilliams/test-repo/issues?access_token=abcdef").to_return(body: mock_data) + stub_request(:any, "https://api.github.com/repos/swilliams/test-repo/issues?access_token=abcdef&per_page=100").to_return(body: mock_data) exporter.export() expect(File.exists? output_filename).to eq true expect(File.read(output_filename)).to eq mock_data @@ -43,7 +56,7 @@ it 'handles errors gracefully' do exporter = IssueExporting::Exporter.new(owner, repo, token) error_hash = {"message" => "Bad credentials","documentation_url" => "https://developer.github.com/v3"} - stub_request(:any, "https://api.github.com/repos/swilliams/test-repo/issues?access_token=abcdef").to_return(body: error_hash.to_json) + stub_request(:any, "https://api.github.com/repos/swilliams/test-repo/issues?access_token=abcdef&per_page=100").to_return(body: error_hash.to_json) expect { exporter.export() }.to raise_error SystemExit expect(File.exists? output_filename).to eq false end diff --git a/spec/github_url_spec.rb b/spec/github_url_spec.rb index 28ae9c8..c1a8d32 100644 --- a/spec/github_url_spec.rb +++ b/spec/github_url_spec.rb @@ -1,17 +1,7 @@ require 'spec_helper' describe 'URL Building' do - it 'builds a URL with the closed flag when present' do - url = IssueExporting.make_url 'herp', 'derp', 'TOKEN', { include_closed_issues: true } - expect(url).to eq "https://api.github.com/repos/herp/derp/issues?access_token=TOKEN&state=all" - end - - it 'builds a URL without the closed flag when it is false' do - url = IssueExporting.make_url 'herp', 'derp', 'TOKEN', { include_closed_issues: false } - expect(url).to eq "https://api.github.com/repos/herp/derp/issues?access_token=TOKEN" - end - - it 'builds a URL without the closed flag when it\'s missing' do + it 'builds a URL' do url = IssueExporting.make_url 'herp', 'derp', 'TOKEN' expect(url).to eq "https://api.github.com/repos/herp/derp/issues?access_token=TOKEN" end