1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dandelion\VersionControl\Platform; |
6
|
|
|
|
7
|
|
|
use Dandelion\Configuration\Repository; |
8
|
|
|
use Dandelion\Configuration\Vcs; |
9
|
|
|
use Dandelion\Exception\SplitRepositoryNotInitializedException; |
10
|
|
|
use GuzzleHttp\ClientInterface as HttpClientInterface; |
11
|
|
|
|
12
|
|
|
use function array_merge; |
13
|
|
|
use function sprintf; |
14
|
|
|
use function str_replace; |
15
|
|
|
use function ucwords; |
16
|
|
|
|
17
|
|
|
class Github implements PlatformInterface |
18
|
|
|
{ |
19
|
|
|
public const FORMAT_DESCRIPTION = '[READ ONLY] Subtree split of the %s module.'; |
20
|
|
|
|
21
|
|
|
public const SPLIT_REPOSITORY_DEFAULTS = [ |
22
|
|
|
'private' => false, |
23
|
|
|
'has_issues' => false, |
24
|
|
|
'has_projects' => false, |
25
|
|
|
'has_wiki' => false, |
26
|
|
|
'is_template' => false, |
27
|
|
|
'auto_init' => false, |
28
|
|
|
'allow_squash_merge' => true, |
29
|
|
|
'allow_merge_commit' => false, |
30
|
|
|
'allow_rebase_merge' => false, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \GuzzleHttp\ClientInterface |
35
|
|
|
*/ |
36
|
|
|
protected $httpClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Dandelion\Configuration\Vcs |
40
|
|
|
*/ |
41
|
|
|
protected $vcs; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \GuzzleHttp\ClientInterface $httpClient |
45
|
|
|
* @param \Dandelion\Configuration\Vcs $vcs |
46
|
|
|
*/ |
47
|
|
|
public function __construct( |
48
|
|
|
HttpClientInterface $httpClient, |
49
|
|
|
Vcs $vcs |
50
|
|
|
) { |
51
|
|
|
$this->httpClient = $httpClient; |
52
|
|
|
$this->vcs = $vcs; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $repositoryName |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getRepositoryUrl(string $repositoryName): string |
61
|
|
|
{ |
62
|
|
|
return sprintf( |
63
|
|
|
'https://%[email protected]/%s/%s.git', |
64
|
|
|
$this->vcs->getToken(), |
65
|
|
|
$this->vcs->getOwner()->getName(), |
66
|
|
|
$repositoryName |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $repositoryName |
72
|
|
|
* |
73
|
|
|
* @return \Dandelion\VersionControl\Platform\PlatformInterface |
74
|
|
|
* |
75
|
|
|
* @throws \Dandelion\Exception\SplitRepositoryNotInitializedException |
76
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
77
|
|
|
*/ |
78
|
|
|
public function initSplitRepository(string $repositoryName): PlatformInterface |
79
|
|
|
{ |
80
|
|
|
$owner = $this->vcs->getOwner(); |
81
|
|
|
|
82
|
|
|
$url = sprintf('https://api.github.com/orgs/%s/repos', $owner->getName()); |
83
|
|
|
|
84
|
|
|
if ($owner->getType() !== 'organisation') { |
85
|
|
|
$url = 'https://api.github.com/user/repos'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$response = $this->httpClient->request('POST', $url, [ |
89
|
|
|
'headers' => [ |
90
|
|
|
'Accept' => 'application/vnd.github.v3+json', |
91
|
|
|
'Authorization' => sprintf('token %s', $this->vcs->getToken()) |
92
|
|
|
], 'json' => array_merge( |
93
|
|
|
[ |
94
|
|
|
'name' => $repositoryName, |
95
|
|
|
'description' => sprintf( |
96
|
|
|
static::FORMAT_DESCRIPTION, |
97
|
|
|
str_replace('-', '', ucwords($repositoryName, '-')) |
98
|
|
|
) |
99
|
|
|
], |
100
|
|
|
static::SPLIT_REPOSITORY_DEFAULTS |
101
|
|
|
), 'http_errors' => false |
102
|
|
|
]); |
103
|
|
|
|
104
|
|
|
if ($response->getStatusCode() !== 201) { |
105
|
|
|
throw new SplitRepositoryNotInitializedException( |
106
|
|
|
sprintf( |
107
|
|
|
'Could not initialize split repository "%s/%s".', |
108
|
|
|
$owner->getName(), |
109
|
|
|
$repositoryName |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $repositoryName |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
* |
122
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
123
|
|
|
*/ |
124
|
|
|
public function existsSplitRepository(string $repositoryName): bool |
125
|
|
|
{ |
126
|
|
|
$url = sprintf( |
127
|
|
|
'https://api.github.com/repos/%s/%s', |
128
|
|
|
$this->vcs->getOwner()->getName(), |
129
|
|
|
$repositoryName |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
$response = $this->httpClient->request('GET', $url, [ |
133
|
|
|
'headers' => [ |
134
|
|
|
'Accept' => 'application/vnd.github.v3+json', |
135
|
|
|
'Authorization' => sprintf('token %s', $this->vcs->getToken()) |
136
|
|
|
], |
137
|
|
|
'http_errors' => false |
138
|
|
|
]); |
139
|
|
|
|
140
|
|
|
return $response->getStatusCode() === 200; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|