-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookNotify.php
More file actions
124 lines (109 loc) · 3.63 KB
/
Copy pathWebhookNotify.php
File metadata and controls
124 lines (109 loc) · 3.63 KB
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
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\GuzzleException;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Exception\Exception;
use PHPCensor\Common\Plugin\Plugin;
/**
* WebhookNotify Plugin
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Lee Willis (Ademti Software): https://www.ademti-software.co.uk
*/
class WebhookNotify extends Plugin
{
/**
* @var string The URL to send the webhook to.
*/
private string $url;
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'webhook_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$payload = [
'project_id' => $this->project->getId(),
'project_title' => $this->project->getTitle(),
'build_id' => $this->build->getId(),
'commit_id' => $this->build->getCommitId(),
'short_commit_id' => \substr((string)$this->build->getCommitId(), 0, 7),
'branch' => $this->build->getBranch(),
'branch_link' => $this->build->getBranchLink(),
'committer_email' => $this->build->getCommitterEmail(),
'commit_message' => $this->build->getCommitMessage(),
'commit_link' => $this->build->getCommitLink(),
'build_link' => $this->variableInterpolator->interpolate('%BUILD_LINK%'),
'project_link' => $this->variableInterpolator->interpolate('%PROJECT_LINK%'),
'status_code' => $this->build->getStatus(),
'readable_status' => $this->getReadableStatus(),
];
try {
$version = $this->variableInterpolator->interpolate('%SYSTEM_VERSION%');
$userAgent = 'PHP Censor/' . $version;
$client = new HttpClient([
'headers' => [
'User-Agent' => $userAgent,
],
]);
$client->request(
'POST',
$this->url,
['json' => $payload]
);
} catch (GuzzleException) {
return false;
}
return true;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
if (!$this->options->all()) {
throw new Exception("Please configure the options for the WebhookNotify plugin!");
}
if (!$this->options->get('url')) {
throw new Exception("Please define the url for WebhookNotify plugin!");
}
$this->url = \trim((string)$this->options->get('url', ''));
}
private function getReadableStatus(): string
{
return match ($this->build->getStatus()) {
BuildInterface::STATUS_PENDING => 'Pending',
BuildInterface::STATUS_RUNNING => 'Running',
BuildInterface::STATUS_SUCCESS => 'Successful',
BuildInterface::STATUS_FAILED => 'Failed',
default => \sprintf('Unknown (%d)', $this->build->getStatus()),
};
}
}