/
BitbucketBuild.php
320 lines (269 loc) · 9.27 KB
/
BitbucketBuild.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
namespace PHPCensor\Model\Build;
use GuzzleHttp\Client;
use PHPCensor\Builder;
use PHPCensor\Helper\Bitbucket;
use PHPCensor\Helper\Diff;
use PHPCensor\Config;
use PHPCensor\Model\Build;
use PHPCensor\Model\BuildError;
/**
* BitBucket Build Model
*
* @author Dan Cryer <dan@block8.co.uk>
*/
class BitbucketBuild extends GitBuild
{
/**
* Get link to commit from another source (i.e. BitBucket)
*
* @return string
*/
public function getCommitLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/commits/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. BitBucket)
*
* @return string
*/
public function getBranchLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. BitBucket)
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return 'https://bitbucket.org/' . $remoteReference . '/src/?at=' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. BitBucket)
*
* @return string
*/
public function getTagLink()
{
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '/src/?at=' . $this->getTag();
}
/**
* Send status updates to any relevant third parties (i.e. Bitbucket)
*
* @return boolean
*/
public function sendStatusPostback()
{
if (!in_array($this->getSource(), [Build::SOURCE_WEBHOOK, Build::SOURCE_WEBHOOK_PULL_REQUEST], true)) {
return false;
}
$project = $this->getProject();
if (empty($project)) {
return false;
}
$username = Config::getInstance()->get('php-censor.bitbucket.username');
$appPassword = Config::getInstance()->get('php-censor.bitbucket.app_password');
if (empty($username) || empty($appPassword) || empty($this->data['id'])) {
return false;
}
$allowStatusCommit = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.status.commit',
false
);
if (!$allowStatusCommit) {
return false;
}
switch ($this->getStatus()) {
case 0:
case 1:
$status = 'INPROGRESS';
$description = 'PHP Censor build running.';
break;
case 2:
$status = 'SUCCESSFUL';
$description = 'PHP Censor build passed.';
break;
case 3:
$status = 'FAILED';
$description = 'PHP Censor build failed.';
break;
default:
$status = 'STOPPED';
$description = 'PHP Censor build failed to complete.';
break;
}
$phpCensorUrl = Config::getInstance()->get('php-censor.url');
$url = sprintf(
'/2.0/repositories/%s/commit/%s/statuses/build',
Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()
? $this->getExtra('remote_reference')
: $project->getReference(),
$this->getCommitId()
);
$client = new Client([
'base_uri' => 'https://api.bitbucket.org',
'http_errors' => false,
]);
$response = $client->post($url, [
'auth' => [$username, $appPassword],
'headers' => [
'Content-Type' => 'application/json',
],
'json' => [
'state' => $status,
'key' => 'PHP-CENSOR',
'url' => $phpCensorUrl . '/build/view/' . $this->getId(),
'name' => 'PHP Censor Build #' . $this->getId(),
'description' => $description,
],
]);
$status = (integer)$response->getStatusCode();
return ($status >= 200 && $status < 300);
}
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
$key = trim($this->getProject()->getSshPrivateKey());
if (!empty($key)) {
return 'git@bitbucket.org:' . $this->getProject()->getReference() . '.git';
} else {
return 'https://bitbucket.org/' . $this->getProject()->getReference() . '.git';
}
}
/**
* Get a template to use for generating links to files.
*
* @return string
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$reference = $this->getExtra('remote_reference');
}
$link = 'https://bitbucket.org/' . $reference . '/';
$link .= 'src/' . $this->getCommitId() . '/';
$link .= '{FILE}';
$link .= '#{BASEFILE}-{LINE}';
return $link;
}
/**
* @inheritdoc
*/
protected function postCloneSetup(Builder $builder, $cloneTo, array $extra = null)
{
$success = true;
$skipGitFinalization = false;
try {
if (Build::SOURCE_WEBHOOK_PULL_REQUEST === $this->getSource()) {
$helper = new Bitbucket();
$diff = $helper->getPullRequestDiff(
$this->getProject()->getReference(),
$this->getExtra('pull_request_number')
);
$diffFile = $this->writeDiff($builder->buildPath, $diff);
$cmd = 'cd "%s" && git checkout -b php-censor/' . $this->getId() . ' && git apply "%s"';
$success = $builder->executeCommand($cmd, $cloneTo, $diffFile);
unlink($diffFile);
$skipGitFinalization = true;
}
} catch (\Exception $ex) {
$success = false;
}
if ($success && !$skipGitFinalization) {
$success = parent::postCloneSetup($builder, $cloneTo, $extra);
}
return $success;
}
/**
* Create an diff file on disk for this build.
*
* @param string $cloneTo
* @param string $diff
*
* @return string
*/
protected function writeDiff($cloneTo, $diff)
{
$filePath = dirname($cloneTo . '/temp');
$diffFile = $filePath . '.patch';
file_put_contents($diffFile, $diff);
chmod($diffFile, 0600);
return $diffFile;
}
/**
* @inheritDoc
*/
public function reportError(
Builder $builder,
$plugin,
$message,
$severity = BuildError::SEVERITY_NORMAL,
$file = null,
$lineStart = null,
$lineEnd = null
) {
$allowCommentCommit = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.comments.commit',
false
);
$allowCommentPullRequest = (boolean)Config::getInstance()->get(
'php-censor.bitbucket.comments.pull_request',
false
);
if ($allowCommentCommit || $allowCommentPullRequest) {
$diffLineNumber = $this->getDiffLineNumber($builder, $file, $lineStart);
if (!is_null($diffLineNumber)) {
$helper = new Bitbucket();
$repo = $this->getProject()->getReference();
$prNumber = $this->getExtra('pull_request_number');
$commit = $this->getCommitId();
if (!empty($prNumber)) {
if ($allowCommentPullRequest) {
$helper->createPullRequestComment($repo, $prNumber, $commit, $file, $lineStart, $message);
}
} else {
if ($allowCommentCommit) {
$helper->createCommitComment($repo, $commit, $file, $lineStart, $message);
}
}
}
}
parent::reportError($builder, $plugin, $message, $severity, $file, $lineStart, $lineEnd);
}
/**
* Uses git diff to figure out what the diff line position is, based on the error line number.
*
* @param Builder $builder
* @param string $file
* @param integer $line
*
* @return integer|null
*/
protected function getDiffLineNumber(Builder $builder, $file, $line)
{
$builder->logExecOutput(false);
$line = (integer)$line;
$prNumber = $this->getExtra('pull_request_number');
$path = $builder->buildPath;
if (!empty($prNumber)) {
$builder->executeCommand('cd %s && git diff origin/%s "%s"', $path, $this->getBranch(), $file);
} else {
$commitId = $this->getCommitId();
$compare = empty($commitId) ? 'HEAD' : $commitId;
$builder->executeCommand('cd %s && git diff %s^^ "%s"', $path, $compare, $file);
}
$builder->logExecOutput(true);
$diff = $builder->getLastOutput();
$helper = new Diff();
$lines = $helper->getLinePositions($diff);
return isset($lines[$line]) ? $lines[$line] : null;
}
}