Skip to content

Commit

Permalink
Merge pull request #58 from ryz310/feature/template_on_gist
Browse files Browse the repository at this point in the history
The github-nippou will be able to use custom format.
  • Loading branch information
masutaka authored Jul 30, 2017
2 parents 9a373fd + 7b20c4b commit f325ab3
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--require spec_helper
--format documentation
17 changes: 16 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEM
tzinfo (~> 1.1)
addressable (2.4.0)
concurrent-ruby (1.0.2)
diff-lcs (1.3)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
i18n (0.7.0)
Expand All @@ -26,6 +27,19 @@ GEM
sawyer (~> 0.7.0, >= 0.5.3)
parallel (1.9.0)
rake (11.2.2)
rspec (3.6.0)
rspec-core (~> 3.6.0)
rspec-expectations (~> 3.6.0)
rspec-mocks (~> 3.6.0)
rspec-core (3.6.0)
rspec-support (~> 3.6.0)
rspec-expectations (3.6.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0)
rspec-mocks (3.6.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0)
rspec-support (3.6.0)
sawyer (0.7.0)
addressable (>= 2.3.5, < 2.5)
faraday (~> 0.8, < 0.10)
Expand All @@ -41,6 +55,7 @@ DEPENDENCIES
bundler
github-nippou!
rake
rspec

BUNDLED WITH
1.12.5
1.15.3
7 changes: 7 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
format:
subject: '### %{subject}'
line: '* [%{title}](%{url}) by %{user} %{status}'
dictionary:
status:
merged: '**merged!**'
closed: '**closed!**'
1 change: 1 addition & 0 deletions github-nippou.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
end
93 changes: 80 additions & 13 deletions lib/github/nippou/commands.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'parallel'
require 'thor'
require 'yaml'

module Github
module Nippou
Expand All @@ -17,7 +18,7 @@ class Commands < Thor
def list
lines = []
mutex = Mutex.new
format = Format.new(client, thread_num, debug)
format = Format.new(client, thread_num, settings, debug)

Parallel.each_with_index(user_events, in_threads: thread_num) do |user_event, i|
# Contain GitHub access.
Expand All @@ -29,6 +30,41 @@ def list
puts format.all(lines)
end

desc 'init', 'Synchronize github-nippou settings on your gist'
def init
unless client.scopes.include? 'gist'
puts <<~MESSAGE
** Gist scope required.
You need personal access token which has gist scope.
Please add gist scope on your personal access token if you use this command.
MESSAGE
exit!
end
unless `git config github-nippou.settings-gist-id`.chomp.empty?
puts <<~MESSAGE
** Already initialized.
It already have gist id that github-nippou.settings-gist-id on your .gitconfig.
MESSAGE
exit!
end

result = client.create_gist(
description: 'github-nippou settings',
public: true,
files: { 'settings.yml' => { content: settings.to_yaml }}
).to_h
`git config --global github-nippou.settings-gist-id #{result[:id]}`

puts <<~MESSAGE
The github-nippou settings was created on following url: #{result[:html_url]}
And the gist id was set your .gitconfig
You can check the gist id with following command
$ git config --global github-nippou.settings-gist-id
MESSAGE
end

desc 'version', 'Displays version'
def version
puts VERSION
Expand All @@ -54,12 +90,12 @@ def user
when !`git config github-nippou.user`.chomp.empty?
`git config github-nippou.user`.chomp
else
puts <<MESSAGE
** User required.
puts <<~MESSAGE
** User required.
Please set github-nippou.user to your .gitconfig.
$ git config --global github-nippou.user [Your GitHub account]
MESSAGE
Please set github-nippou.user to your .gitconfig.
$ git config --global github-nippou.user [Your GitHub account]
MESSAGE
exit!
end
end
Expand All @@ -72,19 +108,50 @@ def access_token
when !`git config github-nippou.token`.chomp.empty?
`git config github-nippou.token`.chomp
else
puts <<MESSAGE
** Authorization required.
puts <<~MESSAGE
** Authorization required.
Please set github-nippou.token to your .gitconfig.
$ git config --global github-nippou.token [Your GitHub access token]
Please set github-nippou.token to your .gitconfig.
$ git config --global github-nippou.token [Your GitHub access token]
To get new token with `repo` scope, visit
https://github.com/settings/tokens/new
MESSAGE
To get new token with `repo` scope, visit
https://github.com/settings/tokens/new
MESSAGE
exit!
end
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
end

def thread_num
@thread_num ||=
case
Expand Down
35 changes: 22 additions & 13 deletions lib/github/nippou/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ class Format
using SawyerResourceGithub
using StringMarkdown

def initialize(client, thread_num, debug)
attr_reader :settings

def initialize(client, thread_num, settings, debug)
@client = client
@thread_num = thread_num
@settings = settings
@debug = debug
end

Expand Down Expand Up @@ -34,18 +37,17 @@ def line(user_event, i)
def all(lines)
result = ""
prev_repo_name = nil
curr_repo_name = nil
current_repo_name = nil

sort(lines).each do |line|
current_repo_name = line[:repo_name]

unless current_repo_name == prev_repo_name
prev_repo_name = current_repo_name
result << "\n### #{current_repo_name}\n\n"
result << "\n#{format_subject(current_repo_name)}\n\n"
end

result << "* [%s](%s) by %s%s\n" %
[line[:title].markdown_escape, line[:url], line[:user], format_status(line[:status])]
result << "#{format_line(line)}\n"
end

result
Expand All @@ -70,14 +72,21 @@ def sort(lines)
end

def format_status(status)
case status
when :merged
' **merged!**'
when :closed
' **closed!**'
else
''
end
settings[:dictionary][:status][status]
end

def format_subject(subject)
sprintf(settings[:format][:subject], subject: subject)
end

def format_line(line)
sprintf(
settings[:format][:line],
title: line[:title].markdown_escape,
url: line[:url],
user: line[:user],
status: format_status(line[:status])
).strip
end
end
end
Expand Down
59 changes: 59 additions & 0 deletions spec/github/nippou/commands_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe Github::Nippou::Commands do
let(:commands) { described_class.new }

describe '#settings' do
subject(:execute) { commands.send(:settings) }

let(:settings) do
{
format: {
subject: '### %{subject}',
line: '* [%{title}](%{url}) by %{user} %{status}',
},
}
end

let(:yaml) { settings.to_yaml }

context "when ENV['GITHUB_NIPPOU_SETTINGS'] present" do
before { ENV['GITHUB_NIPPOU_SETTINGS'] = 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
end
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
execute
rescue SystemExit
nil
end
}.to output(<<~ERROR).to_stdout
** YAML syntax error.
(<unknown>): 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
end
end
end
end
end
Loading

0 comments on commit f325ab3

Please sign in to comment.