diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7ad00f1..d033c94 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -12,7 +12,7 @@ Metrics/AbcSize: # Offense count: 4 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 166 + Enabled: false # Offense count: 3 Metrics/CyclomaticComplexity: diff --git a/lib/github_changelog_generator/parser.rb b/lib/github_changelog_generator/parser.rb index d9bc4bf..6e87f7d 100644 --- a/lib/github_changelog_generator/parser.rb +++ b/lib/github_changelog_generator/parser.rb @@ -133,7 +133,7 @@ module GitHubChangelogGenerator end def self.detect_user_and_project(options) - user_project_from_option(options) + options[:user], options[:project] = user_project_from_option(ARGV[0], ARGV[1], options[:github_site]) if !options[:user] || !options[:project] if ENV["RUBYLIB"] =~ /ruby-debug-ide/ options[:user] = "skywinder" @@ -145,31 +145,38 @@ module GitHubChangelogGenerator end end - def self.user_project_from_option(options) - if ARGV[0] && !ARGV[1] - github_site = options[:github_site] ? options[:github_site] : "github.com" + # Try to find user and project name from git remote output + # + # @param [String] output of git remote command + # @return [Array] user and project + def self.user_project_from_option(arg0, arg2, github_site = "github.com") + user = nil + project = nil + + if arg0 && !arg2 # this match should parse strings such "https://github.com/skywinder/Github-Changelog-Generator" or "skywinder/Github-Changelog-Generator" to user and name - match = /(?:.+#{Regexp.escape(github_site)}\/)?(.+)\/(.+)/.match(ARGV[0]) + match = /(?:.+#{Regexp.escape(github_site)}\/)?(.+)\/(.+)/.match(arg0) begin param = match[2].nil? rescue - puts "Can't detect user and name from first parameter: '#{ARGV[0]}' -> exit'" + puts "Can't detect user and name from first parameter: '#{arg0}' -> exit'" exit end if param exit else - options[:user] = match[1] - options[:project] = match[2] + user = match[1] + project = match[2] end end + [user, project] end # Try to find user and project name from git remote output # # @param [String] output of git remote command - # @return [Arrajy] user and project + # @return [Array] user and project def self.user_project_from_remote(remote) # try to find repo in format: # origin git@github.com:skywinder/Github-Changelog-Generator.git (fetch) @@ -202,7 +209,7 @@ module GitHubChangelogGenerator end if __FILE__ == $PROGRAM_NAME - remote = "origin https://github.com/skywinder/Changelog.Merger (fetch)" - p GitHubChangelogGenerator::Parser.user_project_from_remote(remote)[0] + remote = "invalid reference to project" + p user_project_from_option(ARGV[0], ARGV[1], remote) end end diff --git a/spec/unit/parser_spec.rb b/spec/unit/parser_spec.rb index 54d87de..6f80a6e 100644 --- a/spec/unit/parser_spec.rb +++ b/spec/unit/parser_spec.rb @@ -26,4 +26,20 @@ describe GitHubChangelogGenerator::Parser do it { is_expected.to match_array([nil, nil]) } end end + describe "#self.user_project_from_option" do + # context "when option is invalid" do + # it("should exit") { expect { GitHubChangelogGenerator::Parser.user_project_from_option("blah", nil) }.to raise_error(SystemExit) } + # end + + context "when option is valid" do + subject { GitHubChangelogGenerator::Parser.user_project_from_option("skywinder/ActionSheetPicker-3.0", nil) } + it { is_expected.to be_a(Array) } + it { is_expected.to match_array(["skywinder", "ActionSheetPicker-3.0"]) } + end + context "when option nil" do + subject { GitHubChangelogGenerator::Parser.user_project_from_option(nil, nil) } + it { is_expected.to be_a(Array) } + it { is_expected.to match_array([nil, nil]) } + end + end end