Last active
May 23, 2019 10:04
-
-
Save rijnhard/d025de8f01876cdabb9f to your computer and use it in GitHub Desktop.
[webhook for Quay] #bitbucket #docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#first parameter needs to be webhook endpoint | |
#the credentials to check the Bitbucket rest API need to be inserted into the Curls. | |
( | |
LAST_COMMIT= | |
BRANCH_NAME= | |
while read from_ref to_ref ref_name; do | |
LAST_COMMIT=$to_ref | |
BRANCH_NAME=$ref_name | |
#git show $from_ref..$to_ref | sed 's/^/ /' | |
done | |
AUTHOR_EMAIL=$(git show --quiet --format="%ae" "$LAST_COMMIT" | sed -e "s/\(.*\)/\L\1/" -e "s/ //g") | |
COMMITER_EMAIL=$(git show --quiet --format="%ce" "$LAST_COMMIT" | sed -e "s/\(.*\)/\L\1/" -e "s/ //g") | |
AUTHOR_EMAIL_HASH=$(echo -n $AUTHOR_EMAIL | md5sum | cut -c 1-32) | |
MESSAGE=$(git show --quiet --format="%s" "$LAST_COMMIT" | sed -e 's/"/\\"/g') | |
AUTHOR_DETAILS=$(curl -sX GET \ | |
--noproxy localhost \ | |
-H "Authorization: Basic <stripped>" \ | |
"http://localhost:7990/rest/api/1.0/admin/users?filter=$AUTHOR_EMAIL&limit=1" \ | |
) | |
if [ 0 = $(echo $AUTHOR_DETAILS | awk -F'"size":' '{print substr($2,0,1)}') ]; then | |
AUTHOR_USER=$(git show --quiet --format="%an" "$LAST_COMMIT") | |
else | |
AUTHOR_USER=$(echo $AUTHOR_DETAILS | sed -n 's/.*"name"\:"\(.*\)","emailAddress".*/\1/p') | |
fi | |
echo "AUTHOR_EMAIL: $AUTHOR_EMAIL" | |
echo "COMMITER_EMAIL: $COMMITER_EMAIL" | |
echo "AUTHOR_USER: $AUTHOR_USER" | |
if [[ $AUTHOR_EMAIL = $COMMITER_EMAIL ]]; then | |
COMMITER_EMAIL_HASH=$AUTHOR_EMAIL_HASH | |
COMITTER_USER=$AUTHOR_USER | |
else | |
COMMITER_EMAIL_HASH=$(echo -n $COMMITER_EMAIL | md5sum | cut -c 1-32) | |
COMITTER_DETAILS=$(curl -sX GET \ | |
--noproxy localhost \ | |
-H "Authorization: Basic <stripped>" \ | |
"http://localhost:7990/rest/api/1.0/admin/users?filter=$COMMITER_EMAIL&limit=1" \ | |
) | |
if [ 0 = $(echo $COMITTER_DETAILS | awk -F'"size":' '{print substr($2,0,1)}') ]; then | |
COMITTER_USER=$(git show --quiet --format="%cn" "$LAST_COMMIT") | |
else | |
COMITTER_USER=$(echo $COMITTER_DETAILS | sed -n 's/.*"name"\:"\(.*\)","emailAddress".*/\1/p') | |
fi | |
fi | |
echo "COMITTER_USER: $COMITTER_USER" | |
PAYLOAD_GIT=$(cat <<-EOF | |
{ | |
"commit": "%H", | |
"ref": "$BRANCH_NAME", | |
"default_branch": "dev", | |
"commit_info": { | |
"url": "$STASH_BASE_URL/projects/$STASH_PROJECT_KEY/repos/$STASH_REPO_NAME/commits/%H", | |
"message": "%s", | |
"date": "%cI", | |
"author": { | |
"username": "$AUTHOR_USER", | |
"avatar_url": "http://gravatar.com/avatar/$AUTHOR_EMAIL_HASH.jpg", | |
"url": "$STASH_BASE_URL/users/$STASH_USER_NAME" | |
}, | |
"committer": { | |
"username": "$COMITTER_USER", | |
"avatar_url": "http://gravatar.com/avatar/$COMMITER_EMAIL_HASH.jpg", | |
"url": "$STASH_BASE_URL/users/$STASH_USER_NAME" | |
} | |
} | |
} | |
EOF | |
) | |
PAYLOAD_GIT=$(echo $PAYLOAD_GIT | tr -d '\n') | |
PAYLOAD=$(git show --quiet --format="$PAYLOAD_GIT" "$LAST_COMMIT") | |
for WEBHOOK_ENDPOINT in "$@" | |
do | |
curl -sX POST \ | |
-d "$PAYLOAD" \ | |
-H "Content-Type: application/json" \ | |
$WEBHOOK_ENDPOINT | |
done | |
) | tee -a /tmp/bitbucket-hooks-quay.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#first parameter needs to be webhook endpoint | |
#the credentials to check the Bitbucket rest API need to be inserted into the Curls. | |
LAST_COMMIT= | |
BRANCH_NAME= | |
while read from_ref to_ref ref_name; do | |
LAST_COMMIT=$to_ref | |
BRANCH_NAME=$ref_name | |
#git show $from_ref..$to_ref | sed 's/^/ /' | |
done | |
AUTHOR_EMAIL=$(git show --quiet --format="%ae" "$LAST_COMMIT" | sed -e "s/\(.*\)/\L\1/" -e "s/ //g") | |
COMMITER_EMAIL=$(git show --quiet --format="%ce" "$LAST_COMMIT" | sed -e "s/\(.*\)/\L\1/" -e "s/ //g") | |
AUTHOR_EMAIL_HASH=$(echo -n $AUTHOR_EMAIL | md5sum | cut -c 1-32) | |
MESSAGE=$(git show --quiet --format="%s" "$LAST_COMMIT" | sed -e 's/"/\\"/g') | |
AUTHOR_DETAILS=$(curl -sX GET \ | |
--noproxy localhost \ | |
-H "Authorization: Basic <stripped>" \ | |
"http://localhost:7990/rest/api/1.0/admin/users?filter=$AUTHOR_EMAIL&limit=1" \ | |
) | |
if [ 0 = $(echo $AUTHOR_DETAILS | awk -F'"size":' '{print substr($2,0,1)}') ]; then | |
AUTHOR_USER=$(git show --quiet --format="%an" "$LAST_COMMIT") | |
else | |
AUTHOR_USER=$(echo $AUTHOR_DETAILS | sed -n 's/.*"name"\:"\(.*\)","emailAddress".*/\1/p') | |
fi | |
if [[ $AUTHOR_EMAIL = $COMMITER_EMAIL ]]; then | |
COMMITER_EMAIL_HASH=$AUTHOR_EMAIL_HASH | |
COMITTER_USER=$AUTHOR_USER | |
else | |
COMMITER_EMAIL_HASH=$(echo -n $COMMITER_EMAIL | md5sum | cut -c 1-32) | |
COMITTER_DETAILS=$(curl -sX GET \ | |
--noproxy localhost \ | |
-H "Authorization: Basic <stripped>" \ | |
"http://localhost:7990/rest/api/1.0/admin/users?filter=$COMMITER_EMAIL&limit=1" \ | |
) | |
if [ 0 = $(echo $COMITTER_DETAILS | awk -F'"size":' '{print substr($2,0,1)}') ]; then | |
COMITTER_USER=$(git show --quiet --format="%cn" "$LAST_COMMIT") | |
else | |
COMITTER_USER=$(echo $COMITTER_DETAILS | sed -n 's/.*"name"\:"\(.*\)","emailAddress".*/\1/p') | |
fi | |
fi | |
PAYLOAD_GIT=$(cat <<-EOF | |
{ | |
"commit": "%H", | |
"ref": "$BRANCH_NAME", | |
"default_branch": "dev", | |
"commit_info": { | |
"url": "$STASH_BASE_URL/projects/$STASH_PROJECT_KEY/repos/$STASH_REPO_NAME/commits/%H", | |
"message": "%s", | |
"date": "%cI", | |
"author": { | |
"username": "$AUTHOR_USER", | |
"avatar_url": "http://gravatar.com/avatar/$AUTHOR_EMAIL_HASH.jpg", | |
"url": "$STASH_BASE_URL/users/$STASH_USER_NAME" | |
}, | |
"committer": { | |
"username": "$COMITTER_USER", | |
"avatar_url": "http://gravatar.com/avatar/$COMMITER_EMAIL_HASH.jpg", | |
"url": "$STASH_BASE_URL/users/$STASH_USER_NAME" | |
} | |
} | |
} | |
EOF | |
) | |
PAYLOAD_GIT=$(echo $PAYLOAD_GIT | tr -d '\n') | |
PAYLOAD=$(git show --quiet --format="$PAYLOAD_GIT" "$LAST_COMMIT") | |
for WEBHOOK_ENDPOINT in "$@" | |
do | |
curl -sX POST \ | |
-d "$PAYLOAD" \ | |
-H "Content-Type: application/json" \ | |
$WEBHOOK_ENDPOINT | |
done |
requires a git version where git-show supports %cI
updated to support multiple webhook endpoints.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just replace the
<stripped>
curl HTTP auth credentials in the script with actual credentials and should be good to go.I would like to somehow get this to handle multiple dockerfiles in one repo.