Page Contents
Generate an API for Local GitHub
Create a new API labeled “GitHub” for integration purposes. Make sure to enable the “Send Notification” option in the available settings. Upon completion, you’ll notice the JSON Template tab visible, where you can input the JSON template format.
Request
http://[publicserverip]:14125/api/notify?apikey=C1TA7MH12E43A72OH03L3J336ANGWXE5B6DJ&room=MarketingRoom
Note: To maintain cleaner and more readable web addresses, it’s advisable to exclude spaces in URLs to prevent the insertion of “%20” encoding. This ensures that URLs remain concise and accurately respond to requests.
JSON Template
Action: {{headers.x-github-event}} Repository name: {{repository.name}} Message: {{head_commit.message}} By: {{head_commit.committer.name}}
Description
Fields | Details |
---|---|
API Key | Includes the API key, to trigger notifications via the designated server IP |
room | Room name, you want to send the notification. |
color | Background Color |
otr | Off-The-Record messages. 1 – ON. 0 – OFF. optional. Default value is 0. |
notify | 1 – Trigger User Notification. 0 – No Notification. optional. Default value is 1. |
JSON Navigation with Dot Notation
Using dot notation to access child values within JSON headers streamlines navigation through nested structures by denoting the hierarchical connection between keys and their respective nested values.
To access the name value, you simply use “.” notation, like “head_commit.committer.name,” which will return “JohnDoe“.
Configuring Webhooks in Local GitHub
To set up a Git server post-receive hook that sends notifications to Output Messenger, follow these steps:
1. Navigate to the `hooks` directory:
First, navigate to the hooks directory in your Git repository on the server.
cd /path/to/your/repo.git/hooks
2. Create the `post-receive` hook file:
Create a new file named `post-receive` (without any extension).
touch post-receive
3. Make the post-receive hook executable:
Give execute permissions to the post-receive file.
chmod +x post-receive
4. Edit the post-receive hook file:
Open the post-receive file in your preferred text editor and add the following content. Ensure you replace the placeholder with your Output Messenger server installed ip address & placeholder with your actual API key which is created in Output Messenger Server. Also replace the respective branch names & chat room names in declare branch_params variable.
#!/bin/bash separator=";#;" # OutputMessenger server API URL with the API key CURL_URL="http://<OutputMessengerIP>:14125/api/notify?apikey=" declare -A branch_params=( ["master"]="&room=OUB%20-%20Output%20Books&otr=1" ["release"]="&room=OUB%20-%20Output%20Books&otr=1" ["dev"]="&to=sureshpandi,sathish,saravanan&otr=1" ) repo_name=$(basename "$PWD") branch_name="" send_curl() { # Get the latest commit log information commit_log=$(git log --format="%H$separator%an$separator%s" "$newrev" -1) # Extracting parts of commit log commit_hash=$(echo "$commit_log" | awk -F"$separator" '{print $1}') author_name=$(echo "$commit_log" | awk -F"$separator" '{print $2}') commit_message=$(echo "$commit_log" | awk -F"$separator" '{print $3}') # Create JSON data json_data=$(cat <<<EOF { "repository": { "name": "$repo_name : $branch_name", "branch": "$branch_name" }, "head_commit": { "message": "$commit_message", "committer": { "name": "$author_name" } } } EOF ) # Send JSON data in a POST request curl -m 10 -X POST \ -H "Content-Type: application/json" \ -H "x-github-event: push" \ -d "$json_data" "$CURL_URL$CURL_PARAM" || echo "Failed to send notification for $branch_name" } # Extract commit log information while read oldrev newrev refname; do branch_name=$(basename "$refname" "refs/heads/") # Handle branch deletion if [[ "$newrev" == "0000000000000000000000000000000000000000" ]]; then echo "Branch '$branch_name' has been deleted." continue fi # Handle new branch creation if [[ "$oldrev" == "0000000000000000000000000000000000000000" ]]; then echo "New branch '$branch_name' has been created." fi # Send notifications for specific branches if [[ -n "${branch_params[$branch_name]}" ]]; then CURL_PARAM="${branch_params[$branch_name]}" send_curl fi done
5. Save and close the file:
Save the changes to the post-receive file and close the text editor.
With these steps completed, your Git server will execute the post-receive hook script each time there is a push to the repository. The script will send notifications to Output Messenger based on the branch that received the push.
With above sample script
Master and Release branches: Notifications will be sent to the specified chat room.
Dev branch: Notifications will be sent to the specified users as an announcement.
GitHub Notification
The GitHub notification, presented in a chat window, showcases key details in a structured format:
Event: Indicates the action performed, such as a “Push” event.
Repo: Identifies the repository involved, here labeled “UpdateClientAnalyze“.
Message: Provides additional context, like the update version, in this case, “Configuration Version 10,” along with the user responsible, noted as “JohnDoe“.