-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBitbucketServerBuild.php
More file actions
183 lines (154 loc) · 4.86 KB
/
Copy pathBitbucketServerBuild.php
File metadata and controls
183 lines (154 loc) · 4.86 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
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
<?php
namespace PHPCensor\Model\Build;
use Exception;
use PHPCensor\Builder;
use PHPCensor\Common\Exception\RuntimeException;
use PHPCensor\Model\Build;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class BitbucketServerBuild extends GitBuild
{
public static array $pullrequestTriggersToSources = [
'pr:opened' => Build::SOURCE_WEBHOOK_PULL_REQUEST_CREATED,
'pr:updated' => Build::SOURCE_WEBHOOK_PULL_REQUEST_UPDATED,
'pr:approved' => Build::SOURCE_WEBHOOK_PULL_REQUEST_APPROVED,
'pr:merged' => Build::SOURCE_WEBHOOK_PULL_REQUEST_MERGED,
];
/**
* Get link to commit from another source (i.e. BitBucket)
*
* @return string
*/
public function getCommitLink()
{
return $this->getProject()->getReference() . '/commits/' . $this->getCommitId();
}
/**
* Get link to branch from another source (i.e. BitBucket)
*
* @return string
*/
public function getBranchLink()
{
return $this->getProject()->getReference() . '/src/?at=' . $this->getBranch();
}
/**
* Get link to remote branch (from pull request) from another source (i.e. BitBucket)
*
* @return string
*/
public function getRemoteBranchLink()
{
$remoteBranch = $this->getExtra('remote_branch');
$remoteReference = $this->getExtra('remote_reference');
return $this->getProject()->getReference() . $remoteReference . '/src/?at=' . $remoteBranch;
}
/**
* Get link to tag from another source (i.e. BitBucket)
*
* @return string
*/
public function getTagLink()
{
return $this->getProject()->getReference() . '/src/?at=' . $this->getTag();
}
/**
* Send status updates to any relevant third parties (i.e. Bitbucket)
*
* @return boolean
*/
public function sendStatusPostback()
{
// Just do success. Will have to build a webhook in
// bitbucket to get this or install an app that we can format a request to.
return true;
}
/**
* Get the URL to be used to clone this remote repository.
*
* @return string
*/
protected function getCloneUrl()
{
return $this->getProject()->getReference() . '.git';
}
/**
* Get a template to use for generating links to files.
*
* @return string|null
*/
public function getFileLinkTemplate()
{
$reference = $this->getProject()->getReference();
if (\in_array($this->getSource(), Build::$pullRequestSources, true)) {
$reference = $this->getExtra('remote_reference');
}
$link = $this->getProject()->getReference() . $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 (\in_array($this->getSource(), Build::$pullRequestSources, true)) {
$diff = $this->getPullRequestDiff($builder, $cloneTo, $extra['remote_branch']);
$diffFile = $this->writeDiff($builder->buildPath, $diff);
$cmd = 'cd "%s" && git checkout -b php-censor/' . $this->getId();
$success = $builder->executeCommand($cmd, $cloneTo);
if ($success) {
$applyCmd = 'git apply "%s"';
$success = $builder->executeCommand($applyCmd, $diffFile);
}
$skipGitFinalization = true;
}
} catch (\Throwable $ex) {
$success = false;
}
if ($success && !$skipGitFinalization) {
$success = parent::postCloneSetup($builder, $cloneTo, $extra);
}
return $success;
}
/**
* Create request patch with diff
*
* @param string $cloneTo
* @param string $targetBranch
*/
protected function getPullRequestDiff(Builder $builder, $cloneTo, $targetBranch)
{
$cmd = 'cd "%s" && git diff %s';
$success = $builder->executeCommand($cmd, $cloneTo, $targetBranch);
if ($success) {
return $builder->getLastOutput();
}
throw new RuntimeException('Unable to create diff patch.');
}
/**
* 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;
}
}