Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor creating Github::Nippou::Settings class #62

Merged
merged 5 commits into from
Aug 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/github/nippou.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
31 changes: 2 additions & 29 deletions lib/github/nippou/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`

Expand Down Expand Up @@ -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)
end

def thread_num
Expand Down
21 changes: 13 additions & 8 deletions lib/github/nippou/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -55,6 +57,8 @@ def all(lines)

private

attr_reader :settings

def issue(user_event)
case
when user_event.payload.pull_request
Expand All @@ -71,23 +75,24 @@ def sort(lines)
lines.sort { |a, b| a[:url] <=> b[:url] }
end

def format_status(status)
settings[:dictionary][:status][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],
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
79 changes: 79 additions & 0 deletions lib/github/nippou/settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'json'
require 'ostruct'

module Github
module Nippou
class Settings
def initialize(client:)
@client = client
end

# 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
@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

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
puts <<~MESSAGE
** YAML syntax error.

#{$!.message}
#{yaml_data}
MESSAGE
raise $!
end
end

# Cast to OpenStruct
#
# @param hash [Hash]
# @return [OpenStruct]
def open_struct(hash)
JSON.parse(hash.to_json, object_class: OpenStruct)
end
end
end
end
59 changes: 0 additions & 59 deletions spec/github/nippou/commands_spec.rb

This file was deleted.

99 changes: 99 additions & 0 deletions spec/github/nippou/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
describe Github::Nippou::Settings do
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

before { ENV['GITHUB_NIPPOU_SETTINGS'] = settings_format.to_yaml }

it 'is valid `subject`' do
expect(settings.format.subject).to eq '### %{subject}'
end

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 { settings.format }.to raise_error Psych::SyntaxError
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
end