From f256f858fe9c6e85b58e890f3276b9ed4b679c29 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 17:41:22 +0900 Subject: [PATCH 1/5] Refactor creating Github::Nippou::Settings class --- lib/github/nippou.rb | 1 + lib/github/nippou/commands.rb | 29 +----------- lib/github/nippou/settings.rb | 47 +++++++++++++++++++ .../{commands_spec.rb => settings_spec.rb} | 23 ++++----- 4 files changed, 57 insertions(+), 43 deletions(-) create mode 100644 lib/github/nippou/settings.rb rename spec/github/nippou/{commands_spec.rb => settings_spec.rb} (72%) diff --git a/lib/github/nippou.rb b/lib/github/nippou.rb index 225ec7a..7ab358d 100644 --- a/lib/github/nippou.rb +++ b/lib/github/nippou.rb @@ -3,5 +3,6 @@ require 'github/nippou/commands' require 'github/nippou/format' +require 'github/nippou/settings' require 'github/nippou/user_events' require 'github/nippou/version' diff --git a/lib/github/nippou/commands.rb b/lib/github/nippou/commands.rb index 7a597d5..2ce4f7b 100644 --- a/lib/github/nippou/commands.rb +++ b/lib/github/nippou/commands.rb @@ -122,34 +122,7 @@ def access_token end def settings - return @settings if @settings.present? - - yaml_data = - case - when ENV['GITHUB_NIPPOU_SETTINGS'] - ENV['GITHUB_NIPPOU_SETTINGS'].chomp - when !`git config github-nippou.settings`.chomp.empty? - `git config github-nippou.settings`.chomp - when !`git config github-nippou.settings-gist-id`.chomp.empty? - gist_id = `git config github-nippou.settings-gist-id`.chomp - gist = client.gist(gist_id) - gist[:files][:'settings.yml'][:content] - end - - @settings = - if yaml_data - YAML.load(yaml_data).deep_symbolize_keys - else - YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys - end - rescue Psych::SyntaxError => e - puts <<~MESSAGE - ** YAML syntax error. - - #{e.message} - #{yaml_data} - MESSAGE - exit + @settings ||= Settings.new(client: client).data end def thread_num diff --git a/lib/github/nippou/settings.rb b/lib/github/nippou/settings.rb new file mode 100644 index 0000000..58d9548 --- /dev/null +++ b/lib/github/nippou/settings.rb @@ -0,0 +1,47 @@ +module Github + module Nippou + class Settings + def initialize(client:) + @client = client + end + + # Getting settings data + # + # return [Hash] + def data + return @data if @data.present? + + yaml_data = + case + when ENV['GITHUB_NIPPOU_SETTINGS'] + ENV['GITHUB_NIPPOU_SETTINGS'].chomp + when !`git config github-nippou.settings`.chomp.empty? + `git config github-nippou.settings`.chomp + when !`git config github-nippou.settings-gist-id`.chomp.empty? + gist_id = `git config github-nippou.settings-gist-id`.chomp + gist = client.gist(gist_id) + gist[:files][:'settings.yml'][:content] + end + + @data = + if yaml_data + YAML.load(yaml_data).deep_symbolize_keys + else + YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys + end + rescue Psych::SyntaxError => e + puts <<~MESSAGE + ** YAML syntax error. + + #{e.message} + #{yaml_data} + MESSAGE + exit + end + + private + + attr_reader :client + end + end +end diff --git a/spec/github/nippou/commands_spec.rb b/spec/github/nippou/settings_spec.rb similarity index 72% rename from spec/github/nippou/commands_spec.rb rename to spec/github/nippou/settings_spec.rb index 0b705cd..1014bd1 100644 --- a/spec/github/nippou/commands_spec.rb +++ b/spec/github/nippou/settings_spec.rb @@ -1,12 +1,9 @@ -require 'spec_helper' +describe Github::Nippou::Settings do + describe '#data' do + let(:client) { Octokit::Client.new(login: 'taro', access_token: '1234abcd') } + let(:settings) { described_class.new(client: client) } -describe Github::Nippou::Commands do - let(:commands) { described_class.new } - - describe '#settings' do - subject(:execute) { commands.send(:settings) } - - let(:settings) do + let(:settings_format) do { format: { subject: '### %{subject}', @@ -15,16 +12,12 @@ } end - let(:yaml) { settings.to_yaml } - context "when ENV['GITHUB_NIPPOU_SETTINGS'] present" do - before { ENV['GITHUB_NIPPOU_SETTINGS'] = yaml } + before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format.to_yaml } context 'given valid YAML syntax' do it 'should set YAML value to @settings' do - expect { execute } - .to change { commands.instance_variable_get(:@settings) } - .to settings + expect(settings.data).to eq settings_format end end @@ -40,7 +33,7 @@ it 'should output YAML syntax error message' do expect { begin - execute + settings.data rescue SystemExit nil end From 2f6a54154637e9b7a19808c773d5505eaf1da3f2 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 17:55:44 +0900 Subject: [PATCH 2/5] Refactor Github::Nippou::Settings#data --- lib/github/nippou/settings.rb | 50 +++++++++++++++++------------------ 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/github/nippou/settings.rb b/lib/github/nippou/settings.rb index 58d9548..20a5545 100644 --- a/lib/github/nippou/settings.rb +++ b/lib/github/nippou/settings.rb @@ -9,34 +9,34 @@ def initialize(client:) # # return [Hash] def data - return @data if @data.present? + @data ||= + begin + yaml_data = + case + when ENV['GITHUB_NIPPOU_SETTINGS'] + ENV['GITHUB_NIPPOU_SETTINGS'].chomp + when !`git config github-nippou.settings`.chomp.empty? + `git config github-nippou.settings`.chomp + when !`git config github-nippou.settings-gist-id`.chomp.empty? + gist_id = `git config github-nippou.settings-gist-id`.chomp + gist = client.gist(gist_id) + gist[:files][:'settings.yml'][:content] + end - yaml_data = - case - when ENV['GITHUB_NIPPOU_SETTINGS'] - ENV['GITHUB_NIPPOU_SETTINGS'].chomp - when !`git config github-nippou.settings`.chomp.empty? - `git config github-nippou.settings`.chomp - when !`git config github-nippou.settings-gist-id`.chomp.empty? - gist_id = `git config github-nippou.settings-gist-id`.chomp - gist = client.gist(gist_id) - gist[:files][:'settings.yml'][:content] - end + if yaml_data + YAML.load(yaml_data).deep_symbolize_keys + else + YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys + end + rescue Psych::SyntaxError => e + puts <<~MESSAGE + ** YAML syntax error. - @data = - if yaml_data - YAML.load(yaml_data).deep_symbolize_keys - else - YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys + #{e.message} + #{yaml_data} + MESSAGE + exit end - rescue Psych::SyntaxError => e - puts <<~MESSAGE - ** YAML syntax error. - - #{e.message} - #{yaml_data} - MESSAGE - exit end private From 5bc399c352ea4a238e82ecfa0c146205d916309d Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 18:56:27 +0900 Subject: [PATCH 3/5] Add #format,#dictionary,#yaml instead of the #data in Github::Nippou::Settings --- lib/github/nippou/commands.rb | 4 +- lib/github/nippou/format.rb | 15 ++-- lib/github/nippou/settings.rb | 40 ++++++++- spec/github/nippou/settings_spec.rb | 130 ++++++++++++++++++++-------- 4 files changed, 143 insertions(+), 46 deletions(-) diff --git a/lib/github/nippou/commands.rb b/lib/github/nippou/commands.rb index 2ce4f7b..3198c78 100644 --- a/lib/github/nippou/commands.rb +++ b/lib/github/nippou/commands.rb @@ -53,7 +53,7 @@ def init result = client.create_gist( description: 'github-nippou settings', public: true, - files: { 'settings.yml' => { content: settings.to_yaml }} + files: { 'settings.yml' => { content: settings.yaml }} ).to_h `git config --global github-nippou.settings-gist-id #{result[:id]}` @@ -122,7 +122,7 @@ def access_token end def settings - @settings ||= Settings.new(client: client).data + @settings ||= Settings.new(client: client) end def thread_num diff --git a/lib/github/nippou/format.rb b/lib/github/nippou/format.rb index dc1cb41..ff04f58 100644 --- a/lib/github/nippou/format.rb +++ b/lib/github/nippou/format.rb @@ -4,8 +4,10 @@ class Format using SawyerResourceGithub using StringMarkdown - attr_reader :settings - + # @param client [Octokit::Client] + # @param thread_num [Integer] + # @param settings [Settings] + # @param debug [Boolean] def initialize(client, thread_num, settings, debug) @client = client @thread_num = thread_num @@ -55,6 +57,8 @@ def all(lines) private + attr_reader :settings + def issue(user_event) case when user_event.payload.pull_request @@ -72,16 +76,17 @@ def sort(lines) end def format_status(status) - settings[:dictionary][:status][status] + return nil if status.nil? + settings.dictionary.status.send(status) end def format_subject(subject) - sprintf(settings[:format][:subject], subject: subject) + sprintf(settings.format.subject, subject: subject) end def format_line(line) sprintf( - settings[:format][:line], + settings.format.line, title: line[:title].markdown_escape, url: line[:url], user: line[:user], diff --git a/lib/github/nippou/settings.rb b/lib/github/nippou/settings.rb index 20a5545..09f07b6 100644 --- a/lib/github/nippou/settings.rb +++ b/lib/github/nippou/settings.rb @@ -1,3 +1,6 @@ +require 'json' +require 'ostruct' + module Github module Nippou class Settings @@ -5,7 +8,32 @@ def initialize(client:) @client = client end - # Getting settings data + # Getting format settings + # + # @return [OpenStruct] + def format + open_struct(data[:format]) + end + + # Getting dictionary settings + # + # @return [OpenStruct] + def dictionary + open_struct(data[:dictionary]) + end + + # Getting settings as YAML format + # + # return [String] + def yaml + data.to_yaml + end + + private + + attr_reader :client + + # Getting settings data as Hash # # return [Hash] def data @@ -39,9 +67,13 @@ def data end end - private - - attr_reader :client + # Cast to OpenStruct + # + # @param hash [Hash] + # @return [OpenStruct] + def open_struct(hash) + JSON.parse(hash.to_json, object_class: OpenStruct) + end end end end diff --git a/spec/github/nippou/settings_spec.rb b/spec/github/nippou/settings_spec.rb index 1014bd1..9069f50 100644 --- a/spec/github/nippou/settings_spec.rb +++ b/spec/github/nippou/settings_spec.rb @@ -1,43 +1,48 @@ describe Github::Nippou::Settings do - describe '#data' do - let(:client) { Octokit::Client.new(login: 'taro', access_token: '1234abcd') } - let(:settings) { described_class.new(client: client) } - - let(:settings_format) do - { - format: { - subject: '### %{subject}', - line: '* [%{title}](%{url}) by %{user} %{status}', - }, - } - end + let(:client) { Octokit::Client.new(login: 'taro', access_token: '1234abcd') } + let(:settings) { described_class.new(client: client) } + + describe '#format' do + context 'given valid settings' do + let(:settings_format) do + { + format: { + subject: '### %{subject}', + line: '* [%{title}](%{url}) by %{user} %{status}', + }, + } + end - context "when ENV['GITHUB_NIPPOU_SETTINGS'] present" do before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format.to_yaml } - context 'given valid YAML syntax' do - it 'should set YAML value to @settings' do - expect(settings.data).to eq settings_format - end + it 'is valid `subject`' do + expect(settings.format.subject).to eq '### %{subject}' end - context 'given invalid YAML syntax' do - before do - ENV['GITHUB_NIPPOU_SETTINGS'] = <<~INVALID_YAML - format: - **!!invalid!!** - line: '* [%{title}](%{url}) by %{user} %{status}' - INVALID_YAML - end - - it 'should output YAML syntax error message' do - expect { - begin - settings.data - rescue SystemExit - nil - end - }.to output(<<~ERROR).to_stdout + it 'is valid `line`' do + expect(settings.format.line).to eq '* [%{title}](%{url}) by %{user} %{status}' + end + end + + context 'given invalid settings' do + let(:settings_format_yaml) do + <<~INVALID_YAML + format: + **!!invalid!!** + line: '* [%{title}](%{url}) by %{user} %{status}' + INVALID_YAML + end + + before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format_yaml } + + it 'outputs YAML syntax error message' do + expect do + begin + settings.format + rescue SystemExit + nil + end + end.to output(<<~ERROR).to_stdout ** YAML syntax error. (): did not find expected alphabetic or numeric character while scanning an alias at line 2 column 3 @@ -45,7 +50,62 @@ **!!invalid!!** line: '* [%{title}](%{url}) by %{user} %{status}' ERROR - end + end + end + end + + describe '#dictionary' do + context 'given valid settings' do + let(:settings_dictionary) do + { + dictionary: { + status: { + merged: '**merged!**', + closed: '**closed!**', + }, + }, + } + end + + before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_dictionary.to_yaml } + + it 'is valid `status.merged`' do + expect(settings.dictionary.status.merged).to eq '**merged!**' + end + + it 'is valid `status.closed`' do + expect(settings.dictionary.status.closed).to eq '**closed!**' + end + end + end + + describe '#yaml' do + context 'given valid settings' do + let(:settings_yaml) do + <<~VALID_YAML + format: + subject: "### %{subject}" + line: "* [%{title}](%{url}) by %{user} %{status}" + dictionary: + status: + merged: "**merged!**" + closed: "**closed!**" + VALID_YAML + end + + before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_yaml } + + it 'is valid yaml' do + expect(settings.yaml).to eq <<~VALID_YAML + --- + :format: + :subject: "### %{subject}" + :line: "* [%{title}](%{url}) by %{user} %{status}" + :dictionary: + :status: + :merged: "**merged!**" + :closed: "**closed!**" + VALID_YAML end end end From a79e2758cd6184587da9c86741e2445ef6439743 Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 19:05:34 +0900 Subject: [PATCH 4/5] Sort --- lib/github/nippou/format.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/github/nippou/format.rb b/lib/github/nippou/format.rb index ff04f58..7550276 100644 --- a/lib/github/nippou/format.rb +++ b/lib/github/nippou/format.rb @@ -75,11 +75,6 @@ def sort(lines) lines.sort { |a, b| a[:url] <=> b[:url] } end - def format_status(status) - return nil if status.nil? - settings.dictionary.status.send(status) - end - def format_subject(subject) sprintf(settings.format.subject, subject: subject) end @@ -93,6 +88,11 @@ def format_line(line) status: format_status(line[:status]) ).strip end + + def format_status(status) + return nil if status.nil? + settings.dictionary.status.send(status) + end end end end From 74cdf96c54bd0e2c4349cdc6886247432a04481b Mon Sep 17 00:00:00 2001 From: Takashi Masuda Date: Sun, 6 Aug 2017 19:16:45 +0900 Subject: [PATCH 5/5] Avoid exit code 0 --- lib/github/nippou/settings.rb | 6 +++--- spec/github/nippou/settings_spec.rb | 15 +-------------- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/lib/github/nippou/settings.rb b/lib/github/nippou/settings.rb index 09f07b6..5a5f2a7 100644 --- a/lib/github/nippou/settings.rb +++ b/lib/github/nippou/settings.rb @@ -56,14 +56,14 @@ def data else YAML.load_file(File.expand_path('../../../config/settings.yml', __dir__)).deep_symbolize_keys end - rescue Psych::SyntaxError => e + rescue Psych::SyntaxError puts <<~MESSAGE ** YAML syntax error. - #{e.message} + #{$!.message} #{yaml_data} MESSAGE - exit + raise $! end end diff --git a/spec/github/nippou/settings_spec.rb b/spec/github/nippou/settings_spec.rb index 9069f50..fb63f3a 100644 --- a/spec/github/nippou/settings_spec.rb +++ b/spec/github/nippou/settings_spec.rb @@ -36,20 +36,7 @@ before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format_yaml } it 'outputs YAML syntax error message' do - expect do - begin - settings.format - rescue SystemExit - nil - end - end.to output(<<~ERROR).to_stdout - ** YAML syntax error. - - (): did not find expected alphabetic or numeric character while scanning an alias at line 2 column 3 - format: - **!!invalid!!** - line: '* [%{title}](%{url}) by %{user} %{status}' - ERROR + expect { settings.format }.to raise_error Psych::SyntaxError end end end