-
Notifications
You must be signed in to change notification settings - Fork 6
/
GitHubStatusTrait.php
155 lines (142 loc) · 4.83 KB
/
GitHubStatusTrait.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Usher\Robo\Plugin\Traits;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Robo\Result;
/**
* Trait to allow setting of status checks on GitHub PRs from Tugboat.
*
* The methods in this trait will only work as expected when run in a Tugboat
* environment.
*/
trait GitHubStatusTrait
{
/**
* The error value when setting a GitHub check status.
*
* @var string
*/
protected $checkStatusError = 'error';
/**
* The pending value when setting a GitHub check status.
*
* @var string
*/
protected $checkStatusPending = 'pending';
/**
* The success value when setting a GitHub check status.
*
* @var string
*/
protected $checkStatusSuccess = 'success';
/**
* The GitHub API Base URL.
*
* @var string
*/
protected $githuApiBaseUrl = 'https://api.github.com';
/**
* The Tugboat dashboard URL.
*
* @var string
*/
protected $tugboatDashboardUrl = 'https://dashboard.tugboatqa.com';
/**
* Set GitHub PR status check to pending.
*
* @param string $gitHubCheckName
* The name of the status check.
*/
protected function setGitHubStatusPending(string $gitHubCheckName): void
{
$this->yell("Setting pending status on GitHub check: $gitHubCheckName");
$this->setGitHubStatus($this->checkStatusPending, $gitHubCheckName);
}
/**
* Set GitHub PR status check to success.
*
* @param string $gitHubCheckName
* The name of the status check.
* @param string $gitHubCheckDescription
* The description text to associate with the status check.
* @param string $gitHubCheckUrl
* The URL the status check will link to.
*/
protected function setGitHubStatusSuccess(
string $gitHubCheckName,
string $gitHubCheckDescription,
string $gitHubCheckUrl = null,
): void {
$this->yell("Setting success status on GitHub check: $gitHubCheckName");
$this->say($gitHubCheckDescription);
$this->setGitHubStatus($this->checkStatusSuccess, $gitHubCheckName, $gitHubCheckDescription, $gitHubCheckUrl);
}
/**
* Set GitHub PR status check to error.
*
* @param string $gitHubCheckName
* The name of the status check.
* @param string $gitHubCheckDescription
* The description text to associate with the status check.
* @param string $gitHubCheckUrl
* The URL the status check will link to.
*/
protected function setGitHubStatusError(
string $gitHubCheckName,
string $gitHubCheckDescription,
string $gitHubCheckUrl = null,
): void {
$this->yell("Setting failure status on GitHub check: $gitHubCheckName");
$this->say($gitHubCheckDescription);
$this->setGitHubStatus($this->checkStatusError, $gitHubCheckName, $gitHubCheckDescription, $gitHubCheckUrl);
}
/**
* Set GitHub PR status check.
*
* @param string $state
* The state string.
* @param string $gitHubCheckName
* The name of the status check.
* @param string $checkDescription
* The descriptive text to set on the check.
* @param string $targetUrl
* The URL the status check will link to.
*
* @see https://docs.github.com/en/rest/commits/statuses?apiVersion=2022-11-28#create-a-commit-status
*/
protected function setGitHubStatus(
string $state,
string $gitHubCheckName,
string $checkDescription = '',
string $targetUrl = null,
): void {
$tugboatPreviewID = getenv('TUGBOAT_PREVIEW_ID');
$tugboatPreviewSHA = getenv('TUGBOAT_PREVIEW_SHA');
$gitHubOrg = getenv('TUGBOAT_GITHUB_OWNER');
$gitHubRepo = getenv('TUGBOAT_GITHUB_REPO');
$githubStatusUrl = "$this->githuApiBaseUrl/repos/$gitHubOrg/$gitHubRepo/statuses/$tugboatPreviewSHA";
$gitHubAccessToken = getenv('GITHUB_ACCESS_TOKEN');
$body = [
'state' => $state,
'context' => $gitHubCheckName,
'target_url' => $targetUrl ?? "$this->tugboatDashboardUrl/$tugboatPreviewID",
];
if (strlen($checkDescription) > 0) {
$body['description'] = $checkDescription;
}
try {
$client = new Client(['timeout' => 5]);
$client->post($githubStatusUrl, [
'headers' => [
'Accept' => 'application/vnd.github+json',
'Authorization' => "Bearer $gitHubAccessToken",
'X-GitHub-Api-Version' => '2022-11-28',
],
'body' => json_encode($body, JSON_THROW_ON_ERROR),
]);
} catch (RequestException $exception) {
$this->yell('GitHub status request failed.');
$this->say($exception->getMessage());
}
}
}