gitlab.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/bin/sh
  2. # Functions
  3. die() { echo "error: $@" 1>&2 ; exit 1; }
  4. confDie() { echo "error: $@ Check the server configuration!" 1>&2 ; exit 2; }
  5. debug() {
  6. [ "$debug" = "true" ] && echo "debug: $@"
  7. }
  8. # Validate global configuration
  9. [ -z "$GITLAB_TOKEN" ] && confDie "GITLAB_TOKEN not set."
  10. # Validate Gitlab hook
  11. [ "$x_gitlab_token" != "$GITLAB_TOKEN" ] && die "bad hook token"
  12. # Validate parameters
  13. payload=$1
  14. payload="$(echo "$payload"|tr -d '\n')"
  15. [ -z "$payload" ] && die "missing request payload"
  16. payload_type=$(echo $payload | jq type -r)
  17. [ $? != 0 ] && die "bad body format: expecting JSON"
  18. [ ! $payload_type = "object" ] && die "bad body format: expecting JSON object but having $payload_type"
  19. debug "received payload: $payload"
  20. # Extract values
  21. object_kind=$(echo $payload | jq .object_kind -r)
  22. [ $? != 0 -o "$object_kind" = "null" ] && die "unable to extract 'object_kind' from JSON payload"
  23. # Do something with the payload:
  24. # Here create a simple notification when a push has occured
  25. if [ "$object_kind" = "push" ]
  26. then
  27. username=$(echo $payload | jq .user_name -r)
  28. git_ssh_url=$(echo $payload | jq .project.git_ssh_url -r)
  29. total_commits_count=$(echo $payload | jq .total_commits_count -r)
  30. echo "notify: $username push $total_commits_count commit(s) on $git_ssh_url"
  31. fi