Class: Janky::GitHub::Receiver

Inherits:
Object
  • Object
show all
Defined in:
lib/janky/github/receiver.rb

Overview

Rack app handling GitHub Post-Receive [1] requests.

The JSON payload is parsed into a GitHub::Payload. We then find the associated Repository record based on the Payload’s repository git URL and create the associated records: Branch, Commit and Build.

Finally, we trigger a new Jenkins build.

[1]: help.github.com/post-receive-hooks/

Instance Method Summary collapse

Constructor Details

#initialize(secret) ⇒ Receiver

Returns a new instance of Receiver.



13
14
15
# File 'lib/janky/github/receiver.rb', line 13

def initialize(secret)
  @secret = secret
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
# File 'lib/janky/github/receiver.rb', line 17

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



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
# File 'lib/janky/github/receiver.rb', line 21

def call!(env)
  @request = Rack::Request.new(env)

  if !valid_signature?
    return Rack::Response.new("Invalid signature", 403).finish
  end

  if @request.content_type != "application/json"
    return Rack::Response.new("Invalid Content-Type", 400).finish
  end

  if !payload.head_commit
    return Rack::Response.new("Ignored", 400).finish
  end

  result = BuildRequest.handle(
    payload.uri,
    payload.branch,
    payload.pusher,
    payload.head_commit,
    payload.compare,
    @request.POST["room"]
  )

  Rack::Response.new("OK: #{result}", 201).finish
end

#dataObject



59
60
61
# File 'lib/janky/github/receiver.rb', line 59

def data
  @data ||= data!
end

#data!Object



63
64
65
66
67
# File 'lib/janky/github/receiver.rb', line 63

def data!
  body = ""
  @request.body.each { |chunk| body << chunk }
  body
end

#payloadObject



55
56
57
# File 'lib/janky/github/receiver.rb', line 55

def payload
  @payload ||= GitHub::Payload.parse(data)
end

#valid_signature?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/janky/github/receiver.rb', line 48

def valid_signature?
  digest    = OpenSSL::Digest::SHA1.new
  signature = @request.env["HTTP_X_HUB_SIGNATURE"].split("=").last

  signature == OpenSSL::HMAC.hexdigest(digest, @secret, data)
end