This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTGSHandler.php
79 lines (69 loc) · 1.75 KB
/
TGSHandler.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
<?php
use GuzzleHttp\Client;
class TGSHandler
{
private $defaultHeaders = [
'Api' => 'Tgstation.Server.Api/9.2.0',
'Accept' => 'application/json'
];
private string $baseUri;
private Client $client;
public function __construct(string $host, $port = null)
{
$this->baseUri = $host . ($port ? ":$port" : '');
$this->client = new Client([
'base_uri' => $this->baseUri,
'headers' => $this->defaultHeaders
]);
}
// Login to TGS
public function login(string $user, string $pass)
{
$response = $this->client->post('/', [
'auth' => [$user, $pass]
]);
$body = json_decode($response->getBody());
// Guzzle clients are immutable, so we just create a new one with the bearer
$this->client = new Client([
'base_uri' => $this->baseUri,
'headers' => array_merge($this->defaultHeaders, [
'Authorization' => "Bearer {$body->bearer}"
])
]);
}
// Get the ID of the current user
public function getUserId()
{
$response = $this->client->get('/User');
$body = json_decode($response->getBody());
return $body->id;
}
// Get current repository info
public function getRepo(int $instance)
{
$response = $this->client->get('/Repository', [
'headers' => ['Instance' => $instance],
]);
$body = json_decode($response->getBody());
return $body;
}
// Update the repository
public function updateRepo(int $instance, string $branch, array $testMerges = [])
{
$this->client->post('/Repository', [
'headers' => ['Instance' => $instance],
'json' => [
'updateFromOrigin' => true,
'reference' => $branch,
'newTestMerges' => $testMerges
]
]);
}
// Deploy the instance
public function deploy(int $instance)
{
$this->client->put('/DreamMaker', [
'headers' => ['Instance' => $instance]
]);
}
}