add Gogs compatibility

This commit is contained in:
Paul Liverman III 2018-05-11 13:26:17 -07:00
parent a561edd7a2
commit 6158011da9

View File

@ -2,9 +2,9 @@ lapis = require "lapis"
config = require("lapis.config").get! config = require("lapis.config").get!
import respond_to, json_params from require "lapis.application" import respond_to, json_params from require "lapis.application"
import hmac_sha1 from require "lapis.util.encoding" import hmac_sha1, hmac_sha256 from require "lapis.util.encoding"
import GithookLogs from require "models"
import encode from require "cjson" import encode from require "cjson"
import GithookLogs from require "models"
import locate, autoload, registry from require "locator" import locate, autoload, registry from require "locator"
import settings from autoload "utility" import settings from autoload "utility"
import execute from locate "utility.shell" import execute from locate "utility.shell"
@ -93,8 +93,17 @@ unauthorized = ->
message: "invalid credentials or no credentials were sent" message: "invalid credentials or no credentials were sent"
} }
invalid = (reason) ->
return status: 400, json: {
status: "invalid request"
message: reason
}
class extends lapis.Application class extends lapis.Application
[githook: "/githook"]: respond_to { [githook: "/githook"]: respond_to {
before: =>
@branch = config.githook_branch or settings["githook.branch"] or "master"
GET: => GET: =>
unless settings["githook.allow_get"] unless settings["githook.allow_get"]
return status: 405, json: { return status: 405, json: {
@ -105,37 +114,33 @@ class extends lapis.Application
unless settings["githook.run_without_auth"] unless settings["githook.run_without_auth"]
return unauthorized! return unauthorized!
branch = config.githook_branch or settings["githook.branch"] or "master" @results = run_update(@branch)
@results = run_update branch
return render: locate "views.githook_get" return render: locate "views.githook_get"
POST: json_params => POST: json_params =>
branch = config.githook_branch or settings["githook.branch"] or "master"
if config.githook_secret if config.githook_secret
ngx.req.read_body! ngx.req.read_body!
if body = ngx.req.get_body_data! if body = ngx.req.get_body_data!
authorized = const_compare "sha1=#{hex_dump hmac_sha1 config.githook_secret, body}", @req.headers["X-Hub-Signature"] local authorized
if github_hash = @req.headers["X-Hub-Signature"]
authorized = const_compare "sha1=#{hex_dump hmac_sha1 config.githook_secret, body}", github_hash
elseif gogs_hash = @req.headers["X-Gogs-Signature"]
authorized = const_compare gogs_hash, hex_dump hmac_sha256 config.githook_secret, body
unless authorized unless authorized
return unauthorized! return unauthorized!
if @params.ref == "refs/heads/#{branch}" if @params.ref == "refs/heads/#{@branch}"
return run_update branch return run_update(@branch)
elseif @params.ref == nil elseif @params.ref == nil
return status: 400, json: { return invalid "'ref' not defined in request body"
status: "invalid request"
message: "'ref' not defined in request body"
}
else else
return ignored branch return ignored(@branch)
else else
return status: 400, json: { return invalid "no request body"
status: "invalid request"
message: "no request body"
}
elseif settings["githook.run_without_auth"] elseif settings["githook.run_without_auth"]
if @params.ref == "refs/heads/#{branch}" if @params.ref == "refs/heads/#{@branch}"
return run_update branch return run_update(@branch)
else else
return ignored branch return ignored(@branch)
else else
return unauthorized! return unauthorized!
} }