Skip to content

Commit

Permalink
add feature to force deploy by commit hash
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusgimenez committed Feb 10, 2017
1 parent 2d08b80 commit 3f29f5a
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 16 deletions.
21 changes: 18 additions & 3 deletions admin/assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
'use strict';

$(function () {

// Place your administration-specific JavaScript here

// Deploy by Commit Hash
$( '#brasa-deploy-by-commit' ).on( 'click', function( e ) {
e.preventDefault();
var $input_hash = $( 'input[name="brasa_deploy_force_hash"]' );
var $button = $( this );
var data = {
action: 'brasa_deploy_by_commit',
nonce: $input_hash.attr( 'data-nonce' ),
hash: $input_hash.val(),
};
var default_text = $button.html();
$button.html( $button.attr( 'data-load' ) );
$.post( ajaxurl, data, function(response) {
$button.html( default_text );
$( '#brasa-deploy-status' ).append( response );
});
console.log( ajaxurl );
});
});

}(jQuery));
1 change: 0 additions & 1 deletion admin/assets/sass/_mixins.scss

This file was deleted.

5 changes: 0 additions & 5 deletions admin/assets/sass/admin.scss

This file was deleted.

107 changes: 107 additions & 0 deletions inc/class-deploy-by-commit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Use the PHP for https://github.com/tommcfarlin/WordPress-Plugin-Boilerplate
*
* @package Plugin_Name
* @author Your Name <[email protected]>
* @license GPL-2.0+
* @link http://example.com
* @copyright 2014 Your Name or Company Name
*
* @wordpress-plugin
* Plugin Name: Brasa GitHub Theme Deploy
* Plugin URI: @TODO
* Description: @TODO
* Version: 1.0.0
* Author: @TODO
* Author URI: @TODO
* Text Domain: plugin-name-locale
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/<owner>/<repo>
*/
// Include plugin options
class Brasa_Force_Deploy_By_Commit extends Brasa_Update_Deploy_File {
/**
* Construct class
* @return boolean
*/
public function __construct () {
add_action( 'wp_ajax_brasa_deploy_by_commit', array( $this, 'deploy' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ), 10, 1 );
}
/**
* Add scripts
*/
public function add_scripts() {
$current_screen = get_current_screen();
if ( 'tools_page_brasa_theme_deploy' != $current_screen->base ) {
return;
}
$url = plugins_url( 'admin/assets/js/', dirname(__FILE__) );
wp_enqueue_script( 'brasa-theme-deploy-adminjs', $url . 'admin.js', array(), null, true );
}
/**
* Download & extract theme tar.gz file from GitHub
* @return type
*/
protected function extract_file() {
wp_verify_nonce( $_REQUEST[ 'nonce' ], 'deploy_by_hash' );
$this->options = get_option( 'brasa_theme_deploy_settings' );
if ( ! isset( $this->options[ 'brasa_deploy_repository' ] ) ) {
return false;
}
$hash = esc_textarea( $_REQUEST[ 'hash' ] );
if ( ! $this->commit_exists( $hash ) ) {
return false;
}
$repository_name = explode( '/', $this->options[ 'brasa_deploy_repository' ] );
$repository_name = $repository_name[ 1 ];
$folder = get_template_directory();
$folder = explode( '/', $folder );
unset( $folder[ count( $folder ) - 1 ] );
$folder = implode( '/', $folder );
$download_url = sprintf( 'http://github.com/%s/archive/%s.tar.gz', $this->options[ 'brasa_deploy_repository'], $hash );
exec( "cd $folder && wget $download_url" );

$file_format = sprintf( '%s.tar.gz', $hash );
$file_check = $folder . '/' . $file_format;
if ( ! file_exists( $file_check ) ) {
$this->options[ 'error' ] = $download_url;
return false;
}
$template_folder = get_template_directory();
exec( "rm -rf $template_folder" );
exec( "cd $folder && tar -zxvf $file_format" );
$folder_format = sprintf( '%s-%s', $repository_name, $hash );
rename( $folder . '/' . $folder_format, $template_folder );
unlink( $folder . '/' . $file_format );
return true;
}
/**
* Deploy last commit from GitHub repo
* @return boolean
*/
public function deploy() {
if ( $this->extract_file() ) {
echo '<div class="notice notice-success is-dismissible" style="margin:0;">';
echo '<p>';
_e( 'Done!', 'brasa-theme-deploy' );
echo '</p>';
echo '</div>';
} else {
echo '<div class="notice notice-error is-dismissible" style="margin:0;">';
echo '<p>';
if ( isset( $this->options[ 'error' ] ) ) {
echo $this->options[ 'error' ];
} else {
_e( 'An error has occurred!', 'brasa-theme-deploy' );
}
echo '</p>';
echo '</div>';
}
echo '<br>';
wp_die();
}
}
52 changes: 47 additions & 5 deletions inc/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function brasa_theme_deploy_settings_init( ) {

add_settings_section(
'brasa_theme_deploy_pluginPage_section',
__( 'Brasa Theme Deploy', 'brasa-theme-deploy' ),
__( 'Brasa Theme Deploy - Push to Deploy', 'brasa-theme-deploy' ),
'brasa_theme_deploy_settings_section_callback',
'pluginPage'
);
Expand All @@ -29,6 +29,14 @@ function brasa_theme_deploy_settings_init( ) {
'brasa_theme_deploy_pluginPage_section'
);

add_settings_field(
'brasa_deploy_repository',
__( 'GitHub Repository: user/repo (Example: wpbrasil/odin)', 'brasa-theme-deploy' ),
'brasa_theme_deploy_text_field_3_render',
'pluginPage',
'brasa_theme_deploy_pluginPage_section'
);

add_settings_field(
'brasa_deploy_branch',
__( 'Branch', 'brasa-theme-deploy' ),
Expand All @@ -37,6 +45,36 @@ function brasa_theme_deploy_settings_init( ) {
'brasa_theme_deploy_pluginPage_section'
);

add_settings_section(
'brasa_theme_deploy_pluginPage_section_force_commit',
__( 'Brasa Theme Deploy - Force Update by commmit hash', 'brasa-theme-deploy' ),
'brasa_theme_deploy_settings_section_callback',
'pluginPage'
);

add_settings_field(
'brasa_deploy_force_hash',
__( 'Commit Hash', 'brasa-theme-deploy' ),
'brasa_deploy_force_hash',
'pluginPage',
'brasa_theme_deploy_pluginPage_section_force_commit'
);


}


function brasa_deploy_force_hash( ) {

?>
<input type='text' name='brasa_deploy_force_hash' data-nonce="<?php echo wp_create_nonce( 'deploy_by_hash');?>">
<div class="clear"></div><!-- .clear -->
<div id="brasa-deploy-status"></div><!-- #brasa-deploy-status -->
<br>
<button class="button button-primary" id="brasa-deploy-by-commit" data-load="<?php esc_html_e( 'Loading..', 'odin' );?>">
<?php _e( 'Deploy!', 'odin' );?>
</button>
<?php

}

Expand All @@ -56,15 +94,20 @@ function brasa_theme_deploy_text_field_1_render( ) {
$options = get_option( 'brasa_theme_deploy_settings' );
?>
<input type='text' name='brasa_theme_deploy_settings[brasa_deploy_branch]' value='<?php echo $options['brasa_deploy_branch']; ?>'>
<div class="clear"></div><!-- .clear -->
<?php
submit_button();
}
function brasa_theme_deploy_text_field_3_render( ) {

$options = get_option( 'brasa_theme_deploy_settings' );
?>
<input type='text' name='brasa_theme_deploy_settings[brasa_deploy_repository]' value='<?php echo $options['brasa_deploy_repository']; ?>'>
<?php
}


function brasa_theme_deploy_settings_section_callback( ) {

_e( 'Deploy any WordPress theme hosted on GitHub', 'brasa-theme-deploy' );

}


Expand All @@ -76,7 +119,6 @@ function brasa_theme_deploy_options_page( ) {
<?php
settings_fields( 'pluginPage' );
do_settings_sections( 'pluginPage' );
submit_button();
?>

</form>
Expand Down
34 changes: 32 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
// Include plugin options
require 'inc/options.php';
// Include deploy by commit hash class
require 'inc/class-deploy-by-commit.php';

class Brasa_Update_Deploy_File {
/**
* Instance of this class.
Expand All @@ -42,7 +45,7 @@ class Brasa_Update_Deploy_File {
* Download & extract theme tar.gz file from GitHub
* @return type
*/
private function extract_file() {
protected function extract_file() {
$folder = get_template_directory();
$folder = explode( '/', $folder );
unset( $folder[ count( $folder ) - 1 ] );
Expand All @@ -63,6 +66,26 @@ private function extract_file() {
unlink( $folder . '/' . $file_format );
return true;
}
/**
* Check if a commit exist by hash
* @param string $hash
* @return boolean
*/
public function commit_exists( $hash ) {
if ( ! isset( $this->options[ 'brasa_deploy_repository' ] ) ) {
return false;
}
$response = wp_remote_get( sprintf( 'https://api.github.com/repos/%s/git/commits/%s', $this->options[ 'brasa_deploy_repository' ], $hash ) );
$response_code = wp_remote_retrieve_response_code( $response );
if ( 200 === intval( $response_code ) ) {
return true;
} elseif ( 403 == intval( $response_code ) ) {
$this->options[ 'error' ] = __( 'Error: API rate limit exceeded for your server IP', 'odin' );
} else {
$this->options[ 'error' ] = __( 'Error: This commit does not exist', 'odin' );
return false;
}
}
/**
* Construct class
* @return boolean
Expand Down Expand Up @@ -127,4 +150,11 @@ public static function get_instance() {
return self::$instance;
}
}
add_action( 'plugins_loaded', array( 'Brasa_Update_Deploy_File', 'get_instance' ), 0 );
/**
* Instance plugin classes
*/
function brasa_theme_deploy_load_classes() {
new Brasa_Update_Deploy_File();
new Brasa_Force_Deploy_By_Commit();
}
add_action( 'plugins_loaded', 'brasa_theme_deploy_load_classes' );

0 comments on commit 3f29f5a

Please sign in to comment.