Skip to content

Commit

Permalink
Port V3 webhook work to V2
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorcoran committed Sep 14, 2018
1 parent 28e90cd commit 5ca2d89
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 63 deletions.
23 changes: 13 additions & 10 deletions lib/travis/api/v3/github.rb
Expand Up @@ -2,21 +2,24 @@

module Travis::API::V3
class GitHub
def self.config
@config ||= Travis::Config.load
end

EVENTS = %i(push pull_request issue_comment public member create delete repository)

DEFAULT_OPTIONS = {
client_id: Travis.config.oauth2.try(:client_id),
client_secret: Travis.config.oauth2.try(:client_secret),
scopes: Travis.config.oauth2.try(:scope).to_s.split(?,),
client_id: config.oauth2.try(:client_id),
client_secret: config.oauth2.try(:client_secret),
scopes: config.oauth2.try(:scope).to_s.split(?,),
user_agent: "Travis-API/3 Travis-CI/0.0.1 GH/#{GH::VERSION}",
origin: Travis.config.host,
api_url: Travis.config.github.api_url,
web_url: Travis.config.github.api_url.gsub(%r{\A(https?://)(?:api\.)?([^/]+)(?:/.*)?\Z}, '\1\2'),
ssl: Travis.config.ssl.to_h.merge(Travis.config.github.ssl || {}).compact
origin: config.host,
api_url: config.github.api_url,
web_url: config.github.api_url.gsub(%r{\A(https?://)(?:api\.)?([^/]+)(?:/.*)?\Z}, '\1\2'),
ssl: config.ssl.to_h.merge(config.github.ssl || {}).compact
}
private_constant :DEFAULT_OPTIONS

EVENTS = %i(push pull_request issue_comment public member create delete repository)
private_constant :EVENTS

HOOKS_URL = "repos/%s/hooks"
private_constant :HOOKS_URL

Expand Down
59 changes: 12 additions & 47 deletions lib/travis/github/services/set_hook.rb
@@ -1,66 +1,31 @@
require 'travis/github'
require 'travis/services/base'
require 'travis/api/v3'
require 'travis/api/v3/github'

module Travis
module Github
module Services
class SetHook < Travis::Services::Base
EVENTS = %i(push pull_request issue_comment public member create delete repository)

register :github_set_hook

def run
Github.authenticated(current_user) do
update
end
v3_github.set_hook(repo, active?)
end

private

def repo
@repo ||= run_service(:find_repo, id: params[:id])
end

def active?
params[:active]
end

def hook
@hook ||= find || create
end

def update
GH.patch(hook_url, payload) unless hook['active'] == active?
end

def find
GH[hooks_url].detect { |hook| hook['name'] == 'travis' && hook['config']['domain'] == domain }
end

def create
GH.post(hooks_url, payload)
end

def payload
{
:name => 'travis',
:events => EVENTS,
:active => active?,
:config => { :user => current_user.login, :token => current_user.tokens.first.token, :domain => domain }
}
end

def hooks_url
"repos/#{repo.slug}/hooks"
end
def active?
params[:active]
end

def hook_url
hook['_links']['self']['href']
end
def v3_github
@v3_github ||= Travis::API::V3::GitHub.new(current_user, current_user.github_oauth_token)
end

def domain
Travis.config.service_hook_url || ''
end
def repo
@repo ||= run_service(:find_repo, id: params[:id])
end
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions spec/integration/v2/hooks_spec.rb
@@ -1,4 +1,5 @@
require 'travis/testing/payloads'
require 'travis/api/v3/github'

describe 'Hooks', set_app: true do
before(:each) do
Expand All @@ -21,20 +22,20 @@

let :payload do
{
:name => 'travis',
:events => Travis::Github::Services::SetHook::EVENTS,
:name => 'web',
:events => Travis::API::V3::GitHub::EVENTS,
:active => true,
:config => { :user => user.login, :token => user.tokens.first.token, :domain => 'listener.travis-ci.org' }
:config => { url: 'notify.travis-ci.org' }
}
end

before(:each) do
Travis.config.service_hook_url = 'listener.travis-ci.org'
Travis.config.service_hook_url = 'notify.travis-ci.org'
stub_request(:get, "https://api.github.com/repos/#{repo.slug}/hooks?per_page=100").to_return(status: 200, body: '[]')
stub_request(:post, "https://api.github.com/repos/#{repo.slug}/hooks")
end

it 'sets the hook' do
GH.stubs(:[]).returns([])
GH.expects(:post).with(target, payload).returns(GH.load(PAYLOADS[:github][:hook_active]))
response = put 'hooks', { hook: { id: hook.id, active: 'true' } }, headers
repo.reload.active?.should == true
response.should be_successful
Expand Down

0 comments on commit 5ca2d89

Please sign in to comment.