-
Notifications
You must be signed in to change notification settings - Fork 0
/
tugboat.php
99 lines (85 loc) · 3.47 KB
/
tugboat.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
<?php
declare(strict_types=1);
use GuzzleHttp\Client;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Yaml\Yaml;
require __DIR__ . '/vendor/autoload.php';
class Tugboat
{
private const API_URL = 'https://api.tugboat.qa/v3/';
private $client;
public function __construct()
{
$this->client = new Client([
'base_uri' => self::API_URL,
'headers' => [
'Accept' => 'application/json',
],
]);
}
public function requestWithApiKey($method, $uri, array $payload = [])
{
$request_options = [
'headers' => [
'Authorization' => sprintf('Bearer %s', $_ENV['TUGBOAT_TOKEN']),
]
];
if ($payload !== []) {
if ($method === 'GET') {
throw new \InvalidArgumentException('Cannot set a body with a GET request');
}
$request_options['json'] = $payload;
}
return $this->client->request($method, $uri, $request_options);
}
public function requestWithUrlToken($method, $uri, $token)
{
return $this->client->request($method, $uri, [
'headers' => [
'Authorization' => sprintf('Authorization: Bearer %s', $token),
]
]);
}
}
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
(new SingleCommandApplication('Tugboat'))
->addArgument('branch', InputArgument::REQUIRED, 'Must be a branch in this repository.')
->addArgument('config', InputArgument::OPTIONAL, 'Path to configuration')
->setCode(static function (InputInterface $input, OutputInterface $output) {
$branch = $input->getArgument('branch');
// if no config name passed, assume it is the same as the branch.
$config = $input->getArgument('config') ?: $branch;
$config_path = __DIR__ . '/configs/' . $config . '.yml';
if (!file_exists($config_path)) {
$output->writeln("<error>Could not load `configs/$config.yml`.</error>");
}
$tugboat_config = Yaml::parse(file_get_contents($config_path));
$tugboat_client = new Tugboat;
$tugboat_response = $tugboat_client->requestWithApiKey('POST', 'previews', [
'ref' => $branch,
'config' => $tugboat_config,
'name' => $branch,
'repo' => $_ENV['TUGBOAT_REPOSITORY'],
]);
$response = \json_decode((string) $tugboat_response->getBody());
$output->writeln("<info>Success! Follow the build here: https://dashboard.tugboat.qa/{$response->preview}</info>");
$output->writeln("<comment>Job ID: {$response->job}");
for (;;) {
$output->writeln("<comment>Checking preview build status...</comment>");
$job_status_response = $tugboat_client->requestWithApiKey('GET', "jobs/{$response->job}");
$job_status = \json_decode((string) $job_status_response->getBody());
if ($job_status->type === 'preview') {
$output->writeln("<info>Built! Visit {$job_status->url}");
break;
}
if ($job_status_response->hasHeader('Retry-After')) {
$retry_after = (int) $job_status_response->getHeader('Retry-After')[0];
sleep($retry_after);
}
}
})
->run();