adding environnent script

master
Izimic 2020-11-27 02:39:27 +01:00
parent a219ab19f5
commit 62f1a3215d
5 changed files with 84 additions and 12 deletions

14
src/gitea.cr Normal file
View File

@ -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

13
src/github.cr Normal file
View File

@ -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

13
src/gitlab.cr Normal file
View File

@ -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

View File

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

View File

@ -5,10 +5,12 @@ require "./payload.cr"
VERSION = 0.1 VERSION = 0.1
port = 3000 port = 3000
storage = "webhooksd-data"
args = [] of String args = [] of String
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "usage: webhooksd <scriptfile> [option]" parser.banner = "usage: webhooksd [option]"
parser.on "-v", "--version", "Show version" do parser.on "-v", "--version", "Show version" do
puts "version #{VERSION}" puts "version #{VERSION}"
exit exit
@ -23,6 +25,10 @@ OptionParser.parse do |parser|
port = p.to_i port = p.to_i
end end
parser.on "-s STORAGE", "--storage=STORAGE", "Default: #{storage}" do |s|
storage = s
end
parser.invalid_option do |flag| parser.invalid_option do |flag|
STDERR.puts "ERROR: #{flag} is not a valid option." STDERR.puts "ERROR: #{flag} is not a valid option."
STDERR.puts parser STDERR.puts parser
@ -32,19 +38,14 @@ OptionParser.parse do |parser|
parser.unknown_args do |x| parser.unknown_args do |x|
args = x args = x
if args.size < 1 if args.size != 0
puts parser puts parser
exit 1 exit 1
end end
end end
end end
scriptfile = args[0] scriptfile = "on-push"
if File.exists?(scriptfile) == false
STDERR.puts "ERROR: #{scriptfile} don't exists."
exit 2
end
server = HTTP::Server.new do |context| server = HTTP::Server.new do |context|
if context.request.method != "POST" || context.request.path != "/" if context.request.method != "POST" || context.request.path != "/"
@ -52,9 +53,25 @@ server = HTTP::Server.new do |context|
else else
payload = Payload.new(context.request) 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" context.response.content_type = "text/plain"
if Process.run command: "sh", args: [scriptfile], shell: true, status = Process.run command: "sh", args: [scriptfile], shell: true,
error: STDERR, output: STDOUT, chdir: Process::INITIAL_PWD error: STDERR, output: STDOUT, chdir: path_project
end
end
if status
context.response.print "SUCCESS" context.response.print "SUCCESS"
else else
context.response.print "FAILURE" context.response.print "FAILURE"