/** * Processes the "auto-pull" functionality. * @access public */ public function run_autopull() { revisr()->git = new Revisr_Git(); // If auto-pull isn't enabled, we definitely don't want to do this. if (revisr()->git->get_config('revisr', 'auto-pull') !== 'true') { wp_die(__('Cheatin’ uh?', 'revisr')); } // Verify the provided token matches the token stored locally. $remote = new Revisr_Remote(); $remote->check_token(); // If we're still running at this point, we've successfully authenticated. revisr()->git->reset(); revisr()->git->fetch(); // Grab the commits that need to be pulled. $commits_since = revisr()->git->run('log', array(revisr()->git->branch . '..' . revisr()->git->remote . '/' . revisr()->git->branch, '--pretty=oneline')); // Maybe backup the database. if (revisr()->git->get_config('revisr', 'import-pulls') === 'true') { revisr()->db = new Revisr_DB(); revisr()->db->backup(); $undo_hash = revisr()->git->current_commit(); revisr()->git->set_config('revisr', 'last-db-backup', $undo_hash); } // Pull the changes or return an error on failure. revisr()->git->pull($commits_since); }
/** * Displays/updates the "Auto Pull" settings field. * @access public */ public function auto_pull_callback() { if (isset($_GET['settings-updated'])) { if (isset(revisr()->options['auto_pull'])) { revisr()->git->set_config('revisr', 'auto-pull', 'true'); } else { revisr()->git->run('config', array('--unset', 'revisr.auto-pull')); } } printf('<input type="checkbox" id="auto_pull" name="revisr_remote_settings[auto_pull]" %s /> <label for="auto_pull">%s</label>', checked(revisr()->git->get_config('revisr', 'auto-pull'), 'true', false), __('Check to generate the Revisr Webhook and allow Revisr to automatically pull commits from a remote repository.', 'revisr')); $remote = new Revisr_Remote(); $token = $remote->get_token(); if ($token) { $post_hook = get_admin_url() . 'admin-post.php?action=revisr_update&token=' . $remote->get_token(); ?> <div id="post-hook"> <p class="description revisr-description"><?php _e('Revisr Webhook:', 'revisr'); ?> </p> <input id="post-hook-input" type="text" value="<?php echo $post_hook; ?> " disabled /> <p class="description revisr-description"><?php _e('You can add the above webhook to Bitbucket, GitHub, or another instance of Revisr to automatically update this repository.', 'revisr'); ?> </p> </div> <?php } else { echo '<p id="post-hook" class="description">' . __('There was an error generating the webhook. Please make sure that Revisr has write access to the ".git/config" and try again.', 'revisr') . '</p>'; } }
/** * Returns if a push was successful. * @access public */ public function success_push($output = '', $args = '') { $msg = sprintf(_n('Successfully pushed %s commit to %s/%s.', 'Successfully pushed %s commits to %s/%s.', $args, 'revisr'), $args, $this->remote, $this->branch); Revisr_Admin::alert($msg); Revisr_Admin::log($msg, 'push'); if ($this->config_revisr_url('webhook') !== false) { $remote = new Revisr_Remote(); $remote->send_request(); } }
/** * Processes the request to pull changes into the current branch. * @access public */ public function process_pull() { // Determine whether this is a request from the dashboard or a POST request. $from_dash = check_ajax_referer('dashboard_nonce', 'security', false); if ($from_dash == false) { if ($this->git->config_revisr_option('auto-pull') !== 'true') { wp_die(__('Cheatin’ uh?', 'revisr')); } $remote = new Revisr_Remote(); $remote->check_token(); } $this->git->reset(); $this->git->fetch(); $commits_since = $this->git->run("log {$this->git->branch}..{$this->git->remote}/{$this->git->branch} --pretty=oneline"); if (is_array($commits_since)) { // Iterate through the commits to pull and add them to the database. foreach ($commits_since as $commit) { $commit_hash = substr($commit, 0, 7); $commit_msg = substr($commit, 40); $show_files = $this->git->run('show --pretty="format:" --name-status ' . $commit_hash); if (is_array($show_files)) { $files_changed = array_filter($show_files); $post = array('post_title' => $commit_msg, 'post_content' => '', 'post_type' => 'revisr_commits', 'post_status' => 'publish'); $post_id = wp_insert_post($post); add_post_meta($post_id, 'commit_hash', $commit_hash); add_post_meta($post_id, 'branch', $this->git->branch); add_post_meta($post_id, 'files_changed', count($files_changed)); add_post_meta($post_id, 'committed_files', $files_changed); $view_link = get_admin_url() . "post.php?post={$post_id}&action=edit"; $msg = sprintf(__('Pulled <a href="%s">#%s</a> from %s/%s.', 'revisr'), $view_link, $commit_hash, $this->git->remote, $this->git->branch); Revisr_Admin::log($msg, 'pull'); } } } if ($this->git->config_revisr_option('import-pulls') === 'true') { $this->db->backup(); $undo_hash = $this->git->current_commit(); $this->git->run("config --add revisr.last-db-backup {$undo_hash}"); } // Pull the changes or return an error on failure. $this->git->pull(); }
/** * Returns if a push was successful. * @access public */ public function success_push($output = array(), $args = '') { $msg = sprintf(_n('Successfully pushed %s commit to %s/%s.', 'Successfully pushed %s commits to %s/%s.', $args, 'revisr'), $args, revisr()->git->remote, revisr()->git->branch); Revisr_Admin::alert($msg); Revisr_Admin::log($msg, 'push'); // Fires after a successful push. do_action('revisr_post_push', $output); if (revisr()->git->get_config('revisr', 'webhook-url') !== false) { $remote = new Revisr_Remote(); $remote->send_request(); } }