diff --git a/docs/typetalk b/docs/typetalk new file mode 100644 index 000000000..2353b1f79 --- /dev/null +++ b/docs/typetalk @@ -0,0 +1,13 @@ +Typetalk +====== + +Posts a message to [Typetalk](https://typetalk.in) topics when you push to GitHub. + +Install Notes +------------- + +1. Need to [register new application](https://typetalk.in/my/develop/applications) that authorises GitHub to access to the Typetalk API. (Grant type must be "Client Credentials") +2. Enter your credentials. + - client_id + - client_secret +3. Enter topic id to post messages. diff --git a/lib/services/typetalk.rb b/lib/services/typetalk.rb new file mode 100644 index 000000000..46461f9d2 --- /dev/null +++ b/lib/services/typetalk.rb @@ -0,0 +1,53 @@ +class Service::Typetalk < Service::HttpPost + string :client_id, :topic, :restrict_to_branch + password :client_secret + white_list :topic, :restrict_to_branch + + default_events :push + + url "http://typetalk.in" + logo_url "https://deeb7lj8m1sjw.cloudfront.net/1.3.5/assets/images/common/logo.png" + + def receive_event + raise_config_error "Missing 'client_id'" if data['client_id'].to_s == '' + raise_config_error "Missing 'client_secret'" if data['client_secret'].to_s == '' + raise_config_error "Missing 'topic'" if data['topic'].to_s == '' + + if event.to_s == 'push' + branch = payload['ref'].split('/').last + branch_restriction = data['restrict_to_branch'].to_s + if branch_restriction.length > 0 && branch_restriction.index(branch) == nil + return + end + end + + http.url_prefix = 'https://typetalk.in' + http.headers['X-GitHub-Event'] = event.to_s + + # get an access_token + res = http_post '/oauth2/access_token', + { :client_id => data['client_id'], + :client_secret => data['client_secret'], + :grant_type => 'client_credentials', + :scope => 'topic.post',} + + json = JSON.parse(res.body) + http.headers['Authorization'] = "Bearer #{json['access_token']}" + + topics = data['topic'].to_s.split(",") + topics.each do |topic| + params = { + :message => format_pushed_message(payload) + } + res = http_post "/api/v1/topics/#{topic}", params + if res.status < 200 || res.status > 299 + raise_config_error + end + end + end + + def format_pushed_message(payload) + branch = payload['ref'].split('/').last + return "#{payload['pusher']['name']} has pushed #{payload['commits'].size} commit(s) to #{branch} at #{payload['repository']['name']}\n#{payload['compare']}" + end +end diff --git a/test/typetalk_test.rb b/test/typetalk_test.rb new file mode 100644 index 000000000..eb0738fec --- /dev/null +++ b/test/typetalk_test.rb @@ -0,0 +1,48 @@ +require File.expand_path('../helper', __FILE__) + +class TypetalkTest < Service::TestCase + def setup + @stubs = Faraday::Adapter::Test::Stubs.new + end + + def test_push + @stubs.post "/oauth2/access_token" do |env| + form = Faraday::Utils.parse_query(env[:body]) + assert_equal 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', form['client_id'] + assert_equal 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', form['client_secret'] + assert_equal 'client_credentials', form['grant_type'] + assert_equal 'topic.post', form['scope'] + [200, {}, '{ "access_token": "TestToken" }'] + end + @stubs.post "/api/v1/topics/1" do |env| + form = Faraday::Utils.parse_query(env[:body]) + headers = env[:request_headers] + assert_equal 'Bearer TestToken', headers['Authorization'] + assert_equal "dragon3 has pushed 2 commit(s) to master at dragon3/github-services\nhttps://github.com/dragon3/github-services/compare/06f63b43050935962f84fe54473a7c5de7977325...06f63b43050935962f84fe54473a7c5de7977326", form['message'] + [200, {}, ''] + end + + svc = service({'client_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', + 'client_secret' => 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', + 'topic' => '1'}, payload_for_test) + svc.receive_event + end + + def service(*args) + super Service::Typetalk, *args + end + + def payload_for_test + { + 'ref' => 'refs/heads/master', + 'compare' => 'https://github.com/dragon3/github-services/compare/06f63b43050935962f84fe54473a7c5de7977325...06f63b43050935962f84fe54473a7c5de7977326', + 'pusher' => { 'name' => 'dragon3', }, + 'commits' => [ + {'id' => '06f63b43050935962f84fe54473a7c5de7977325'}, + {'id' => '06f63b43050935962f84fe54473a7c5de7977326'}], + 'repository' => {'name' => 'dragon3/github-services'}, + } + end + +end +