From d224aab96290766a5eee6ed6f8cfc86b3e0ae294 Mon Sep 17 00:00:00 2001 From: dragon3 Date: Mon, 7 Apr 2014 09:42:42 +0900 Subject: [PATCH 1/2] Add Typetalk service --- docs/typetalk | 13 ++++++++++ lib/services/typetalk.rb | 53 ++++++++++++++++++++++++++++++++++++++++ test/typetalk_test.rb | 48 ++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 docs/typetalk create mode 100644 lib/services/typetalk.rb create mode 100644 test/typetalk_test.rb 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..22fb15fe4 --- /dev/null +++ b/lib/services/typetalk.rb @@ -0,0 +1,53 @@ +class Service::Typetalk < Service + 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 + From 330490a6153c9b54722acd53ba7ddf9bb6b6d50c Mon Sep 17 00:00:00 2001 From: dragon3 Date: Tue, 8 Apr 2014 09:08:07 +0900 Subject: [PATCH 2/2] Change base class from Service to Service::HttpPost --- lib/services/typetalk.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/typetalk.rb b/lib/services/typetalk.rb index 22fb15fe4..46461f9d2 100644 --- a/lib/services/typetalk.rb +++ b/lib/services/typetalk.rb @@ -1,4 +1,4 @@ -class Service::Typetalk < Service +class Service::Typetalk < Service::HttpPost string :client_id, :topic, :restrict_to_branch password :client_secret white_list :topic, :restrict_to_branch pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy