diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 485dee6..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.idea diff --git a/README.md b/README.md index 23f8cd5..2e658be 100644 --- a/README.md +++ b/README.md @@ -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/ @@ -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. diff --git a/hooks/diffy/diffy_trigger_compare_job.php b/hooks/diffy/diffy_trigger_compare_job.php index a6297c1..814b9ac 100644 --- a/hooks/diffy/diffy_trigger_compare_job.php +++ b/hooks/diffy/diffy_trigger_compare_job.php @@ -1,65 +1,115 @@ '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);