diff --git a/Gemfile.lock b/Gemfile.lock index cb1bcb126..2c791faef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - github-services (1.0.0.f39cad6) + github-services (1.0.0.0778ac6) activeresource (~> 4.0.0) addressable (~> 2.2.7) aws-sdk (~> 1.27) diff --git a/docs/chatwork b/docs/chatwork new file mode 100644 index 000000000..2a0048de3 --- /dev/null +++ b/docs/chatwork @@ -0,0 +1,25 @@ +Beyond chat. Beyond simple. Text, voice, video, task management, and file sharing all in one place. + +http://chatwork.com/ + +If you want to see detailed repo activity in your room, follow this steps: + + - Click the **Settings Cog** on the top right + - Click on **Integrations** + - Select **GitHub** + - Click on **configure manually** option + - Copy and paste the provided token here + +Install Notes +------------- + +http://chatwork.com + +1. **Token** - Enter your API Token. +2. **Rooms** - Enter a comma delimited set of Room ID. + +Options + +3. **Mute push** - Mute push notifications. +4. **Mute pull request** - Mute pull request notifications. +5. **Mute issues** - Mute issues notifications. diff --git a/lib/services/chatwork.rb b/lib/services/chatwork.rb new file mode 100644 index 000000000..63834d368 --- /dev/null +++ b/lib/services/chatwork.rb @@ -0,0 +1,83 @@ +class Service::ChatWork < Service + string :rooms, :restrict_to_branch + password :token + boolean :mute_push, :mute_pull_request, :mute_issues + white_list :rooms, :restrict_to_branch + + default_events :push, :pull_request, :issues + + maintained_by github: 'chatwork' + supported_by github: 'chatwork' + + url 'http://www.chatwork.com/' + logo_url 'http://www.chatwork.com/image/common/logo_hz.png' + + def receive_push + required_config + if config_boolean_true? 'mute_push' + return + end + + branch_restriction = data['restrict_to_branch'].to_s + if branch_restriction.length > 0 && branch_restriction.index(branch) == nil + return + end + + message = <<-EOH +[info][title]#{pusher_name} has pushed #{commits.size} commit(s)[/title]#{commit_messages.join("\n")} +[hr]Branch: #{branch_name} +Repository: #{repo_name} +Compare: #{compare_url}[/info] +EOH + send message + end + + def receive_pull_request + required_config + if config_boolean_true? 'mute_pull_request' + return + end + + message = <<-EOH +[info][title]#{action} a pull request. #{pull.title}[/title]#{summary_message} +[hr]URL: #{summary_url}[/info] +EOH + send message + end + + def receive_issues + required_config + return if config_boolean_true? 'mute_issues' + + message = <<-EOH +[info][title]#{action} an issue. #{issue.title}[/title]#{summary_message} +[hr]URL: #{summary_url}[/info] +EOH + send message + end + + private + + def required_config + raise_config_error 'Token is required' if data['token'].to_s.empty? + raise_config_error 'rooms is required' if data['rooms'].to_s.empty? + end + + def send(message) + http.url_prefix = 'https://api.chatwork.com' + http.headers['X-ChatWorkToken'] = data['token'] + http.headers['X-GitHub-Event'] = event.to_s + http.headers['Content-Type'] = 'application/json' + http.headers['Accept'] = 'application/json' + + rooms = data['rooms'].to_s.split(',') + rooms.each do |room_id| + response = http_post "/v1/rooms/#{room_id}/messages", :body => message + case response.status + when 200..299 + else raise_config_error "HTTP Error: #{response.status} #{response.body}" + end + end + end + +end diff --git a/test/chatwork_test.rb b/test/chatwork_test.rb new file mode 100644 index 000000000..ae9559be7 --- /dev/null +++ b/test/chatwork_test.rb @@ -0,0 +1,56 @@ +require File.expand_path('../helper', __FILE__) + +class ChatWorkTest < Service::TestCase + def setup + @stubs = Faraday::Adapter::Test::Stubs.new + @token = 'TOooooooooooooooooooooooooooOKEN' + @roomid = 'ROOM_ID' + @config = { + "token" => @token, + "rooms" => @roomid + } + end + + def test_push + @stubs.post "/v1/rooms/#{@roomid}/messages" do |env| + assert_equal 'api.chatwork.com', env[:url].host + assert_equal 'https', env[:url].scheme + assert_equal 'application/json', env[:request_headers]['Content-Type'] + assert_equal 'push', env[:request_headers]['X-GitHub-Event'] + assert_equal :post, env[:method] + [200, {}, ''] + end + + service(:push, @config).receive_push + end + + def test_pull + @stubs.post "/v1/rooms/#{@roomid}/messages" do |env| + assert_equal 'api.chatwork.com', env[:url].host + assert_equal 'https', env[:url].scheme + assert_equal 'application/json', env[:request_headers]['Content-Type'] + assert_equal 'pull_request', env[:request_headers]['X-GitHub-Event'] + assert_equal :post, env[:method] + [200, {}, ''] + end + + service(:pull_request, @config).receive_pull_request + end + + def test_issues + @stubs.post "/v1/rooms/#{@roomid}/messages" do |env| + assert_equal 'api.chatwork.com', env[:url].host + assert_equal 'https', env[:url].scheme + assert_equal 'application/json', env[:request_headers]['Content-Type'] + assert_equal 'issues', env[:request_headers]['X-GitHub-Event'] + assert_equal :post, env[:method] + [200, {}, ''] + end + + service(:issues, @config).receive_issues + end + + def service(*args) + super Service::ChatWork, *args + end +end