webhooksd/src/payload.cr

41 lines
960 B
Crystal

require "json"
require "./gitea.cr"
require "./github.cr"
require "./gitlab.cr"
class Payload
include JSON::Serializable
property kind : String
property project : String
property content : String
def initialize(req : HTTP::Request)
@content = req.body.not_nil!.gets_to_end.to_s
agent = req.headers.fetch("User-Agent", "None")
if agent == "GiteaServer" && req.headers.has_key?("X-Gitea-Event")
@kind = "gitea"
content = Gitea::Payload.from_json @content
@project = content.repository.full_name
elsif agent.starts_with?("GitHub-Hookshot/") && req.headers.has_key?("X-Github-Event")
@kind = "github"
content = Github::Payload.from_json @content
@project = content.repository.full_name
elsif req.headers.has_key?("X-Gitlab-Event")
@kind = "gitlab"
content = Gitlab::Payload.from_json @content
@project = content.project.path_with_namespace
else
@kind = "undefined"
@project = "undefined"
end
end
end