mirror of
https://github.com/vitodeploy/vito.git
synced 2025-04-19 09:51:37 +00:00
Fix auto-deployment branch (#506)
This commit is contained in:
parent
1223ea1499
commit
e99146209e
@ -29,7 +29,8 @@ public function __invoke(Request $request)
|
|||||||
->firstOrFail();
|
->firstOrFail();
|
||||||
|
|
||||||
foreach ($gitHook->actions as $action) {
|
foreach ($gitHook->actions as $action) {
|
||||||
if ($action == 'deploy') {
|
$webhookBranch = $gitHook->site->sourceControl->provider()->getWebhookBranch($request->array());
|
||||||
|
if ($action == 'deploy' && $gitHook->site->branch === $webhookBranch) {
|
||||||
try {
|
try {
|
||||||
app(Deploy::class)->run($gitHook->site);
|
app(Deploy::class)->run($gitHook->site);
|
||||||
} catch (SourceControlIsNotConnected) {
|
} catch (SourceControlIsNotConnected) {
|
||||||
|
@ -70,4 +70,9 @@ protected function handleResponseErrors(Response $res, string $repo): void
|
|||||||
throw new RepositoryPermissionDenied($repo);
|
throw new RepositoryPermissionDenied($repo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getWebhookBranch(array $payload): string
|
||||||
|
{
|
||||||
|
return str($payload['ref'] ?? '')->after('refs/heads/')->toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -183,4 +183,9 @@ private function getAuthenticationHeaders(): array
|
|||||||
'Authorization' => 'Basic '.$basicAuth,
|
'Authorization' => 'Basic '.$basicAuth,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getWebhookBranch(array $payload): string
|
||||||
|
{
|
||||||
|
return data_get($payload, 'push.changes.0.new.name', 'default-branch');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,4 +176,9 @@ public function getApiUrl(): string
|
|||||||
|
|
||||||
return $host.$this->apiVersion;
|
return $host.$this->apiVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getWebhookBranch(array $payload): string
|
||||||
|
{
|
||||||
|
return $payload['ref'] ?? '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,4 +36,6 @@ public function getLastCommit(string $repo, string $branch): ?array;
|
|||||||
* @throws FailedToDeployGitKey
|
* @throws FailedToDeployGitKey
|
||||||
*/
|
*/
|
||||||
public function deployKey(string $title, string $repo, string $key): void;
|
public function deployKey(string $title, string $repo, string $key): void;
|
||||||
|
|
||||||
|
public function getWebhookBranch(array $payload): string;
|
||||||
}
|
}
|
||||||
|
@ -206,19 +206,21 @@ public function test_update_env_file(): void
|
|||||||
SSH::assertExecutedContains('APP_ENV="production"');
|
SSH::assertExecutedContains('APP_ENV="production"');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_git_hook_deployment(): void
|
/**
|
||||||
|
* @dataProvider hookData
|
||||||
|
*/
|
||||||
|
public function test_git_hook_deployment(string $provider, array $webhook, string $url, array $payload, bool $skip): void
|
||||||
{
|
{
|
||||||
SSH::fake();
|
SSH::fake();
|
||||||
Http::fake([
|
Http::fake([
|
||||||
'github.com/*' => Http::response([
|
$url => Http::response($payload),
|
||||||
'sha' => '123',
|
]);
|
||||||
'commit' => [
|
|
||||||
'message' => 'test commit message',
|
$this->site->update([
|
||||||
'name' => 'test commit name',
|
'branch' => 'main',
|
||||||
'email' => 'user@example.com',
|
]);
|
||||||
'url' => 'https://github.com',
|
$this->site->sourceControl->update([
|
||||||
],
|
'provider' => $provider,
|
||||||
], 200),
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
GitHook::factory()->create([
|
GitHook::factory()->create([
|
||||||
@ -233,15 +235,29 @@ public function test_git_hook_deployment(): void
|
|||||||
'content' => 'git pull',
|
'content' => 'git pull',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->post(route('api.git-hooks'), [
|
$this->post(route('api.git-hooks', [
|
||||||
'secret' => 'secret',
|
'secret' => 'secret',
|
||||||
])->assertSessionDoesntHaveErrors();
|
]), $webhook)->assertSessionDoesntHaveErrors();
|
||||||
|
|
||||||
|
if ($skip) {
|
||||||
|
$this->assertDatabaseMissing('deployments', [
|
||||||
|
'site_id' => $this->site->id,
|
||||||
|
'deployment_script_id' => $this->site->deploymentScript->id,
|
||||||
|
'status' => DeploymentStatus::FINISHED,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$this->assertDatabaseHas('deployments', [
|
$this->assertDatabaseHas('deployments', [
|
||||||
'site_id' => $this->site->id,
|
'site_id' => $this->site->id,
|
||||||
'deployment_script_id' => $this->site->deploymentScript->id,
|
'deployment_script_id' => $this->site->deploymentScript->id,
|
||||||
'status' => DeploymentStatus::FINISHED,
|
'status' => DeploymentStatus::FINISHED,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$deployment = $this->site->deployments()->first();
|
||||||
|
$this->assertEquals('saeed', $deployment->commit_data['name']);
|
||||||
|
$this->assertEquals('saeed@vitodeploy.com', $deployment->commit_data['email']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_git_hook_deployment_invalid_secret(): void
|
public function test_git_hook_deployment_invalid_secret(): void
|
||||||
@ -271,4 +287,146 @@ public function test_git_hook_deployment_invalid_secret(): void
|
|||||||
'status' => DeploymentStatus::FINISHED,
|
'status' => DeploymentStatus::FINISHED,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function hookData(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
'github',
|
||||||
|
[
|
||||||
|
'ref' => 'refs/heads/main',
|
||||||
|
],
|
||||||
|
'github.com/*',
|
||||||
|
[
|
||||||
|
'sha' => '123',
|
||||||
|
'commit' => [
|
||||||
|
'committer' => [
|
||||||
|
'name' => 'saeed',
|
||||||
|
'email' => 'saeed@vitodeploy.com',
|
||||||
|
],
|
||||||
|
'message' => 'test commit message',
|
||||||
|
'url' => 'https://github.com',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'github',
|
||||||
|
[
|
||||||
|
'ref' => 'refs/heads/other-branch',
|
||||||
|
],
|
||||||
|
'github.com/*',
|
||||||
|
[
|
||||||
|
'sha' => '123',
|
||||||
|
'commit' => [
|
||||||
|
'committer' => [
|
||||||
|
'name' => 'saeed',
|
||||||
|
'email' => 'saeed@vitodeploy.com',
|
||||||
|
],
|
||||||
|
'message' => 'test commit message',
|
||||||
|
'url' => 'https://github.com',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'gitlab',
|
||||||
|
[
|
||||||
|
'ref' => 'main',
|
||||||
|
],
|
||||||
|
'gitlab.com/*',
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'id' => '123',
|
||||||
|
'committer_name' => 'saeed',
|
||||||
|
'committer_email' => 'saeed@vitodeploy.com',
|
||||||
|
'title' => 'test',
|
||||||
|
'web_url' => 'https://gitlab.com',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'gitlab',
|
||||||
|
[
|
||||||
|
'ref' => 'other-branch',
|
||||||
|
],
|
||||||
|
'gitlab.com/*',
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'id' => '123',
|
||||||
|
'committer_name' => 'saeed',
|
||||||
|
'committer_email' => 'saeed@vitodeploy.com',
|
||||||
|
'title' => 'test',
|
||||||
|
'web_url' => 'https://gitlab.com',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'bitbucket',
|
||||||
|
[
|
||||||
|
'push' => [
|
||||||
|
'changes' => [
|
||||||
|
[
|
||||||
|
'new' => [
|
||||||
|
'name' => 'main',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'bitbucket.org/*',
|
||||||
|
[
|
||||||
|
'values' => [
|
||||||
|
[
|
||||||
|
'hash' => '123',
|
||||||
|
'author' => [
|
||||||
|
'raw' => 'saeed <saeed@vitodeploy.com>',
|
||||||
|
],
|
||||||
|
'message' => 'test',
|
||||||
|
'links' => [
|
||||||
|
'html' => [
|
||||||
|
'href' => 'https://bitbucket.org',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
false,
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'bitbucket',
|
||||||
|
[
|
||||||
|
'push' => [
|
||||||
|
'changes' => [
|
||||||
|
[
|
||||||
|
'new' => [
|
||||||
|
'name' => 'other-branch',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
'bitbucket.org/*',
|
||||||
|
[
|
||||||
|
'values' => [
|
||||||
|
[
|
||||||
|
'hash' => '123',
|
||||||
|
'author' => [
|
||||||
|
'raw' => 'saeed <saeed@vitodeploy.com>',
|
||||||
|
],
|
||||||
|
'message' => 'test',
|
||||||
|
'links' => [
|
||||||
|
'html' => [
|
||||||
|
'href' => 'https://bitbucket.org',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
true,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user