backlog_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rspec
19
+
20
+ spec/vcr_cassettes/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
4
+ # - "2.1.1"
5
+ script:
6
+ - bundle exec rspec
7
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # -*- coding: utf-8 -*-
2
+ source 'https://rubygems.org'
3
+
4
+ group :test do
5
+ gem 'coveralls', :require => false# カバレッジ測定
6
+ end
7
+
8
+
9
+ # Specify your gem's dependencies in backlog_api.gemspec
10
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,33 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'livereload' do
5
+ watch(%r{app/views/.+\.(erb|haml|slim)$})
6
+ watch(%r{app/helpers/.+\.rb})
7
+ watch(%r{public/.+\.(css|js|html)})
8
+ watch(%r{config/locales/.+\.yml})
9
+ # Rails Assets Pipeline
10
+ watch(%r{(app|vendor)(/assets/\w+/(.+\.(css|js|html|png|jpg))).*}) { |m| "/assets/#{m[3]}" }
11
+ end
12
+
13
+ guard :rspec do
14
+ watch(%r{^spec/.+_spec\.rb$})
15
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
16
+ watch('spec/spec_helper.rb') { "spec" }
17
+
18
+ # Rails example
19
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
20
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
21
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
22
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
23
+ watch('config/routes.rb') { "spec/routing" }
24
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
25
+
26
+ # Capybara features specs
27
+ watch(%r{^app/views/(.+)/.*\.(erb|haml|slim)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
28
+
29
+ # Turnip features and steps
30
+ watch(%r{^spec/acceptance/(.+)\.feature$})
31
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
32
+ end
33
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 shibata
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ [![Build Status](https://travis-ci.org/a1125ts/backlog_api.svg?branch=master)](https://travis-ci.org/a1125ts/backlog_api)
2
+ [![Coverage Status](https://coveralls.io/repos/a1125ts/backlog_api/badge.png)](https://coveralls.io/r/a1125ts/backlog_api)
3
+ [![Code Climate](https://codeclimate.com/github/a1125ts/backlog_api.png)](https://codeclimate.com/github/a1125ts/backlog_api)
4
+
5
+
6
+ # BacklogApi
7
+
8
+ Ruby toolkit for the Backlog API.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'backlog_api'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install backlog_api
23
+
24
+ ## Usage
25
+
26
+ ### Get timeline
27
+ ```ruby
28
+ require 'backlog_api'
29
+ client = BacklogApi::Client.new
30
+ client.get_timeline
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,52 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'backlog_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "backlog_api"
8
+ spec.version = BacklogApi::VERSION
9
+ spec.authors = ["a1125ts"]
10
+ spec.email = ["a1125ts@aiit.ac.jp"]
11
+ spec.description = %q{backlogのバックログを叩くよ}
12
+ spec.summary = %q{backlogのバックログを叩くよ。まずはチケットを作れるようにしたよ。}
13
+ spec.homepage = "https://github.com/a1125ts/backlog_api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'webmock' # web_api用のmock
25
+ spec.add_development_dependency 'vcr' # 2回目以降はローカルのデータをmockとして使用
26
+ spec.add_development_dependency 'guard-livereload' # ブラウザ自動リロード
27
+ spec.add_development_dependency 'guard-rspec' # 自動テスト
28
+ spec.add_development_dependency 'growl' # テスト結果をgrowlで通知
29
+ spec.add_development_dependency 'rb-fsevent' # ファイルの変更を検知する。OSX用。いる?
30
+ spec.add_development_dependency 'capybara' # ブラウザ操作
31
+ spec.add_development_dependency 'selenium-webdriver'# cappybaraのドライバ(firefox,chrome,safari,etc)
32
+ spec.add_development_dependency 'poltergeist' # cappybaraのドライバ(phantomjs)
33
+ spec.add_development_dependency 'capybara-webkit' # cappybaraのドライバ(Qtwebkit直)
34
+ spec.add_development_dependency 'launchy' # capybaraのsave_and_open_pageメソッド(スクリーンショットを取る)で必要
35
+
36
+ spec.add_development_dependency "pry"
37
+ spec.add_development_dependency 'pry-doc'
38
+ spec.add_development_dependency 'pry-debugger'
39
+ spec.add_development_dependency 'tapp'
40
+ spec.add_development_dependency 'sinatra-contrib'
41
+ spec.add_development_dependency 'simplecov' # カバレッジ測定
42
+ # spec.add_development_dependency 'coveralls' # カバレッジ測定
43
+
44
+ spec.add_runtime_dependency "thor"
45
+ spec.add_runtime_dependency "activesupport"
46
+ spec.add_runtime_dependency 'awesome_print'
47
+ spec.add_runtime_dependency 'net-netrc'
48
+ spec.add_runtime_dependency 'sinatra'
49
+ spec.add_runtime_dependency 'haml'
50
+ spec.add_runtime_dependency "formatador"
51
+
52
+ end
data/bin/backlog ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'backlog_api'
4
+ BacklogApi::Command.start(ARGV)
data/config.ru ADDED
@@ -0,0 +1,8 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'rack'
5
+ require 'application.rb'
6
+
7
+ # 起動(port=9292)
8
+ run Sinatra::Application
@@ -0,0 +1,124 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'sinatra'
3
+ require 'sinatra/reloader'
4
+ require "haml"
5
+
6
+ require 'backlog_api'
7
+
8
+ # 何度も読み込まれて舞う
9
+ PROJECT_ID = ENV['BACKLOG_PROJECTID2'].to_i
10
+ ASSIGENER_ID = ENV['BACKLOG_USERID'].to_i
11
+
12
+
13
+ # CLIENT = BacklogApi::Client.new
14
+ # トップレベルには定義できないのでここにビューヘルパーとか追加してね
15
+ helpers do
16
+ def select_tag(name, selected_id, opt)
17
+ options = opt.map do |k,v|
18
+ "<option value='#{k}' #{selected_id == k.to_i ? %{selected} : ''} > #{v}</option>"
19
+ end.join
20
+
21
+ <<-"EOS"
22
+ <select name='#{name}'>
23
+ #{options}
24
+ </select>
25
+ EOS
26
+ end
27
+
28
+ def status_select_tag(issue)
29
+ select_tag("status[#{issue["key"]}]", issue["status"]["id"],
30
+ '1' => '未対応',
31
+ '2' => '処理中',
32
+ '3' => '処理済み',
33
+ '4' => '完了',
34
+ )
35
+ end
36
+
37
+ end
38
+
39
+
40
+ # アクションみたいなの
41
+ get '/' do
42
+ # "views/index.erb"をrender。○しかし今回は"@@ index" の部分をrenderする
43
+
44
+ @issues = BacklogApi::Client.new.find_issue(
45
+ projectId: PROJECT_ID,
46
+ assignerId: ASSIGENER_ID,
47
+ sort: :STATUS, # ステータスでソート
48
+ order: true, # 昇順
49
+ ).tapp(&:count)
50
+
51
+ erb :index
52
+ end
53
+
54
+ post '/issues' do
55
+ BacklogApi::Client.new.create_issue(
56
+ projectId: PROJECT_ID,
57
+ assignerId: ASSIGENER_ID,
58
+ summary: params[:summary],
59
+ issueTypeId: 1074130811, # タスク
60
+ )
61
+ redirect '/'
62
+ end
63
+
64
+
65
+ post '/update_issues' do
66
+ params["status"].each do |k, v|
67
+ begin
68
+ BacklogApi::Client.new.switch_status(
69
+ key: k,
70
+ statusId: v.to_i,
71
+ )
72
+ puts "update"
73
+ rescue
74
+ # TODO: 警告とかね
75
+ puts "do nothing"
76
+ end
77
+ end
78
+ redirect '/'
79
+ end
80
+
81
+
82
+ get '/test' do
83
+ "test"
84
+ end
85
+
86
+
87
+
88
+
89
+
90
+ # __END__: コレ以降がインラインテンプレートになる
91
+ __END__
92
+
93
+ # @@index: "views/index.erb"と同義っぽい
94
+ @@index
95
+
96
+ <html>
97
+ <body>
98
+ <h1>課題追加</h1>
99
+ <form action='/issues' method='post'>
100
+ <input type='text_field' value='' name='summary'>
101
+ <input type='submit' value='さぁ、課題を追加するのです!'>
102
+ </form>
103
+
104
+ <h1>課題更新</h1>
105
+ <form action='/update_issues' method='post'>
106
+ <table>
107
+ <tr>
108
+ <th>件名</th>
109
+ <th>ステータス</th>
110
+ <th>リンク</th>
111
+ </tr>
112
+ <% @issues.each do |issue| %>
113
+ <tr>
114
+ <td><%= issue["summary"] %></td>
115
+ <td><%= status_select_tag issue %></td>
116
+ <td><a href='<%= issue["url"] %>'>リンクです!!</a></td>
117
+ </tr>
118
+ <% end %>
119
+ </table>
120
+ <input type='submit' value='課題の一括更新です!'>
121
+ </form>
122
+ <body>
123
+ </html>
124
+
@@ -0,0 +1,69 @@
1
+ # -*- coding: utf-8 -*-
2
+ module BacklogApi
3
+
4
+ class Client
5
+ HOST = '%s.backlog.jp'
6
+ PATH = '/XML-RPC'
7
+ PORT = '443' # HTTPS
8
+ PROXY_HOST = nil
9
+ PROXY_PORT = nil
10
+ USE_SSL = true
11
+ TIMEOUT = 60
12
+
13
+ METHOD = 'backlog.%s'
14
+
15
+ attr_accessor :space, :user, :password, :host, :client
16
+
17
+ def initialize(user = nil , password = nil , space = nil)
18
+ login_from_args(user, password, space) || login_from_netrc || login_from_environment_variables
19
+ @client = XMLRPC::Client.new(@host, PATH, PORT, PROXY_HOST, PROXY_PORT, @user, @password, USE_SSL, TIMEOUT)
20
+ end
21
+
22
+ # まとめてメソッド定義
23
+ # 例: BacklogApi::Client.new.get_timline
24
+ BacklogApi::API_METHODS.keys.uniq.each do |api_method|
25
+ define_method api_method.underscore, &->(params = nil) do
26
+ call METHOD % api_method, params
27
+ end
28
+ end
29
+
30
+
31
+ private
32
+
33
+ def call(api_method, params = nil)
34
+ if params
35
+ @client.call(api_method, params)
36
+ else
37
+ @client.call(api_method)
38
+ end
39
+ end
40
+
41
+ def login_from_netrc
42
+ netrc = Net::Netrc.locate HOST.sub('.','') % ''
43
+ return unless netrc
44
+ @user = netrc.user
45
+ @password = netrc.password
46
+ @space = netrc.space
47
+ @host = HOST % @space
48
+ end
49
+
50
+ def login_from_environment_variables
51
+ return unless ENV["BACKLOG_USER"] && ENV["BACKLOG_PASSWORD"] && ENV["BACKLOG_SPACE"]
52
+ @user = ENV["BACKLOG_USER"]
53
+ @password = ENV["BACKLOG_PASSWORD"]
54
+ @space = ENV["BACKLOG_SPACE"]
55
+ @host = HOST % @space
56
+ end
57
+
58
+ def login_from_args(user = nil, password = nil, space = nil)
59
+ return unless user && password && space
60
+ @user = user
61
+ @password = password
62
+ @space = space
63
+ @host = HOST % @space
64
+ end
65
+
66
+
67
+
68
+ end # Client
69
+ end # BacklogApi
@@ -0,0 +1,120 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module BacklogApi
4
+ class Command < Thor
5
+
6
+ # まとめて定義
7
+ # 例: $ backlog get_timeline
8
+ BacklogApi::API_METHODS.keys.uniq.each do |api_method|
9
+ # オプション
10
+ # とりあえず必須パラメータだけオプションに指定できるようにした
11
+ BacklogApi::API_METHODS[api_method]["required"].each do |key|
12
+ method_option( key,
13
+ type: :string,
14
+ desc: key,
15
+ required: true,
16
+ aliases: "-#{key[0]}"
17
+ )
18
+ end
19
+
20
+ # 説明(必須)
21
+ desc api_method.underscore, (BacklogApi::API_METHODS[api_method]['desc'].presence || api_method)
22
+
23
+
24
+
25
+ # サブコマンド定義
26
+ define_method api_method.underscore, &->(params = nil) do
27
+
28
+ # APIのパラメータの型の違いを吸収
29
+ # TODO: 微妙...scalarに関してはコマンドライン引数で取得するように修正する
30
+ params =
31
+ case BacklogApi::API_METHODS[api_method]["type"]
32
+ when 'scalar'
33
+ options.values[0]
34
+ when 'struct'
35
+ options
36
+ when nil
37
+ nil
38
+ end
39
+
40
+ response = Client.new.send api_method.underscore, params
41
+ ap response
42
+ end
43
+ end
44
+
45
+ # Sinatraアプリ起動
46
+ method_option( :server,
47
+ type: :string,
48
+ desc: :server,
49
+ required: false,
50
+ aliases: "s"
51
+ )
52
+ desc :server, '課題管理用のwebインタフェースを立ち上げます。'
53
+ def server
54
+ puts "Start sinatra app..."
55
+ root = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
56
+ exec "cd #{root} && rackup config.ru"
57
+ end
58
+
59
+ # 進捗状況を表示
60
+ desc :progress, 'プロジェクトの進捗状況を表示します'
61
+ def progress
62
+ # data = [
63
+ # {:foo => '123', :bar => '456', :buz => '789'},
64
+ # {:foo => 'abc', :bar => 'def', :buz => 'ghi'},
65
+ # {:foo => 'alpha', :bar => 'beta', :buz => 'gamma'},
66
+ # ]
67
+
68
+ issues = BacklogApi::Client.new.find_issue(projectId: PROJECT_ID)
69
+ issues = group_issues_by_assigner(issues)
70
+ data = issues.map do |name,issues|
71
+ {
72
+ "名前" => (name || '未割当'),
73
+ "進捗" => progress_bar(issues),
74
+ "完了件数" => completed_count_per_total_count(issues),
75
+ "完了率" => progress_rate(issues),
76
+ }
77
+ end
78
+ Formatador.display_table(data)
79
+ end
80
+
81
+
82
+ private
83
+
84
+ def group_issues_by_assigner(issues)
85
+ issues.group_by{|issue|issue["assigner"]["name"] if issue["assigner"]}
86
+ end
87
+
88
+ def progress_rate(issues)
89
+ total_count = issues.count
90
+ completed_count = issues.count{|issue|issue["status"]["id"] == 4}
91
+ "%d%" % (completed_count.to_f / total_count * 100)
92
+ end
93
+
94
+ def completed_count_per_total_count(issues)
95
+ total_count = issues.count
96
+ completed_count = issues.count{|issue|issue["status"]["id"] == 4}
97
+ "%d/%d" % [completed_count, total_count]
98
+ end
99
+
100
+ def progress_bar(issues)
101
+ issues.inject('') do |ret, issue|
102
+ ret <<
103
+ case issue["status"]["id"]
104
+ when 1
105
+ "|".red
106
+ when 2
107
+ "|".yellow
108
+ when 3
109
+ "|".green
110
+ when 4
111
+ "|".blue
112
+ end
113
+ end.tapp.tapp(&:size)
114
+
115
+ end
116
+
117
+
118
+
119
+ end # Command < Thor
120
+ end # BacklogApi
@@ -0,0 +1,5 @@
1
+ # -*- coding: utf-8 -*-
2
+ module BacklogApi
3
+ # BacklogApi::Command,BacklogApi::Clientで利用するからここがいいのかなー?
4
+ API_METHODS = YAML.load_file(File.expand_path(File.join('../..', 'data', 'api_methods.yml'), __FILE__))
5
+ end
@@ -0,0 +1,3 @@
1
+ module BacklogApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "thor"
3
+ require 'xmlrpc/client'
4
+ require "active_support/core_ext"
5
+ require "awesome_print"
6
+ require "net/netrc"
7
+ require "yaml"
8
+ require "sinatra"
9
+ require "haml"
10
+ require 'sinatra'
11
+ require 'sinatra/reloader'
12
+
13
+ require "backlog_api/version"
14
+ require "backlog_api/const"
15
+ require "backlog_api/client"
16
+ require "backlog_api/command"
17
+ require "application"
18
+ require "formatador"
19
+
20
+
21
+ # TODO: ここじゃまずい気が...どこに書けばいいの?
22
+ require "pry-debugger"
23
+ require "tapp"