File: hook_server.rb

package info (click to toggle)
jenkins-debian-glue 0.23.6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: sh: 2,636; ruby: 543; makefile: 30; python: 24
file content (86 lines) | stat: -rwxr-xr-x 2,035 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env ruby

require 'httparty'
require 'json'
require 'net/http'
require 'sinatra'

set :port, 8042

## config
CONFIG = YAML.load_file('config.yml')
TOKEN = CONFIG["token"] || "unconfigured"
CAUSE = CONFIG["cause"] || "git_commit_triggered"
SERVER = CONFIG["server"] || "https://jenkins/"


## helper functions
def trigger_job(job, branch)
  uri = URI("#{SERVER}job/#{job}/buildWithParameters")

  req = Net::HTTP::Post.new(uri.path)
  req.set_form_data('token'  => TOKEN,
                    'cause'  => CAUSE,
                    'branch' => branch)

  res = Net::HTTP.start(uri.host, uri.port,
                        :use_ssl => uri.scheme == 'https',
                        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
    http.request(req)
    end

  case res
  when Net::HTTPSuccess, Net::HTTPRedirection
    p "OK"
  else
    res.value
  end
end


def job_names
  json = HTTParty.get("#{SERVER}/api/json")
  json["jobs"].map {|job| job["name"]}
end


def job_details(job_name)
  HTTParty.get("#{SERVER}/job/#{job_name}/api/json")
end


def build_details(job_name, build_number)
  HTTParty.get("#{SERVER}/job/#{job_name}/#{build_number}/api/json")
end

## main sinatra app

# this is where GitHub actually ends up at, configured as
# WebHook URL at https://github.com/$OWNER/$PROJECT/settings/hooks
post '/trigger' do
  push = JSON.parse(params[:payload])
  # p "JSON data: #{push.inspect}"
  # p JSON.pretty_generate(push)

  project = push["repository"]["name"]
  project = project + "-source" # we use $job-source as default entry point
  branch = push["ref"]

  # make sure we do not trigger a build with a branch that was just deleted
  deleted = push["deleted"]

  if deleted
    puts "branch #{branch} was deleted, not triggering build"
  elsif job_names.include? project
    trigger_job(project, branch)
  else
    puts "no such job #{project}"
  end
end

# debugging helper
post '/debug' do
  puts params[:payload]
end

## END OF FILE #################################################################