diff --git a/terminus.drush.inc b/terminus.drush.inc index 419ea3c..9f4ed99 100644 --- a/terminus.drush.inc +++ b/terminus.drush.inc @@ -683,6 +683,46 @@ function terminus_drush_command() { 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, ); + $items['pantheon-site-upstream-info'] = array( + 'description' => 'Get the upstream info for a site.', + 'arguments' => array( + 'site' => 'UUID of the site.', + ), + 'examples' => array( + 'drush psite-upstream-info SITE_UUID' => 'See if there are upstream udpates available.', + ), + 'aliases' => array('psite-upstream-info'), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, + ); + + $items['pantheon-site-upstream-updates'] = array( + 'description' => 'Get available upstream update status for a site.', + 'arguments' => array( + 'site' => 'UUID of the site.', + ), + 'examples' => array( + 'drush psite-upstream-updates SITE_UUID' => 'See if there are upstream udpates available.', + ), + 'aliases' => array('psite-upstream-updates'), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, + ); + + $items['pantheon-site-upstream-updates-apply'] = array( + 'description' => 'Apply available upstream update status for a site.', + 'arguments' => array( + 'site' => 'UUID of the site.', + ), + 'options' => array( + 'overwrite' => 'Allow upstream changes to overwrite if there are conflicts.', + 'updatedb' => 'Run a "drush updatedb" after deploying code.', + ), + 'examples' => array( + 'drush psite-upstream-updates-apply SITE_UUID --overwrite --updatedb' => 'Apply available upstream updates .', + ), + 'aliases' => array('psite-upstream-updates-apply'), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, + ); + /** * Utility Functions. */ @@ -1220,6 +1260,100 @@ function drush_terminus_pantheon_site_clone($site_uuid, $env_source, $env_target } } +/** + * Upstream update info validate. + */ +function drush_terminus_pantheon_site_upstream_info_validate($site_uuid = FALSE) { + return terminus_validate_session_data() && terminus_validate_site_uuid_input($site_uuid); +} + +/** + * Upstream update info. + */ +function drush_terminus_pantheon_site_upstream_info() { + $site_uuid = drush_get_option('site_uuid'); + $result = terminus_api_site_uptream_get($site_uuid); + if (drush_get_option('json')) { + drush_print($result['json']); + } + else { + $upstream = json_decode($result['json']); + drush_print("Upstream: $upstream->url", 2); + drush_print("Branch: $upstream->branch", 2); + } +} + + +/** + * Upstream updates validate. + */ +function drush_terminus_pantheon_site_upstream_updates_validate($site_uuid = FALSE) { + return terminus_validate_session_data() && terminus_validate_site_uuid_input($site_uuid); +} + +/** + * Upstream updates available?. + */ +function drush_terminus_pantheon_site_upstream_updates() { + $site_uuid = drush_get_option('site_uuid'); + $result = terminus_api_site_uptream_updates_get($site_uuid); + if (drush_get_option('json')) { + drush_print($result['json']); + } + else { + $updates = json_decode($result['json']); + if ($updates->behind == 0) { + drush_log('No updates available. The dev env is up to date.', 'ok'); + } + else { + $rows = array(); + foreach ($updates->update_log as $commit => $info) { + $rows[] = array($info->email, $info->datetime, $info->message); + } + array_unshift($rows, array("Author", "Date", "Message")); + drush_log('Updates found', 'ok'); + drush_print_table($rows, TRUE); + } + } +} + +/** + * Apply upstream updates validate. + */ +function drush_terminus_pantheon_site_upstream_updates_apply_validate($site_uuid = FALSE) { + return terminus_validate_session_data() && terminus_validate_site_uuid_input($site_uuid); +} + +/** + * Apply upstream updates. + */ +function drush_terminus_pantheon_site_upstream_updates_apply() { + $site_uuid = drush_get_option('site_uuid'); + $updatedb = drush_get_option('updatedb') ? TRUE : FALSE; + $overwrite = drush_get_option('overwrite') ? TRUE : FALSE; + $result = terminus_api_site_uptream_updates_apply($site_uuid, $updatedb, $overwrite); + if (drush_get_option('json')) { + drush_print($result['json']); + } + else { + drush_log('Applying upstream updates...', 'ok'); + $result = json_decode($result['json']); + if ($result->conflicts == '') { + drush_log('Success!', 'ok'); + } + else { + drush_log('Conflicts detected!', 'error'); + print_r($result->conflicts); + if (!$overwrite) { + drush_print("Try re-running with the --overwrite option to prioritize upstream updates and resolve these conflicts."); + } + else { + drush_print("We were not able to auto-resolve these. You will need to clone the repository, pull the updates, and resolve the conflicts manually."); + } + } + } +} + /** * User data about self. */ @@ -4422,3 +4556,53 @@ function terminus_validate_sites($json) { function terminus_validate_hostname($hostname) { return preg_match('/(?=^.{1,254}$)(^(?:(?!\d|-)[a-z0-9\-]{1,63}(? $updatedb); + if ($overwrite) { + $data['xoption'] = 'theirs'; + } + + return terminus_request($realm, $uuid, $path, $method, $data); +} + /** * API Function to Get Site Attributes */