Skip to content

Instantly share code, notes, and snippets.

@mcmullengreg
Last active December 5, 2022 21:02
Show Gist options
  • Save mcmullengreg/0b88d49850633a465196f38043e792a7 to your computer and use it in GitHub Desktop.
Save mcmullengreg/0b88d49850633a465196f38043e792a7 to your computer and use it in GitHub Desktop.
Webhoook integration for ColdFusion. Downloads the Zip after a push event and moves the contents to a folder determined based on the repository name. Used as a means of keeping testing environments updated whenever new commits are pushed to the master.
<!--- Webhook secrets -- yours, not mine. --->
<cfset secret = "">
<cftry>
<cfset _payload = "{}">
<cfset _payload = getHttpRequestData()>
<cfset jsonPayload = form.payload>
<cfset hash = RemoveChars(_payload.headers['X-Hub-Signature'], 1, 5)>
<cfset payload_hash = hmac(_payload.content, secret, 'HMACSHA1')>
<cfif payload_hash NEQ hash>
Incorrect hash
<cfabort>
</cfif>
<cfif !isJson(jsonPayload)>
Invalid JSON
<cfabort>
</cfif>
<cfset _requestpayload = deserializeJSON(jsonPayload)>
<cfset archiveURL = ReplaceNoCase(_requestpayload.repository.archive_url, "{archive_format}{/ref}", "zipball/master")>
<cfset repo = _requestpayload.repository.name>
<cfset repoReplaceFolder = Replace(_requestpayload.repository.full_name, "/", "-") & "-" & Left(_requestpayload.after, 7)>
<!--- Ensure only the repos we want to use this hook --->
<cfif repo EQ "REPO-NAME">
<cfset folder = "\\Path\To\Remote">
<cfset newFolderName = "NewName">
<cfelse>
Repo does not have a valid folder.
<cfabort>
</cfif>
<cfcatch>
GitHub Webhook error.
<cfmail to="You@you.com" from="GitHub Webhook Error <donotreply@Doman.whatev>" subject="Can't get Archive URL" type="html">
<p>GitHub Webhook error. Could not retrieve the repository.</p>
<cfdump var="#cfcatch#">
<cfdump var="#variables#">
</cfmail>
</cfcatch>
</cftry>
<cftry>
<cfif !DirectoryExists("#folder#")>
No directory found, please ensure <cfoutput>#folder#</cfoutput> exists.
<cfabort>
</cfif>
<!--- Use the API to get the zip archive --->
<cfhttp url="#archiveURL#" file="master.zip" path="#getdirectoryfrompath(expandpath(CGI.script_name))#">
<!--- Unzip it and move it to the proper location --->
<cfzip action="unzip" file="#ExpandPath('./master.zip')#" destination="#folder#" overwrite="true" storepath="yes">
<cfif !DirectoryExists("#folder##repoReplaceFolder#")>
Cannot find folder to rename.
<cfabort>
</cfif>
<cfset DirectoryRename(folder & repoReplaceFolder, newFolderName)>
<cfcatch>
Github API error. May not have been able to create the folder or move the zip.
<cfmail to="You@you.com" from="GitHub Webhook Error <donotreply@Doman.whatev>" subject="GitHub Webhook Error" type="html">
<p>Github API error. May not have been able to create the folder or move the zip.</p>
<cfdump var="#cfcatch#">
<cfdump var="#variables#">
</cfmail>
</cfcatch>
</cftry>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment