-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated script to support env variables and adhere to coding standards.
- Loading branch information
1 parent
f87570a
commit f32fd46
Showing
3 changed files
with
93 additions
and
40 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |