#! /bin/bash # https://unix.stackexchange.com/a/423052 function find_free_port { comm -23 <(seq 49152 65535 | sort) <(ss -Htan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1 } # gen_webhook "REPO_URL" #function gen_webhook { # echo "{ \"project\": { \"git_http_url\": \"$1\" } }" #} function gen_webhook() { export repo_url="$2" envsubst < "$1" } # send_webhook "ENDPOINT" "PAYLOAD" "SECRET" "HEADER" # ENDPOINT: where to send the request # PAYLOAD: POST body # SECRET: the secret for this transaction # HEADER: where to store the secret function send_webhook { echo "$2" > $TMPFILE # We can make a few attempts, just in case the webserver hasn't started yet n=0 while [[ "$status" != "0" ]]; do if [ $n -eq 3 ]; then # Failed to reach server after 3 attempts return 1; fi # --data-binary so that newlines aren't broken # (otherwise, signature won't match) run curl --header "Content-Type: application/json" \ --header ""$4": "$3"" \ --request POST \ --data-binary @$TMPFILE \ -s -w "%{http_code}" \ "$1" # Requested succeeded, break out of loop if [ $status -eq 0 ]; then echo "$output" if [[ ! "$output" = 200 ]]; then return 2; fi return 0; fi ((n++)) done } # https://stackoverflow.com/a/7385197 function hash_hmac { digest="$1" data="$2" key="$3" shift 3 # Don't print (stdin)= ... echo -n "$data" | openssl dgst "-$digest" -hmac "$key" | awk '{print $2}' }