From 62f1a3215dcd7cc888837a71368a4657509bb483 Mon Sep 17 00:00:00 2001 From: Izimic Date: Fri, 27 Nov 2020 02:39:27 +0100 Subject: [PATCH] adding environnent script --- src/gitea.cr | 14 ++++++++++++++ src/github.cr | 13 +++++++++++++ src/gitlab.cr | 13 +++++++++++++ src/payload.cr | 17 ++++++++++++++++- src/webhooksd.cr | 39 ++++++++++++++++++++++++++++----------- 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/gitea.cr create mode 100644 src/github.cr create mode 100644 src/gitlab.cr diff --git a/src/gitea.cr b/src/gitea.cr new file mode 100644 index 0000000..be5570c --- /dev/null +++ b/src/gitea.cr @@ -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 + diff --git a/src/github.cr b/src/github.cr new file mode 100644 index 0000000..1506b28 --- /dev/null +++ b/src/github.cr @@ -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 diff --git a/src/gitlab.cr b/src/gitlab.cr new file mode 100644 index 0000000..45f5493 --- /dev/null +++ b/src/gitlab.cr @@ -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 diff --git a/src/payload.cr b/src/payload.cr index c1655f1..745c90c 100644 --- a/src/payload.cr +++ b/src/payload.cr @@ -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 diff --git a/src/webhooksd.cr b/src/webhooksd.cr index c43f8ca..b5f9827 100644 --- a/src/webhooksd.cr +++ b/src/webhooksd.cr @@ -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 [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) - context.response.content_type = "text/plain" - if Process.run command: "sh", args: [scriptfile], shell: true, - error: STDERR, output: STDOUT, chdir: Process::INITIAL_PWD + 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" + 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"