mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-01 05:56:16 +00:00
@ -17,6 +17,35 @@ public function __construct(SourceControl $sourceControl)
|
||||
$this->sourceControl = $sourceControl;
|
||||
}
|
||||
|
||||
public function createRules(array $input): array
|
||||
{
|
||||
return [
|
||||
'token' => 'required',
|
||||
'url' => [
|
||||
'nullable',
|
||||
'url:http,https',
|
||||
'ends_with:/',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function createData(array $input): array
|
||||
{
|
||||
return [
|
||||
'token' => $input['token'] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
public function data(): array
|
||||
{
|
||||
// support for older data
|
||||
$token = $this->sourceControl->access_token ?? '';
|
||||
|
||||
return [
|
||||
'token' => $this->sourceControl->provider_data['token'] ?? $token,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws SourceControlIsNotConnected
|
||||
* @throws RepositoryNotFound
|
||||
|
@ -13,9 +13,33 @@ class Bitbucket extends AbstractSourceControlProvider
|
||||
{
|
||||
protected string $apiUrl = 'https://api.bitbucket.org/2.0';
|
||||
|
||||
public function createRules(array $input): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required',
|
||||
'password' => 'required',
|
||||
];
|
||||
}
|
||||
|
||||
public function createData(array $input): array
|
||||
{
|
||||
return [
|
||||
'username' => $input['username'] ?? '',
|
||||
'password' => $input['password'] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
public function data(): array
|
||||
{
|
||||
return [
|
||||
'username' => $this->sourceControl->provider_data['username'] ?? '',
|
||||
'password' => $this->sourceControl->provider_data['password'] ?? '',
|
||||
];
|
||||
}
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
$res = Http::withToken($this->sourceControl->access_token)
|
||||
$res = Http::withHeaders($this->getAuthenticationHeaders())
|
||||
->get($this->apiUrl.'/repositories');
|
||||
|
||||
return $res->successful();
|
||||
@ -26,7 +50,7 @@ public function connect(): bool
|
||||
*/
|
||||
public function getRepo(?string $repo = null): mixed
|
||||
{
|
||||
$res = Http::withToken($this->sourceControl->access_token)
|
||||
$res = Http::withHeaders($this->getAuthenticationHeaders())
|
||||
->get($this->apiUrl."/repositories/$repo");
|
||||
|
||||
$this->handleResponseErrors($res, $repo);
|
||||
@ -44,14 +68,15 @@ public function fullRepoUrl(string $repo, string $key): string
|
||||
*/
|
||||
public function deployHook(string $repo, array $events, string $secret): array
|
||||
{
|
||||
$response = Http::withToken($this->sourceControl->access_token)->post($this->apiUrl."/repositories/$repo/hooks", [
|
||||
'description' => 'deploy',
|
||||
'url' => url('/api/git-hooks?secret='.$secret),
|
||||
'events' => [
|
||||
'repo:'.implode(',', $events),
|
||||
],
|
||||
'active' => true,
|
||||
]);
|
||||
$response = Http::withHeaders($this->getAuthenticationHeaders())
|
||||
->post($this->apiUrl."/repositories/$repo/hooks", [
|
||||
'description' => 'deploy',
|
||||
'url' => url('/api/git-hooks?secret='.$secret),
|
||||
'events' => [
|
||||
'repo:'.implode(',', $events),
|
||||
],
|
||||
'active' => true,
|
||||
]);
|
||||
|
||||
if ($response->status() != 201) {
|
||||
throw new FailedToDeployGitHook($response->json()['error']['message']);
|
||||
@ -69,7 +94,8 @@ public function deployHook(string $repo, array $events, string $secret): array
|
||||
public function destroyHook(string $repo, string $hookId): void
|
||||
{
|
||||
$hookId = urlencode($hookId);
|
||||
$response = Http::withToken($this->sourceControl->access_token)->delete($this->apiUrl."/repositories/$repo/hooks/$hookId");
|
||||
$response = Http::withHeaders($this->getAuthenticationHeaders())
|
||||
->delete($this->apiUrl."/repositories/$repo/hooks/$hookId");
|
||||
|
||||
if ($response->status() != 204) {
|
||||
throw new FailedToDestroyGitHook('Error');
|
||||
@ -81,7 +107,7 @@ public function destroyHook(string $repo, string $hookId): void
|
||||
*/
|
||||
public function getLastCommit(string $repo, string $branch): ?array
|
||||
{
|
||||
$res = Http::withToken($this->sourceControl->access_token)
|
||||
$res = Http::withHeaders($this->getAuthenticationHeaders())
|
||||
->get($this->apiUrl."/repositories/$repo/commits?include=".$branch);
|
||||
|
||||
$this->handleResponseErrors($res, $repo);
|
||||
@ -108,7 +134,7 @@ public function getLastCommit(string $repo, string $branch): ?array
|
||||
*/
|
||||
public function deployKey(string $title, string $repo, string $key): void
|
||||
{
|
||||
$res = Http::withToken($this->sourceControl->access_token)->post(
|
||||
$res = Http::withHeaders($this->getAuthenticationHeaders())->post(
|
||||
$this->apiUrl."/repositories/$repo/deploy-keys",
|
||||
[
|
||||
'label' => $title,
|
||||
@ -116,7 +142,7 @@ public function deployKey(string $title, string $repo, string $key): void
|
||||
]
|
||||
);
|
||||
|
||||
if ($res->status() != 201) {
|
||||
if ($res->status() != 200) {
|
||||
throw new FailedToDeployGitKey($res->json()['error']['message']);
|
||||
}
|
||||
}
|
||||
@ -130,4 +156,15 @@ protected function getCommitter(string $raw): array
|
||||
'email' => Str::replace('>', '', $committer[1]),
|
||||
];
|
||||
}
|
||||
|
||||
private function getAuthenticationHeaders(): array
|
||||
{
|
||||
$username = $this->data()['username'];
|
||||
$password = $this->data()['password'];
|
||||
$basicAuth = base64_encode("$username:$password");
|
||||
|
||||
return [
|
||||
'Authorization' => 'Basic '.$basicAuth,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ public function connect(): bool
|
||||
{
|
||||
$res = Http::withHeaders([
|
||||
'Accept' => 'application/vnd.github.v3+json',
|
||||
'Authorization' => 'Bearer '.$this->sourceControl->access_token,
|
||||
'Authorization' => 'Bearer '.$this->data()['token'],
|
||||
])->get($this->apiUrl.'/user/repos');
|
||||
|
||||
return $res->successful();
|
||||
@ -34,7 +34,7 @@ public function getRepo(?string $repo = null): mixed
|
||||
}
|
||||
$res = Http::withHeaders([
|
||||
'Accept' => 'application/vnd.github.v3+json',
|
||||
'Authorization' => 'Bearer '.$this->sourceControl->access_token,
|
||||
'Authorization' => 'Bearer '.$this->data()['token'],
|
||||
])->get($url);
|
||||
|
||||
$this->handleResponseErrors($res, $repo);
|
||||
@ -54,7 +54,7 @@ public function deployHook(string $repo, array $events, string $secret): array
|
||||
{
|
||||
$response = Http::withHeaders([
|
||||
'Accept' => 'application/vnd.github.v3+json',
|
||||
'Authorization' => 'Bearer '.$this->sourceControl->access_token,
|
||||
'Authorization' => 'Bearer '.$this->data()['token'],
|
||||
])->post($this->apiUrl."/repos/$repo/hooks", [
|
||||
'name' => 'web',
|
||||
'events' => $events,
|
||||
@ -82,7 +82,7 @@ public function destroyHook(string $repo, string $hookId): void
|
||||
{
|
||||
$response = Http::withHeaders([
|
||||
'Accept' => 'application/vnd.github.v3+json',
|
||||
'Authorization' => 'Bearer '.$this->sourceControl->access_token,
|
||||
'Authorization' => 'Bearer '.$this->data()['token'],
|
||||
])->delete($this->apiUrl."/repos/$repo/hooks/$hookId");
|
||||
|
||||
if ($response->status() != 204) {
|
||||
@ -98,7 +98,7 @@ public function getLastCommit(string $repo, string $branch): ?array
|
||||
$url = $this->apiUrl.'/repos/'.$repo.'/commits/'.$branch;
|
||||
$res = Http::withHeaders([
|
||||
'Accept' => 'application/vnd.github.v3+json',
|
||||
'Authorization' => 'Bearer '.$this->sourceControl->access_token,
|
||||
'Authorization' => 'Bearer '.$this->data()['token'],
|
||||
])->get($url);
|
||||
|
||||
$this->handleResponseErrors($res, $repo);
|
||||
@ -124,7 +124,7 @@ public function getLastCommit(string $repo, string $branch): ?array
|
||||
*/
|
||||
public function deployKey(string $title, string $repo, string $key): void
|
||||
{
|
||||
$response = Http::withToken($this->sourceControl->access_token)->post(
|
||||
$response = Http::withToken($this->data()['token'])->post(
|
||||
$this->apiUrl.'/repos/'.$repo.'/keys',
|
||||
[
|
||||
'title' => $title,
|
||||
|
@ -16,7 +16,7 @@ class Gitlab extends AbstractSourceControlProvider
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
$res = Http::withToken($this->sourceControl->access_token)
|
||||
$res = Http::withToken($this->data()['token'])
|
||||
->get($this->getApiUrl().'/projects');
|
||||
|
||||
return $res->successful();
|
||||
@ -28,7 +28,7 @@ public function connect(): bool
|
||||
public function getRepo(?string $repo = null): mixed
|
||||
{
|
||||
$repository = $repo ? urlencode($repo) : null;
|
||||
$res = Http::withToken($this->sourceControl->access_token)
|
||||
$res = Http::withToken($this->data()['token'])
|
||||
->get($this->getApiUrl().'/projects/'.$repository.'/repository/commits');
|
||||
|
||||
$this->handleResponseErrors($res, $repo);
|
||||
@ -49,7 +49,7 @@ public function fullRepoUrl(string $repo, string $key): string
|
||||
public function deployHook(string $repo, array $events, string $secret): array
|
||||
{
|
||||
$repository = urlencode($repo);
|
||||
$response = Http::withToken($this->sourceControl->access_token)->post(
|
||||
$response = Http::withToken($this->data()['token'])->post(
|
||||
$this->getApiUrl().'/projects/'.$repository.'/hooks',
|
||||
[
|
||||
'description' => 'deploy',
|
||||
@ -84,7 +84,7 @@ public function deployHook(string $repo, array $events, string $secret): array
|
||||
public function destroyHook(string $repo, string $hookId): void
|
||||
{
|
||||
$repository = urlencode($repo);
|
||||
$response = Http::withToken($this->sourceControl->access_token)->delete(
|
||||
$response = Http::withToken($this->data()['token'])->delete(
|
||||
$this->getApiUrl().'/projects/'.$repository.'/hooks/'.$hookId
|
||||
);
|
||||
|
||||
@ -99,7 +99,7 @@ public function destroyHook(string $repo, string $hookId): void
|
||||
public function getLastCommit(string $repo, string $branch): ?array
|
||||
{
|
||||
$repository = urlencode($repo);
|
||||
$res = Http::withToken($this->sourceControl->access_token)
|
||||
$res = Http::withToken($this->data()['token'])
|
||||
->get($this->getApiUrl().'/projects/'.$repository.'/repository/commits?ref_name='.$branch);
|
||||
|
||||
$this->handleResponseErrors($res, $repo);
|
||||
@ -126,7 +126,7 @@ public function getLastCommit(string $repo, string $branch): ?array
|
||||
public function deployKey(string $title, string $repo, string $key): void
|
||||
{
|
||||
$repository = urlencode($repo);
|
||||
$response = Http::withToken($this->sourceControl->access_token)->post(
|
||||
$response = Http::withToken($this->data()['token'])->post(
|
||||
$this->getApiUrl().'/projects/'.$repository.'/deploy_keys',
|
||||
[
|
||||
'title' => $title,
|
||||
|
@ -4,6 +4,12 @@
|
||||
|
||||
interface SourceControlProvider
|
||||
{
|
||||
public function createRules(array $input): array;
|
||||
|
||||
public function createData(array $input): array;
|
||||
|
||||
public function data(): array;
|
||||
|
||||
public function connect(): bool;
|
||||
|
||||
public function getRepo(?string $repo = null): mixed;
|
||||
|
Reference in New Issue
Block a user