Snippets

Andžs Pilskalns Webhook hotfix

Created by Andžs Pilskalns
<?php

/**
 * Deploys commits to the file-system
 */
function deployChangeSet( $postData ) {
	global $CONFIG, $DEPLOY, $DEPLOY_BRANCH;
	global $processed;
	global $rmdirs;
	
	$o = json_decode($postData);
	if( !$o ) {
		// could not parse ?
		echo "    ! Invalid JSON file\n";
		return false;
	}
	
	// determine the destination of the deployment
	if( array_key_exists($o->repository->name, $DEPLOY) ) {
		$deployLocation = $DEPLOY[ $o->repository->name ] . (substr($DEPLOY[ $o->repository->name ], -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR);
	} else {
		// unknown repository ?
		echo "    ! Repository not configured for sync: {$o->repository->name}\n";
		return false;
	}
	
	// determine from which branch to get the data
	if( isset($DEPLOY_BRANCH) && array_key_exists($o->repository->name, $DEPLOY_BRANCH) ) {
		$deployBranch = $DEPLOY_BRANCH[ $o->repository->name ];
	} else {
		// use the default branch
		$deployBranch = $CONFIG['deployBranch'];
	}
	
	// build URL to get the updated files
	$baseUrl = 'https://bitbucket.org';             # https://bitbucket.org
	$apiUrl = '/api/1.0/repositories';              # /api/1.0/repositories
	$repoUrl = strtolower($o->repository->full_name);	# /user/repo/
	$rawUrl = 'raw/';								# raw/
	$branchUrl = $deployBranch . '/';     			# branch/
	
	// prepare to get the files
	$pending = array();
	
	// loop through commits
	foreach($o->push->changes->commits as $commit) {
		// check if the branch is known at this step
		loginfo("    > Change-set: " . trim($commit->message) . "\n");
		if(!empty($commit->branch) || !empty($commit->branches)) {
			// if commit was on the branch we're watching, deploy changes
			if( $commit->branch == $deployBranch || 
					(!empty($commit->branches) && array_search($deployBranch, $commit->branches) !== false)) {
				// if there are any pending files, merge them in
				$files = array_merge($pending, $commit->files);
				
				// get a list of files
				foreach($files as $file) {
					if( $file->type == 'modified' || $file->type == 'added' ) {
						if( empty($processed[$file->file]) ) {
							$processed[$file->file] = 1; // mark as processed
							$contents = getFileContents($baseUrl . $apiUrl . $repoUrl . $rawUrl . $branchUrl . $file->file);
							if( $contents == 'Not Found' ) {
								// try one more time, BitBucket gets weirdo sometimes
								$contents = getFileContents($baseUrl . $apiUrl . $repoUrl . $rawUrl . $branchUrl . $file->file);
							}
							
							if( $contents != 'Not Found' && $contents !== false ) {
								if( !is_dir( dirname($deployLocation . $file->file) ) ) {
									// attempt to create the directory structure first
									mkdir( dirname($deployLocation . $file->file), 0755, true );
								}
								file_put_contents( $deployLocation . $file->file, $contents );
								loginfo("      - Synchronized $file->file\n");
								
							} else {
								echo "      ! Could not get file contents for $file->file\n";
								flush();
							}
						}
						
					} else if( $file->type == 'removed' ) {
						unlink( $deployLocation . $file->file );
						$processed[$file->file] = 0; // to allow for subsequent re-creating of this file
						$rmdirs[dirname($deployLocation . $file->file)] = dirname($file->file);
						loginfo("      - Removed $file->file\n");
					}
				}
			}
			
			// clean pending files, if any
			$pending = array();
		
		} else {
			// unknown branch for now, keep these files
			$pending = array_merge($pending, $commit->files);
		}
	}
	
	return true;
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.