Salesforce - GitHub integration using Webhook
Salesforce supports webhooks for communicating with many other applications. In this post let us see how we can use webhooks to communicate between Github and Salesforce to track details of push event.
1. What is Webhook
In Salesforce, we can define what action should happen when a specific event occurs. Triggers and flows can define before and after actions. Similarly, other applications provides event actions called webhooks, which is basically an HTTP POST to external URLs, which are registered under specific event actions.
There are multiple applications that currently support webhooks. Github, JIRA, ServiceNow, Twilio, Whatsapp are some examples.
These applications can register multiple subscribers for the same event, thus passing the same message to multiple systems when some event is happening.
2. How to create a Webhook supported URL in Salesforce
In Salesforce creating a webhook supported URL involves the below steps:
1. Create Apex REST APItake a note of the url mapping - @RestResource(urlMapping='/api/Webhooks/incoming/pushDetails/*')
2. Create a public SiteIn addition to Push activity, GitHub supports webhook for the below events also:
4. Request processing in Apex
Before processing this in Apex, you can find a sample payload for Push event in Github Documentation
When a push event is happening we would like to get the below details in Salesforce processing the payload:
1. Committer name
2. Commiter Email
3. Committer Comment
4. Committed Time
5. Committed url
6. Repository name
7. Added Components
8. Removed Components
9. Modified Components
To store these details, first, let us create a custom object - GitHub_Push_Details__c with the above fields.
Now update logic inside Apex class to parse the response and retrieve required details.
As an example let us see how to retrieve committer name and email from payload.
"pusher": {
"name": "MeeraRNair",
"email": "m*********@****.com"
},
To get this we can define an inner class:
And now parse the request and retrieve details as shown below:
Similarly we can retrieve other required details also. Once this is retrieved, we can create a record of GitHub_Push_Details__c and insert it.
You can also see setting response. The complete logic is uploaded here.
Now from VS code, connect to newly created repo by:
git remote add origin https://github.com/MeeraRNair/WebhookDemo.git
And push the class to repository:
In webhook details in GitHub, you can see that webhook got published successfully.
Request:
So this approach will help us to see the push details in Salesforce itself without logging into GitHub.
5. How to make this more secure
The problem with public site is that any one with this URL will be able to post data to salesforce and that will get processed. How we can add additional security to this communication?
GitHub provides an option to add Secret key to the Webhook set up. We can refer this link to get additional details on this.
How this secret will work?
When we add a secret,Github will hash the payload using the provided secret, using HMAC hex digest and hashed value will be added to the header as shown below:
Now we can modify our Apex class to validate this hashed header compoenent. Use methods provided by Crypto class to hash the body of payload using the same secret, and compare that value with the one we received at request header.
Code:
if the authentication fails, we can set corresponsing status code and message in the response:
Also in webhook sender, we can see the error:
You can watch a short Demo here:
References:
https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push
https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks
This is great. Thank you!
ReplyDeleteDo you know if we can do other way? From Salesforce to git. Add / update the file in Git after a change in Salesforce?
I am not sure, if we can directly do it from Salesforce in a real time manner. But you can write some script file to perform something similar and schedule it. That is get metadata components created/updated in a specific timeframe and push it. I have used powershell script in the past.
ReplyDeleteIt is really very helpful
ReplyDeleteThank you Amitash
DeleteNice Post. Also Check salesforce online course salesforce online courses
ReplyDeleteThis is useful information for all of us building any software or website in Jira integration, but also check out this workshop management software free trial.
ReplyDeleteHello ,
ReplyDeletethank you for this tutorial.it is very helpful.But in my case i'd like to push data from Salesforce to an external application using webhooks (i have only the web service for this application).How can i establish this ?
Thank you
Thankyou for such an informative blog. Read about OKR in this guide .
ReplyDelete