1+ #! /bin/bash
2+
3+ echo " Git Hook: Post Commit"
4+
5+ # Get commit information
6+ COMMIT_HASH=$( git rev-parse HEAD)
7+ COMMIT_MESSAGE=$( git log -1 --pretty=%B)
8+ COMMIT_AUTHOR=$( git log -1 --pretty=" %an <%ae>" )
9+ BRANCH_NAME=$( git branch --show-current)
10+ REPO_NAME=$( basename " $( git rev-parse --show-toplevel) " )
11+ MODIFIED_FILES=$( git show --pretty=format: --name-only HEAD | jq -R . | jq -s .)
12+
13+ # Get total commit count on branch
14+ DEFAULT_BRANCH=$( git branch --sort=committerdate --format=' %(committerdate:short) %(refname:short)' | head -n 1 | cut -d' ' -f2)
15+ if [ " $BRANCH_NAME " != " $DEFAULT_BRANCH " ]; then
16+ BRANCH_COMMIT_COUNT=$( git rev-list --count ${DEFAULT_BRANCH} ..${BRANCH_NAME} )
17+ BRANCH_COMMIT_MESSAGES=$( git log ${DEFAULT_BRANCH} ..${BRANCH_NAME} --pretty=format:' %h%x1f%s' | jq -R -s -c ' split("\n") | map(select(length > 0)) | map(split("\u001f") | {id: .[0], message: .[1]})' )
18+ # echo "Total commits on branch '${BRANCH_NAME}' since '${DEFAULT_BRANCH}': ${BRANCH_COMMIT_COUNT}"
19+ else
20+ BRANCH_COMMIT_COUNT=$( git rev-list --count HEAD)
21+ BRANCH_COMMIT_MESSAGES=$( git log --pretty=format:' %h%x1f%s' | jq -R -s -c ' split("\n") | map(select(length > 0)) | map(split("\u001f") | {id: .[0], message: .[1]})' )
22+ # echo "Total commits on branch '${DEFAULT_BRANCH}': ${BRANCH_COMMIT_COUNT}"
23+ fi
24+
25+ # Build JSON payload
26+ PAYLOAD=$( jq -n \
27+ --arg event_type " post-commit" \
28+ --arg commit_hash " $COMMIT_HASH " \
29+ --arg commit_message " $COMMIT_MESSAGE " \
30+ --arg commit_author " $COMMIT_AUTHOR " \
31+ --arg branch_name " $BRANCH_NAME " \
32+ --argjson branch_commit_count " $BRANCH_COMMIT_COUNT " \
33+ --argjson branch_commit_messages " $BRANCH_COMMIT_MESSAGES " \
34+ --arg repository_name " $REPO_NAME " \
35+ --argjson modified_files " $MODIFIED_FILES " \
36+ --arg timestamp " $( date -u +" %Y-%m-%dT%H:%M:%SZ" ) " \
37+ ' {
38+ "event_type": $event_type,
39+ "client_payload": {
40+ "commit_hash": $commit_hash,
41+ "commit_message": $commit_message,
42+ "commit_author": $commit_author,
43+ "branch_name": $branch_name,
44+ "branch_commit_count": $branch_commit_count,
45+ "branch_commit_messages": $branch_commit_messages,
46+ "repository_name": $repository_name,
47+ "modified_files": $modified_files,
48+ "timestamp": $timestamp
49+ }
50+ }' )
51+
52+ # Send repository dispatch event
53+ curl -X POST \
54+ -H " Accept: application/vnd.github.v3+json" \
55+ -H " Authorization: token ${GITHUB_TOKEN} " \
56+ " https://api.github.com/repos/${GITHUB_REPOSITORY} /dispatches" \
57+ -d " $PAYLOAD " 2> /dev/null || echo " Failed to send repository dispatch event"
0 commit comments