Fix auto-deployment branch (#506)

This commit is contained in:
Saeed Vaziry 2025-02-23 12:50:46 +01:00 committed by GitHub
parent 1223ea1499
commit e99146209e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 189 additions and 13 deletions

View File

@ -29,7 +29,8 @@ public function __invoke(Request $request)
->firstOrFail();
foreach ($gitHook->actions as $action) {
if ($action == 'deploy') {
$webhookBranch = $gitHook->site->sourceControl->provider()->getWebhookBranch($request->array());
if ($action == 'deploy' && $gitHook->site->branch === $webhookBranch) {
try {
app(Deploy::class)->run($gitHook->site);
} catch (SourceControlIsNotConnected) {

View File

@ -70,4 +70,9 @@ protected function handleResponseErrors(Response $res, string $repo): void
throw new RepositoryPermissionDenied($repo);
}
}
public function getWebhookBranch(array $payload): string
{
return str($payload['ref'] ?? '')->after('refs/heads/')->toString();
}
}

View File

@ -183,4 +183,9 @@ private function getAuthenticationHeaders(): array
'Authorization' => 'Basic '.$basicAuth,
];
}
public function getWebhookBranch(array $payload): string
{
return data_get($payload, 'push.changes.0.new.name', 'default-branch');
}
}

View File

@ -176,4 +176,9 @@ public function getApiUrl(): string
return $host.$this->apiVersion;
}
public function getWebhookBranch(array $payload): string
{
return $payload['ref'] ?? '';
}
}

View File

@ -36,4 +36,6 @@ public function getLastCommit(string $repo, string $branch): ?array;
* @throws FailedToDeployGitKey
*/
public function deployKey(string $title, string $repo, string $key): void;
public function getWebhookBranch(array $payload): string;
}

View File

@ -206,19 +206,21 @@ public function test_update_env_file(): void
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();
Http::fake([
'github.com/*' => Http::response([
'sha' => '123',
'commit' => [
'message' => 'test commit message',
'name' => 'test commit name',
'email' => 'user@example.com',
'url' => 'https://github.com',
],
], 200),
$url => Http::response($payload),
]);
$this->site->update([
'branch' => 'main',
]);
$this->site->sourceControl->update([
'provider' => $provider,
]);
GitHook::factory()->create([
@ -233,15 +235,29 @@ public function test_git_hook_deployment(): void
'content' => 'git pull',
]);
$this->post(route('api.git-hooks'), [
$this->post(route('api.git-hooks', [
'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', [
'site_id' => $this->site->id,
'deployment_script_id' => $this->site->deploymentScript->id,
'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
@ -271,4 +287,146 @@ public function test_git_hook_deployment_invalid_secret(): void
'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,
],
];
}
}