-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitbucketRepositoryAdaptor.php
206 lines (177 loc) · 6.78 KB
/
BitbucketRepositoryAdaptor.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
<?php namespace Netcarver;
require_once 'GitRepositoryInterface.php';
require_once 'GitRepositoryAdaptor.php';
/**
*/
class BitbucketRepositoryAdaptor extends GitRepositoryAdaptor implements GitRepositoryInterface {
// Data common across all instances...
static protected $info = [
'name' => 'Bitbucket',
'icon' => '<i class="fa fa-icon fa-bitbucket"></i>', // Fontawesome icon markup
'limit' => 60,
'remaining' => 60,
'reset' => 0,
'capabilities' => [
/* 'tags', */
/* 'release-notes', */
/* 'tag-to-tag-commits', */
/* 'changelog-access', */
'commits',
/* 'forks', */
],
];
/**
* Returns information about this repository interface.
*
* This is a mixture of static and per-instance information.
*/
public function GetInfo() {
return array_merge(
self::$info,
[
'remote' => $this->remote,
'owner' => $this->owner,
'repo' => $this->repo,
'tags' => $this->tags,
]
);
}
public function SetRemote($remote) {
}
public function GetRepoInfo() {
return [];
}
/**
*
*/
public function __construct($http, $remote, $application, array $options) {
$owner = '';
$repo = '';
$m = [];
if (array_key_exists('debug', $options)) {
$this->debug($options['debug']);
}
if (array_key_exists('cache_dir', $options)) {
$this->cache = new \Netcarver\FileCache();
$this->cache->setCacheDirectory($options['cache_dir']);
} else {
throw new \Exception('Cache directory is needed.');
}
$ok = preg_match('~^https?://bitbucket.org/([^/]++)/(.++)~i', $remote, $m);
if ($ok) {
$this->headers['User-Agent'] = $application;
$this->http = $http;
$this->remote = $remote;
$this->owner = $m[1];
$this->repo = rtrim($m[2], '/\\');
$this->encoded_owner = rawurlencode($m[1]);
$this->encoded_repo = rawurlencode($this->repo);
$this->GetTags();
} else {
throw new \Exception('Invalid repository signature');
}
}
public function GetTags() {
if (null === $this->tags) {
$url = "https://api.bitbucket.org/2.0/repositories/{$this->encoded_owner}/{$this->encoded_repo}/refs/tags";
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code);
if (200 == $http_code || 304 == $http_code) {
$result = [];
$num = count($reply['values']);
if ($num) {
foreach ($reply['values'] as $tagref) {
$sha = $tagref['object']['hash'];
$tag = str_replace('refs/tags/', '', $tagref['ref']);
$result[$sha] = $tag;
}
}
$this->tags = $result;
}
}
return $this->tags;
}
/**
* GetCommits(12); // Returns the latest 12 commits.
* GetCommits('0.3.0', '0.4.0'); // Returns the commits between the given tags
* GetCommits('0.3.0'); // Returns the commits between '0.3.0' and HEAD.
*
* If the commits cannot be accessed, null is returned.
*/
public function GetCommits($startref, $endref='HEAD') {
if (is_string($startref) && ('' !== trim($startref))) {
//
// If we could not access any tags for this repo, then there will be no tag-to-tag commit.
//
// If the startref or the endref (when not HEAD) is not in the taglist there will be no tag-to-tag list.
if (null === $this->tags || !in_array($startref, $this->tags)) {
return null;
}
if ('HEAD' !== $endref && !in_array($endref, $this->tags)) {
return null;
}
//
// Pull all commits between the startref and endref reference points.
// Useful for getting a list of commits between tags.
//
$startref_encoded = rawurlencode($startref);
$endref_encoded = rawurlencode($endref);
$url = "https://api.bitbucket.org/2.0/repositories/{$this->encoded_owner}/{$this->encoded_repo}/compare/$startref_encoded...$endref_encoded";
$slice = false;
} else {
//
// Pull the last n commits.
//
$url = "https://api.bitbucket.org/2.0/repositories/{$this->encoded_owner}/{$this->encoded_repo}/commits/master?fields=values.hash,values.links.html,values.date,values.message,values.author.user.display_name";
$slice = intval($startref);
if ($slice < 1) $slice = 1;
if ($slice > 30) $slice = 30;
}
$http_code = null;
$reply = $this->RepositoryRead($url, $http_code);
if (200 == $http_code || 304 == $http_code) {
if ($slice) {
$repo_url = null;
$commits = array_slice($reply['values'], 0, $slice);
} else {
//$repo_url = $reply['html_url'];
$commits = $reply['values'];
}
//\TD::barDump($commits, "Commit List");
$history = [];
foreach ($commits as $commit) {
//\TD::barDump($commit);
$entry = [];
$entry['sha'] = $commit['hash'];
$entry['url'] = $commit['links']['html']['href'];
$entry['author'] = $commit['author']['user']['display_name'];
$entry['date'] = str_replace('+00:00', 'Z', $commit['date']);
$entry['message'] = $commit['message'];
$entry['tag'] = @$this->tags[$entry['sha']];
ksort($entry);
$history[] = $entry;
}
return ['commits' => $history, 'url' => $repo_url];
}
return null;
}
/**
*
*/
public function GetChangelog() {
$url = "https://api.bitbucket.com/2.0/repositories/{$this->encoded_owner}/{$this->encoded_repo}/src/HEAD/CHANGELOG.md";
$http_code = null;
return $this->RepositoryRead($url, $http_code, false);
}
/**
*
*/
public function GetForks($sort) {
if (!in_array($sort, ['newest', 'oldest', 'watchers'])) {
$sort = 'newest';
}
$url = "https://api.bitbucket.org/2.0/repositories/{$this->encoded_owner}/{$this->encoded_repo}/forks";
$http_code = null;
return $this->RepositoryRead($url, $http_code);
}
}