Skip to content

Commit

Permalink
Add *two* new samples for CIs and deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Apr 4, 2014
1 parent a0dceac commit 5b3fb77
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/ruby/building-a-ci-server/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"

gem "json", "1.7.7"
gem 'sinatra', '~> 1.3.5'
gem "shotgun"
gem "octokit", '~> 3.0'
32 changes: 32 additions & 0 deletions api/ruby/building-a-ci-server/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GEM
remote: http://rubygems.org/
specs:
addressable (2.3.6)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
json (1.7.7)
multipart-post (2.0.0)
octokit (3.0.0)
sawyer (~> 0.5.3)
rack (1.5.2)
rack-protection (1.5.2)
rack
sawyer (0.5.4)
addressable (~> 2.3.5)
faraday (~> 0.8, < 0.10)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.6)
rack (~> 1.4)
rack-protection (~> 1.3)
tilt (~> 1.3, >= 1.3.3)
tilt (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
json (= 1.7.7)
octokit (~> 3.0)
shotgun
sinatra (~> 1.3.5)
2 changes: 2 additions & 0 deletions api/ruby/building-a-ci-server/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "./server"
run CITutorial
35 changes: 35 additions & 0 deletions api/ruby/building-a-ci-server/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'sinatra/base'
require 'json'
require 'octokit'

class CITutorial < Sinatra::Base

# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
# Instead, set and test environment variables, like below
ACCESS_TOKEN = ENV['MY_PERSONAL_TOKEN']

before do
@client ||= Octokit::Client.new(:access_token => ACCESS_TOKEN)
end

post '/event_handler' do
@payload = JSON.parse(params[:payload])

case request.env['HTTP_X_GITHUB_EVENT']
when "pull_request"
if @payload["action"] == "opened"
process_pull_request(@payload["pull_request"])
end
end
end

helpers do
def process_pull_request(pull_request)
puts "Processing pull request..."
@client.create_status(pull_request['head']['repo']['full_name'], pull_request['head']['sha'], 'pending')
sleep 2 # do busy work...
@client.create_status(pull_request['head']['repo']['full_name'], pull_request['head']['sha'], 'success')
puts "Pull request processed!"
end
end
end
6 changes: 6 additions & 0 deletions api/ruby/delivering-deployments/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source "http://rubygems.org"

gem "json", "1.7.7"
gem 'sinatra', '~> 1.3.5'
gem "shotgun"
gem "octokit", '~> 3.0'
32 changes: 32 additions & 0 deletions api/ruby/delivering-deployments/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
GEM
remote: http://rubygems.org/
specs:
addressable (2.3.6)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
json (1.7.7)
multipart-post (2.0.0)
octokit (3.0.0)
sawyer (~> 0.5.3)
rack (1.5.2)
rack-protection (1.5.2)
rack
sawyer (0.5.4)
addressable (~> 2.3.5)
faraday (~> 0.8, < 0.10)
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.6)
rack (~> 1.4)
rack-protection (~> 1.3)
tilt (~> 1.3, >= 1.3.3)
tilt (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
json (= 1.7.7)
octokit (~> 3.0)
shotgun
sinatra (~> 1.3.5)
2 changes: 2 additions & 0 deletions api/ruby/delivering-deployments/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "./server"
run DeploymentTutorial
51 changes: 51 additions & 0 deletions api/ruby/delivering-deployments/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'sinatra/base'
require 'json'
require 'octokit'

class DeploymentTutorial < Sinatra::Base

# !!! DO NOT EVER USE HARD-CODED VALUES IN A REAL APP !!!
# Instead, set and test environment variables, like below
ACCESS_TOKEN = ENV['MY_PERSONAL_TOKEN']

before do
@client ||= Octokit::Client.new(:access_token => ACCESS_TOKEN)
end

post '/event_handler' do
@payload = JSON.parse(params[:payload])

case request.env['HTTP_X_GITHUB_EVENT']
when "pull_request"
if @payload["action"] == "closed" && @payload["pull_request"]["merged"]
start_deployment(@payload["pull_request"])
end
when "deployment"
process_deployment
when "deployment_status"
update_deployment_status
end
end

helpers do
def start_deployment(pull_request)
user = pull_request['user']['login']
payload = JSON.generate(:environment => 'production', :deploy_user => user)
@client.create_deployment(pull_request['head']['repo']['full_name'], pull_request['head']['sha'], {:payload => payload, :description => "Deploying my sweet branch"})
end

def process_deployment
payload = JSON.parse(@payload['payload'])
# you can send this information to your chat room, monitor, pager, e.t.c.
puts "Processing '#{@payload['description']}' for #{payload['deploy_user']} to #{payload['environment']}"
sleep 2 # simulate work
@client.create_deployment_status("repos/#{@payload['repository']['full_name']}/deployments/#{@payload['id']}", 'pending')
sleep 2 # simulate work
@client.create_deployment_status("repos/#{@payload['repository']['full_name']}/deployments/#{@payload['id']}", 'success')
end

def update_deployment_status
puts "Deployment status for #{@payload['id']} is #{@payload['state']}"
end
end
end

0 comments on commit 5b3fb77

Please sign in to comment.