Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/typetalk
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions lib/services/typetalk.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions test/typetalk_test.rb
Original file line number Diff line number Diff line change
@@ -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

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