-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBitbucket.php
More file actions
144 lines (117 loc) · 3.97 KB
/
Copy pathBitbucket.php
File metadata and controls
144 lines (117 loc) · 3.97 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
<?php
namespace PHPCensor\Helper;
use GuzzleHttp\Client;
use PHPCensor\Common\Application\ConfigurationInterface;
use PHPCensor\Model\Build;
/**
* The Bitbucket Helper class provides some Bitbucket API call functionality.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Bitbucket
{
private ConfigurationInterface $configuration;
public function __construct(ConfigurationInterface $configuration)
{
$this->configuration = $configuration;
}
/**
* Create a comment on a specific file (and commit) in a Bitbucket Pull Request.
*
* @param string $repo
* @param int $pullId
* @param string $commitId
* @param string $file
* @param int $line
* @param string $comment
*
* @return null
*/
public function createPullRequestComment($repo, $pullId, $commitId, $file, $line, $comment)
{
$username = $this->configuration->get('php-censor.bitbucket.username');
$appPassword = $this->configuration->get('php-censor.bitbucket.app_password');
if (empty($username) || empty($appPassword)) {
return;
}
$url = '/1.0/repositories/' . $repo . '/pullrequests/' . $pullId . '/comments/';
$data = [
'content' => $comment,
'anchor' => \substr($commitId, 0, 12),
'filename' => $file,
];
if ($line > 0) {
$data['line_to'] = $line;
}
$client = new Client(['base_uri' => 'https://api.bitbucket.org']);
$client->post($url, [
'auth' => [$username, $appPassword],
'headers' => [
'Content-Type' => 'application/json',
],
'json' => $data,
]);
}
/**
* Create a comment on a Bitbucket commit.
*
* @return null
*/
public function createCommitComment($repo, $commitId, $file, $line, $comment)
{
$username = $this->configuration->get('php-censor.bitbucket.username');
$appPassword = $this->configuration->get('php-censor.bitbucket.app_password');
if (empty($username) || empty($appPassword)) {
return;
}
$url = '/1.0/repositories/' . $repo . '/changesets/' . $commitId . '/comments';
$data = [
'content' => $comment,
'filename' => $file,
];
if ($line > 0) {
$data['line_to'] = $line;
}
$client = new Client(['base_uri' => 'https://api.bitbucket.org']);
$client->post($url, [
'auth' => [$username, $appPassword],
'headers' => [
'Content-Type' => 'application/json',
],
'json' => $data,
]);
}
/**
* @param string $repo
* @param int $pullRequestId
*
* @return string
*/
public function getPullRequestDiff($repo, $pullRequestId)
{
$username = $this->configuration->get('php-censor.bitbucket.username');
$appPassword = $this->configuration->get('php-censor.bitbucket.app_password');
if (empty($username) || empty($appPassword)) {
return;
}
$url = '/2.0/repositories/' . $repo . '/pullrequests/' . $pullRequestId . '/diff';
$client = new Client(['base_uri' => 'https://api.bitbucket.org']);
$response = $client->get($url, ['auth' => [$username, $appPassword]]);
return (string)$response->getBody();
}
public function getFileLinkTemplate(Build $build): string
{
$reference = $build->getProject()->getReference();
if (\in_array($build->getSource(), Build::$pullRequestSources, true)) {
$reference = $build->getExtra('remote_reference');
}
$link = 'https://bitbucket.org/' . $reference . '/';
$link .= 'src/' . $build->getCommitId() . '/';
$link .= '{FILE}';
$link .= '#{BASEFILE}-{LINE}';
return $link;
}
}