From 908232f2faa206957f1bee106b6299ff6c9f39f3 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Fri, 17 Jan 2020 14:48:59 +0000 Subject: [PATCH] chore(): add script examples --- scripts/examples/github.sh | 37 +++++++++++++++++++++++++++++++++++++ scripts/examples/gitlab.sh | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 scripts/examples/github.sh create mode 100755 scripts/examples/gitlab.sh diff --git a/scripts/examples/github.sh b/scripts/examples/github.sh new file mode 100755 index 0000000..2883395 --- /dev/null +++ b/scripts/examples/github.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +# Functions +die() { echo "error: $@" 1>&2 ; exit 1; } +confDie() { echo "error: $@ Check the server configuration!" 1>&2 ; exit 2; } +debug() { + [ "$debug" = "true" ] && echo "debug: $@" +} + +# Validate global configuration +[ -z "$GITHUB_SECRET" ] && confDie "GITHUB_SECRET not set." + +# Validate Github hook +signature=$(echo -n "$1" | openssl sha1 -hmac "$GITHUB_SECRET" | sed -e 's/^.* //') +[ "$signature" != "$x_hub_signature" ] && die "bad hook signature: expecting $x_hub_signature and got $signature" + +# Validate parameters +payload=$1 +[ -z "$payload" ] && die "missing request payload" +payload_type=$(echo $payload | jq type -r) +[ $? != 0 ] && die "bad body format: expecting JSON" +[ ! $payload_type = "object" ] && die "bad body format: expecting JSON object but having $payload_type" + +debug "received payload: $payload" + +# Extract values +action=$(echo $payload | jq .action -r) +[ $? != 0 -o "$action" = "null" ] && die "unable to extract 'action' from JSON payload" + +# Do something with the payload: +# Here create a simple notification when an issue has been published +if [ "$action" = "opened" ] +then + issue_url=$(echo $payload | jq .issue.url -r) + sender=$(echo $payload | jq .sender.login -r) + echo "notify: New issue from $sender: $issue_url" +fi \ No newline at end of file diff --git a/scripts/examples/gitlab.sh b/scripts/examples/gitlab.sh new file mode 100755 index 0000000..05eafe6 --- /dev/null +++ b/scripts/examples/gitlab.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +# Functions +die() { echo "error: $@" 1>&2 ; exit 1; } +confDie() { echo "error: $@ Check the server configuration!" 1>&2 ; exit 2; } +debug() { + [ "$debug" = "true" ] && echo "debug: $@" +} + +# Validate global configuration +[ -z "$GITLAB_TOKEN" ] && confDie "GITLAB_TOKEN not set." + +# Validate Gitlab hook +[ "$x_gitlab_token" != "$GITLAB_TOKEN" ] && die "bad hook token" + +# Validate parameters +payload=$1 +[ -z "$payload" ] && die "missing request payload" +payload_type=$(echo $payload | jq type -r) +[ $? != 0 ] && die "bad body format: expecting JSON" +[ ! $payload_type = "object" ] && die "bad body format: expecting JSON object but having $payload_type" + +debug "received payload: $payload" + +# Extract values +object_kind=$(echo $payload | jq .object_kind -r) +[ $? != 0 -o "$object_kind" = "null" ] && die "unable to extract 'object_kind' from JSON payload" + +# Do something with the payload: +# Here create a simple notification when a push has occured +if [ "$object_kind" = "push" ] +then + username=$(echo $payload | jq .user_name -r) + git_ssh_url=$(echo $payload | jq .project.git_ssh_url -r) + total_commits_count=$(echo $payload | jq .total_commits_count -r) + echo "notify: $username push $total_commits_count commit(s) on $git_ssh_url" +fi \ No newline at end of file