adding environnent script
parent
a219ab19f5
commit
62f1a3215d
|
@ -0,0 +1,14 @@
|
|||
require "json"
|
||||
|
||||
class Gitea::Repository
|
||||
include JSON::Serializable
|
||||
|
||||
property full_name : String
|
||||
end
|
||||
|
||||
class Gitea::Payload
|
||||
include JSON::Serializable
|
||||
|
||||
property repository : Repository
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
require "json"
|
||||
|
||||
class Github::Repository
|
||||
include JSON::Serializable
|
||||
|
||||
property full_name : String
|
||||
end
|
||||
|
||||
class Github::Payload
|
||||
include JSON::Serializable
|
||||
|
||||
property repository : Repository
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
require "json"
|
||||
|
||||
class Gitlab::Project
|
||||
include JSON::Serializable
|
||||
|
||||
property path_with_namespace : String
|
||||
end
|
||||
|
||||
class Gitlab::Payload
|
||||
include JSON::Serializable
|
||||
|
||||
property project : Project
|
||||
end
|
|
@ -1,25 +1,40 @@
|
|||
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
|
||||
|
||||
@content = req.body.not_nil!.gets_to_end.to_s
|
||||
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,10 +5,12 @@ require "./payload.cr"
|
|||
VERSION = 0.1
|
||||
port = 3000
|
||||
|
||||
storage = "webhooksd-data"
|
||||
|
||||
args = [] of String
|
||||
|
||||
OptionParser.parse do |parser|
|
||||
parser.banner = "usage: webhooksd <scriptfile> [option]"
|
||||
parser.banner = "usage: webhooksd [option]"
|
||||
parser.on "-v", "--version", "Show version" do
|
||||
puts "version #{VERSION}"
|
||||
exit
|
||||
|
@ -23,6 +25,10 @@ OptionParser.parse do |parser|
|
|||
port = p.to_i
|
||||
end
|
||||
|
||||
parser.on "-s STORAGE", "--storage=STORAGE", "Default: #{storage}" do |s|
|
||||
storage = s
|
||||
end
|
||||
|
||||
parser.invalid_option do |flag|
|
||||
STDERR.puts "ERROR: #{flag} is not a valid option."
|
||||
STDERR.puts parser
|
||||
|
@ -32,19 +38,14 @@ OptionParser.parse do |parser|
|
|||
parser.unknown_args do |x|
|
||||
args = x
|
||||
|
||||
if args.size < 1
|
||||
if args.size != 0
|
||||
puts parser
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
scriptfile = args[0]
|
||||
|
||||
if File.exists?(scriptfile) == false
|
||||
STDERR.puts "ERROR: #{scriptfile} don't exists."
|
||||
exit 2
|
||||
end
|
||||
scriptfile = "on-push"
|
||||
|
||||
server = HTTP::Server.new do |context|
|
||||
if context.request.method != "POST" || context.request.path != "/"
|
||||
|
@ -52,9 +53,25 @@ server = HTTP::Server.new do |context|
|
|||
else
|
||||
payload = Payload.new(context.request)
|
||||
|
||||
path_project = storage + "/" + payload.project
|
||||
path_scriptfile = path_project + "/" + scriptfile
|
||||
|
||||
status = false
|
||||
|
||||
if File.exists?(path_project) == false
|
||||
STDERR.puts "ERROR: Project #{payload.project} not found"
|
||||
status = false
|
||||
else
|
||||
if File.exists?(path_scriptfile) == false
|
||||
STDERR.puts "ERROR: Scriptfile not found"
|
||||
status = false
|
||||
else
|
||||
context.response.content_type = "text/plain"
|
||||
if Process.run command: "sh", args: [scriptfile], shell: true,
|
||||
error: STDERR, output: STDOUT, chdir: Process::INITIAL_PWD
|
||||
status = Process.run command: "sh", args: [scriptfile], shell: true,
|
||||
error: STDERR, output: STDOUT, chdir: path_project
|
||||
end
|
||||
end
|
||||
if status
|
||||
context.response.print "SUCCESS"
|
||||
else
|
||||
context.response.print "FAILURE"
|
||||
|
|
Loading…
Reference in New Issue