/
Sample Webhook Providers and Functions

Sample Webhook Providers and Functions

This page contains examples of provider match and function scripts for some common providers. 

GitHub

Provider Match Script- Hmac Secret

This provider match script for GitHub validates based on the secret configured on your GitHub webhook. This script is largely based around GitHub's Hmac encryption

GitHub Provider Match Script
// perform checks and functions to ensure an incoming message is valid and matches this provider LOG.fine("Evaluating GitHub for incoming message"); def match = false; def gitHubSecret = 'REPLACE_ME'; // validating based on GitHub secret if (HTTP_HEADERS['user-agent'] && HTTP_HEADERS['user-agent'].toLowerCase().contains('github-hookshot')) { //generate hmac string, be sure to replace with your github secret def HMAC_RESULT = HMAC.generateHmacSHA1(FLX_PRISTINE_PAYLOAD, gitHubSecret); def RECEIVED_HMAC = HTTP_HEADERS['x-hub-signature']; match = RECEIVED_HMAC && RECEIVED_HMAC.contains(HMAC_RESULT); } LOG.fine("GitHub provider is a match: ${match}"); return match;

Function Script - Manage Streams

GitHub offers the flexibility to manage branch events either separately or along with a push event. The following implementation assumes a GitHub webhook is created for branch events, managed separately from push events. This script manages FlexDeploy streams based on GitHub branch events (i.e. delete, create).

GitHub Manage FlexDeploy Streams
// get necessary information from GitHub headers, payload, and query parameters def event = HTTP_HEADERS["x-github-event"]; LOG.info("Webhook triggered by ${event} event"); def projectId = QUERY_PARAMS.projectId; def repoName = PAYLOAD.repository.name; def branch = PAYLOAD.ref; LOG.info("Running GitHub function for: ${repoName}, ${branch}"); // if projectId was passed, assume event is only associated with this project if (projectId) { //the query param received is a string, we need a long def lProjectId = new Long(projectId); if (event.equals("create")) { LOG.fine("Creating stream ${branch} for project ${projectId}"); def stream = FLEXDEPLOY.createStream(lProjectId, branch); LOG.setMessage("Successfully created stream ${branch} for project ${projectId}"); } else if (event.equals("delete")) { LOG.fine("Inactivating stream ${branch} for project ${projectId}"); FLEXDEPLOY.inactivateStream(lProjectId, branch); LOG.setMessage("Successfully inactivated stream ${branch} for project ${projectId}"); } } // get all project ids for projects affected by this SCM change else { def projects = FLEXDEPLOY.findProjectsForNewBranch(repoName); if (event.equals("create")) { LOG.fine("Creating streams with name ${branch} for projects ${projects}"); for (def project in projects)