Skip to content

Commit

Permalink
Updated script to support env variables and adhere to coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Jul 29, 2019
1 parent f87570a commit f32fd46
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 40 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Diffy Acquia cloud hooks integration

Automates triggering "compare" job for your Acquia project whenever you do deployment.
Automates triggering "compare" job for your Acquia project whenever you do a
deployment.

If you do deployment to "dev" or "test" environments this script triggers job to compare dev/test with prod.
If you do a deployment to "dev" or "test" environments this script triggers a
job to compare "dev"/"test" with "prod".

More on Acquia cloud hooks: https://docs.acquia.com/acquia-cloud/develop/api/cloud-hooks/

Expand All @@ -12,6 +14,8 @@ Video tutorial: https://youtu.be/wOuB8tRNNYw

## Installation notes

You would need to have a project created in Diffy (need its ID). Also you need to provide API key so script can authenticate.
You would need to have a project created in Diffy (need its ID). Also you need
to provide API key so the script can authenticate.

Please provide these details https://github.com/DiffyWebsite/diffy-acquia/blob/master/hooks/diffy/diffy_trigger_compare_job.php.
Provide these details either as `DIFFY_API_KEY` and `DIFFY_PROJECT_ID`
environment variables or update [diffy_trigger_compare_job.php](hooks/diffy/diffy_trigger_compare_job.php) file.
120 changes: 85 additions & 35 deletions hooks/diffy/diffy_trigger_compare_job.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,115 @@
<?php

/**
* Script to trigger compare jobs on Diffy API.
* @see https://diffy.website for more details.
**/
list($script, $site, $target_env, $source_branch, $deployed_tag, ) = $argv;
* @file
* Trigger compare jobs using Diffy API.
*
* @see https://diffy.website
*/

list($script, $site, $target_env, $source_branch, $deployed_tag,) = $argv;

// Provide Diffy API key and project id as environment variables or
// override here.
define('DIFFY_API_KEY', getenv('DIFFY_API_KEY') ? getenv('DIFFY_API_KEY') : 'DIFFY_API_KEY_PLACEHOLDER');
define('DIFFY_PROJECT_ID', getenv('DIFFY_PROJECT_ID') ? getenv('DIFFY_PROJECT_ID') : 'DIFFY_PROJECT_ID_PLACEHOLDER');

// Settings. Please provide your own here.
$api_key = 'AAA';
$project_id = 111;
// Default base environment to compare to. Defaults to 'prod'.
define('DIFFY_BASE_ENVIRONMENT', getenv('DIFFY_BASE_ENVIRONMENT') ? getenv('DIFFY_BASE_ENVIRONMENT') : 'prod');

if (empty(DIFFY_API_KEY)) {
print 'Please provide DIFFY_API_KEY';
exit(1);
}

if (empty(DIFFY_PROJECT_ID)) {
print 'Please provide DIFFY_PROJECT_ID';
exit(1);
}

if (empty(DIFFY_BASE_ENVIRONMENT)) {
print 'Please provide DIFFY_BASE_ENVIRONMENT';
exit(1);
}

// Choose what environments to compare.
switch ($target_env) {
case 'dev':
$operation = 'prod-dev';
$environments = DIFFY_BASE_ENVIRONMENT . '-dev';
break;

case 'test':
$operation = 'prod-stage';
$environments = DIFFY_BASE_ENVIRONMENT . '-stage';
break;

default:
echo 'Neither Dev nor Test environment. Do not trigger any job.';
exit;
exit('Neither Dev nor Test environment. Do not trigger any jobs.');
}

// Get access token from key.
print "Getting access token from the key.";
$ch = curl_init();
$curl_options = array(
curl_setopt_array($ch, [
CURLOPT_URL => 'https://diffy.website/api/auth/key',
CURLOPT_HTTPHEADER => array(
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Content-Type: application/json'
),
'Content-Type: application/json',
],
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode(array(
'key' => $api_key
)),
CURLOPT_POSTFIELDS => json_encode([
'key' => DIFFY_API_KEY,
]),
CURLOPT_RETURNTRANSFER => TRUE,
);
curl_setopt_array($ch, $curl_options);
$curl_response = json_decode(curl_exec($ch));
]);

$curl_response = curl_exec($ch);
// Check if the curl request succeeded.
if ($curl_response === FALSE) {
$info = var_export(curl_getinfo($ch), TRUE);
$error = curl_error($ch);
curl_close($ch);

print_r($info);
print_r($error);

exit(1);
}

print "Successfully retrieved access token from the key.";
$curl_response = json_decode($curl_response);
curl_close($ch);

$token = $curl_response->token;

// Run Compare job.
print "Starting compare job.";
$ch = curl_init();
$curl_options = array(
CURLOPT_URL => 'https://diffy.website/api/projects/' . $project_id . '/compare',
CURLOPT_HTTPHEADER => array(
curl_setopt_array($ch, [
CURLOPT_URL => 'https://diffy.website/api/projects/' . DIFFY_PROJECT_ID . '/compare',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer ' . $token,
),
],
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode(array(
'environments' => $operation,
)),
CURLOPT_POSTFIELDS => json_encode([
'environments' => $environments,
]),
CURLOPT_RETURNTRANSFER => TRUE,
);
curl_setopt_array($ch, $curl_options);
$curl_response = json_decode(curl_exec($ch));
]);
$curl_response = curl_exec($ch);

if ($curl_response === FALSE) {
$info = var_export(curl_getinfo($ch), TRUE);
$error = curl_error($ch);
curl_close($ch);

print_r($info);
print_r($error);

exit(1);
}

$curl_response = json_decode($curl_response);
curl_close($ch);

echo "Compare job has started.\n";
echo 'Check out the result here: https://diffy.website/ui#/diffs/' . str_replace('diff: ', '', $curl_response) . "\n";
print "Compare job has started.";
print 'Check out the result here: https://diffy.website/ui#/diffs/' . str_replace('diff: ', '', $curl_response);

0 comments on commit f32fd46

Please sign in to comment.