chore(): add script examples

This commit is contained in:
Nicolas Carlier 2020-01-17 14:48:59 +00:00
parent 4c7b73b987
commit 908232f2fa
2 changed files with 74 additions and 0 deletions

37
scripts/examples/github.sh Executable file
View File

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

37
scripts/examples/gitlab.sh Executable file
View File

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