-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffy_trigger_compare_job.php
115 lines (96 loc) · 3.01 KB
/
diffy_trigger_compare_job.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
/**
* @file
* Trigger compare jobs using Diffy API.
*
* @see https://diffy.website
*/
list($script, $site, $target_env, $source_branch, $deployed_tag,) = $argv;
// Project ID. Copy project ID from your project URL.
define('DIFFY_PROJECT_ID', getenv('DIFFY_PROJECT_ID') ? getenv('DIFFY_PROJECT_ID') : '');
// Your API key. Create key in Diffy UI: Profile -> Keys -> Add key.
define('DIFFY_API_KEY', getenv('DIFFY_API_KEY') ? getenv('DIFFY_API_KEY') : '');
// 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' . PHP_EOL;
exit(1);
}
if (empty(DIFFY_PROJECT_ID)) {
print 'Please provide DIFFY_PROJECT_ID' . PHP_EOL;
exit(1);
}
if (empty(DIFFY_BASE_ENVIRONMENT)) {
print 'Please provide DIFFY_BASE_ENVIRONMENT' . PHP_EOL;
exit(1);
}
switch ($target_env) {
case 'dev':
$environments = DIFFY_BASE_ENVIRONMENT . '-dev';
break;
case 'test':
$environments = DIFFY_BASE_ENVIRONMENT . '-stage';
break;
default:
exit('Running in Neither Dev nor Test environment. No comparision jobs will be triggered.');
}
print "Getting access token from the key." . PHP_EOL;
$ch = curl_init();
$curl_opts = [
CURLOPT_URL => 'https://app.diffy.website/api/auth/key',
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Content-Type: application/json',
],
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode([
'key' => DIFFY_API_KEY,
]),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FAILONERROR => TRUE,
];
curl_setopt_array($ch, $curl_opts);
$curl_response = curl_exec($ch);
// Check if the curl request succeeded.
if ($curl_response === FALSE) {
print_r(curl_error($ch));
print PHP_EOL;
print_r(var_export(curl_getinfo($ch), TRUE));
print PHP_EOL;
curl_close($ch);
exit(1);
}
print 'Successfully retrieved access token from the key.' . PHP_EOL;
$curl_response = json_decode($curl_response);
curl_close($ch);
$token = $curl_response->token;
print 'Starting compare job.' . PHP_EOL;
$ch = curl_init();
$curl_opts = [
CURLOPT_URL => 'https://app.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([
'environments' => $environments,
]),
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_FAILONERROR => TRUE,
];
curl_setopt_array($ch, $curl_opts);
$curl_response = curl_exec($ch);
if ($curl_response === FALSE) {
print_r(curl_error($ch));
print PHP_EOL;
print_r(var_export(curl_getinfo($ch), TRUE));
print PHP_EOL;
curl_close($ch);
exit(1);
}
$curl_response = json_decode($curl_response);
curl_close($ch);
print 'Compare job has started.' . PHP_EOL;
print 'Check out the result here: https://app.diffy.website/#/diffs/' . str_replace('diff: ', '', $curl_response) . PHP_EOL;