1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the composer-changelogs project. |
5
|
|
|
* |
6
|
|
|
* (c) Loïck Piera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pyrech\ComposerChangelogs\UrlGenerator; |
13
|
|
|
|
14
|
|
|
use Pyrech\ComposerChangelogs\Version; |
15
|
|
|
|
16
|
|
|
class BitbucketUrlGenerator extends AbstractUrlGenerator |
17
|
|
|
{ |
18
|
|
|
const DOMAIN = 'bitbucket.org'; |
19
|
|
|
const URL_REGEX = '@bitbucket.org/(?P<user>[^/]+)/(?P<repository>[^/]+)@'; |
20
|
|
|
const SSH_REGEX = '/^git@bitbucket\.org:(?P<user>[^\/]+)\/(?P<repository>.+)\.git$/'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function supports($sourceUrl) |
26
|
|
|
{ |
27
|
|
|
return strpos($sourceUrl, self::DOMAIN) !== false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function generateCompareUrl($sourceUrlFrom, Version $versionFrom, $sourceUrlTo, Version $versionTo) |
34
|
|
|
{ |
35
|
|
|
$sourceUrlFrom = $this->generateBaseUrl($this->reformatSshUrl($sourceUrlFrom)); |
36
|
|
|
$sourceUrlTo = $this->generateBaseUrl($this->reformatSshUrl($sourceUrlTo)); |
37
|
|
|
|
38
|
|
|
// Check if comparison across forks is needed |
39
|
|
|
if ($sourceUrlFrom !== $sourceUrlTo) { |
40
|
|
|
$repositoryFrom = $this->extractRepositoryInformation($sourceUrlFrom); |
41
|
|
|
$repositoryTo = $this->extractRepositoryInformation($sourceUrlTo); |
42
|
|
|
|
43
|
|
|
return sprintf( |
44
|
|
|
'%s/branches/compare/%s/%s:%s%%0D%s/%s:%s', |
45
|
|
|
$sourceUrlTo, |
46
|
|
|
$repositoryTo['user'], |
47
|
|
|
$repositoryTo['repository'], |
48
|
|
|
$this->getCompareVersion($versionTo), |
49
|
|
|
$repositoryFrom['user'], |
50
|
|
|
$repositoryFrom['repository'], |
51
|
|
|
$this->getCompareVersion($versionFrom) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return sprintf( |
56
|
|
|
'%s/branches/compare/%s%%0D%s', |
57
|
|
|
$sourceUrlTo, |
58
|
|
|
$this->getCompareVersion($versionTo), |
59
|
|
|
$this->getCompareVersion($versionFrom) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public function generateReleaseUrl($sourceUrl, Version $version) |
67
|
|
|
{ |
68
|
|
|
// Releases are not supported on Bitbucket :'( |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $sourceUrl |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
private function extractRepositoryInformation($sourceUrl) |
78
|
|
|
{ |
79
|
|
|
preg_match(self::URL_REGEX, $sourceUrl, $matches); |
80
|
|
|
|
81
|
|
|
if (!isset($matches['user']) || !isset($matches['repository'])) { |
82
|
|
|
throw new \LogicException( |
83
|
|
|
sprintf('Malformed Bitbucket source url: "%s"', $sourceUrl) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
'user' => $matches['user'], |
89
|
|
|
'repository' => $matches['repository'], |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $url |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
private function reformatSshUrl($url) |
99
|
|
|
{ |
100
|
|
|
if (preg_match(self::SSH_REGEX, $url, $matches)) { |
101
|
|
|
return sprintf( |
102
|
|
|
'https://bitbucket.org/%s/%s', |
103
|
|
|
$matches['user'], |
104
|
|
|
$matches['repository'] |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $url; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|