Skip to content

Instantly share code, notes, and snippets.

@willmot
Created June 26, 2012 15:19
Show Gist options
  • Select an option

  • Save willmot/2996390 to your computer and use it in GitHub Desktop.

Select an option

Save willmot/2996390 to your computer and use it in GitHub Desktop.

Revisions

  1. willmot revised this gist Jul 4, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions github callback handler.php
    Original file line number Diff line number Diff line change
    @@ -11,8 +11,8 @@
    if ( ! isset( $_POST['payload'] ) ) {

    header( 'Status: 301' );
    header( 'Location: http://hmn.md/' );
    exit;
    header( 'Location: http://hmn.md/' );
    exit;

    }

    @@ -29,7 +29,7 @@
    $sites = array();

    $sites['Human-Made-website'] = array(
    'dir' => 'humanmade',
    'dir' => 'humanmade',
    'ref' => 'master'
    );

  2. willmot revised this gist Jul 4, 2012. 1 changed file with 25 additions and 20 deletions.
    45 changes: 25 additions & 20 deletions github callback handler.php
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,23 @@
    <?php

    // TODO Handle tagged releases
    // TODO Handle auto pulling other branches
    // TODO Check current branch is checked out before pulling
    // TODO Re-write as class
    // TODO Store sites in separate config file
    // TODO Allow config file to be in folder above root or root.
    // TODO Allow config file to be in folder above root.
    // TODO Allow updating via $_GET?
    // TODO Make sure sane git status before pulling?
    // You can't view this file directly
    if ( !isset( $_POST['payload'] ) ) {
    if ( ! isset( $_POST['payload'] ) ) {

    header( 'Status: 301' );
    header( 'Location: http://hmn.md/' );
    exit;

    }

    global $github;

    $github = json_decode( $_POST['payload'] );

    /**
    @@ -32,18 +33,13 @@
    'ref' => 'master'
    );

    $sites['Yell-Marketing-Site'] = array(
    'dir' => 'yell-marketing.hmn.md',
    'ref' => 'master'
    );

    define( 'ABSPATH', dirname( dirname( __FILE__ ) ) );

    // Is the payload for one of the configured sites
    // Check the payload is for one of the configured sites
    if ( isset( $sites[$github->repository->name] ) && $site = $sites[$github->repository->name] ) {

    // If the directory doesn't exist then bail
    if ( !file_exists( ABSPATH . '/' . $site['dir'] ) )
    if ( ! file_exists( ABSPATH . '/' . $site['dir'] ) )
    return;

    $response = '';
    @@ -59,25 +55,34 @@
    notify( $response, $site );
    }

    function git_pull( $dir ) {
    return shell_exec( 'cd ' . ABSPATH . '/' . $dir . ' ; git pull ; git submodule update --init --recursive' );
    }

    function git_checkout( $ref ) {

    /**
    * Perform a git pull & git submodule update command.
    *
    * @access public
    * @param string $dir
    * @return void
    */
    function git_pull( $dir ) {
    return shell_exec( 'cd ' . escshellarg( ABSPATH . '/' . $dir ) . ' ; git pull ; git submodule update --init --recursive' );
    }

    function get_is_branch_checked_out( $branch ) {

    }

    /**
    * Send an email notification with details of the payload that was just auto pulled
    *
    * @access public
    * @param string $response
    * @param array $site
    * @return void
    */
    function notify( $response, $site ) {

    global $github;

    mail(

    'tom@humanmade.co.uk, joe@humanmade.co.uk',
    'example@email.com',

    'Git Callback from ' . $site['dir'],

  3. willmot renamed this gist Jun 26, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. willmot created this gist Jun 26, 2012.
    108 changes: 108 additions & 0 deletions github callback handler.
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    <?php

    // TODO Handle tagged releases
    // TODO Handle auto pulling other branches
    // TODO Check current branch is checked out before pulling
    // TODO Re-write as class
    // TODO Store sites in separate config file
    // TODO Allow config file to be in folder above root or root.
    // TODO Allow updating via $_GET?
    // TODO Make sure sane git status before pulling?

    // You can't view this file directly
    if ( !isset( $_POST['payload'] ) ) {
    header( 'Status: 301' );
    header( 'Location: http://hmn.md/' );
    exit;
    }

    global $github;
    $github = json_decode( $_POST['payload'] );

    /**
    * Array of sites and their github details
    *
    * Format is array( [github repo name] => array( [site directory], [pull from branch_name or tag] );
    *
    */
    $sites = array();

    $sites['Human-Made-website'] = array(
    'dir' => 'humanmade',
    'ref' => 'master'
    );

    $sites['Yell-Marketing-Site'] = array(
    'dir' => 'yell-marketing.hmn.md',
    'ref' => 'master'
    );

    define( 'ABSPATH', dirname( dirname( __FILE__ ) ) );

    // Is the payload for one of the configured sites
    if ( isset( $sites[$github->repository->name] ) && $site = $sites[$github->repository->name] ) {

    // If the directory doesn't exist then bail
    if ( !file_exists( ABSPATH . '/' . $site['dir'] ) )
    return;

    $response = '';

    // If we're set to pull from a branch and that branch was part of the payload
    if ( $site['ref'] != 'tag' && $github->ref == 'refs/heads/' . $site['ref'] )

    // Then Git Pull
    $response = git_pull( $site['dir'] );

    // Send a notification
    if ( $response )
    notify( $response, $site );
    }

    function git_pull( $dir ) {
    return shell_exec( 'cd ' . ABSPATH . '/' . $dir . ' ; git pull ; git submodule update --init --recursive' );
    }

    function git_checkout( $ref ) {

    }

    function get_is_branch_checked_out( $branch ) {

    }

    function notify( $response, $site ) {

    global $github;

    mail(

    'tom@humanmade.co.uk, joe@humanmade.co.uk',

    'Git Callback from ' . $site['dir'],

    '

    <p><a href="mailto:' . reset( $github->commits )->author->email . '">' . reset( $github->commits )->author->name . '</a> pushed to <a href="http://' . $site['dir'] . '">' . $site['dir'] . '</a></p>

    <p>Commit: <a href="' . reset( $github->commits )->url . '">' . reset( $github->commits )->id . '</a></p>

    <p>Description:</p>

    <p><em>' . reset( $github->commits )->message . '</em></p>

    <p>Response:</p>

    <p><em>' . $response . '</em></p>

    <p>Full Details:</p>

    <p><pre>' . print_r( $github, true ) . '</pre></p>

    ',

    'Content-type: text/html; charset=iso-8859-1'

    );

    }