Skip to content

Commit e991462

Browse files
authored
Fix auto-deployment branch (#506)
1 parent 1223ea1 commit e991462

File tree

6 files changed

+189
-13
lines changed

6 files changed

+189
-13
lines changed

app/Http/Controllers/API/GitHookController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function __invoke(Request $request)
2929
->firstOrFail();
3030

3131
foreach ($gitHook->actions as $action) {
32-
if ($action == 'deploy') {
32+
$webhookBranch = $gitHook->site->sourceControl->provider()->getWebhookBranch($request->array());
33+
if ($action == 'deploy' && $gitHook->site->branch === $webhookBranch) {
3334
try {
3435
app(Deploy::class)->run($gitHook->site);
3536
} catch (SourceControlIsNotConnected) {

app/SourceControlProviders/AbstractSourceControlProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ protected function handleResponseErrors(Response $res, string $repo): void
7070
throw new RepositoryPermissionDenied($repo);
7171
}
7272
}
73+
74+
public function getWebhookBranch(array $payload): string
75+
{
76+
return str($payload['ref'] ?? '')->after('refs/heads/')->toString();
77+
}
7378
}

app/SourceControlProviders/Bitbucket.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,9 @@ private function getAuthenticationHeaders(): array
183183
'Authorization' => 'Basic '.$basicAuth,
184184
];
185185
}
186+
187+
public function getWebhookBranch(array $payload): string
188+
{
189+
return data_get($payload, 'push.changes.0.new.name', 'default-branch');
190+
}
186191
}

app/SourceControlProviders/Gitlab.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,9 @@ public function getApiUrl(): string
176176

177177
return $host.$this->apiVersion;
178178
}
179+
180+
public function getWebhookBranch(array $payload): string
181+
{
182+
return $payload['ref'] ?? '';
183+
}
179184
}

app/SourceControlProviders/SourceControlProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ public function getLastCommit(string $repo, string $branch): ?array;
3636
* @throws FailedToDeployGitKey
3737
*/
3838
public function deployKey(string $title, string $repo, string $key): void;
39+
40+
public function getWebhookBranch(array $payload): string;
3941
}

tests/Feature/ApplicationTest.php

Lines changed: 170 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,21 @@ public function test_update_env_file(): void
206206
SSH::assertExecutedContains('APP_ENV="production"');
207207
}
208208

209-
public function test_git_hook_deployment(): void
209+
/**
210+
* @dataProvider hookData
211+
*/
212+
public function test_git_hook_deployment(string $provider, array $webhook, string $url, array $payload, bool $skip): void
210213
{
211214
SSH::fake();
212215
Http::fake([
213-
'github.com/*' => Http::response([
214-
'sha' => '123',
215-
'commit' => [
216-
'message' => 'test commit message',
217-
'name' => 'test commit name',
218-
'email' => 'user@example.com',
219-
'url' => 'https://github.com',
220-
],
221-
], 200),
216+
$url => Http::response($payload),
217+
]);
218+
219+
$this->site->update([
220+
'branch' => 'main',
221+
]);
222+
$this->site->sourceControl->update([
223+
'provider' => $provider,
222224
]);
223225

224226
GitHook::factory()->create([
@@ -233,15 +235,29 @@ public function test_git_hook_deployment(): void
233235
'content' => 'git pull',
234236
]);
235237

236-
$this->post(route('api.git-hooks'), [
238+
$this->post(route('api.git-hooks', [
237239
'secret' => 'secret',
238-
])->assertSessionDoesntHaveErrors();
240+
]), $webhook)->assertSessionDoesntHaveErrors();
241+
242+
if ($skip) {
243+
$this->assertDatabaseMissing('deployments', [
244+
'site_id' => $this->site->id,
245+
'deployment_script_id' => $this->site->deploymentScript->id,
246+
'status' => DeploymentStatus::FINISHED,
247+
]);
248+
249+
return;
250+
}
239251

240252
$this->assertDatabaseHas('deployments', [
241253
'site_id' => $this->site->id,
242254
'deployment_script_id' => $this->site->deploymentScript->id,
243255
'status' => DeploymentStatus::FINISHED,
244256
]);
257+
258+
$deployment = $this->site->deployments()->first();
259+
$this->assertEquals('saeed', $deployment->commit_data['name']);
260+
$this->assertEquals('saeed@vitodeploy.com', $deployment->commit_data['email']);
245261
}
246262

247263
public function test_git_hook_deployment_invalid_secret(): void
@@ -271,4 +287,146 @@ public function test_git_hook_deployment_invalid_secret(): void
271287
'status' => DeploymentStatus::FINISHED,
272288
]);
273289
}
290+
291+
public static function hookData(): array
292+
{
293+
return [
294+
[
295+
'github',
296+
[
297+
'ref' => 'refs/heads/main',
298+
],
299+
'github.com/*',
300+
[
301+
'sha' => '123',
302+
'commit' => [
303+
'committer' => [
304+
'name' => 'saeed',
305+
'email' => 'saeed@vitodeploy.com',
306+
],
307+
'message' => 'test commit message',
308+
'url' => 'https://github.com',
309+
],
310+
],
311+
false,
312+
],
313+
[
314+
'github',
315+
[
316+
'ref' => 'refs/heads/other-branch',
317+
],
318+
'github.com/*',
319+
[
320+
'sha' => '123',
321+
'commit' => [
322+
'committer' => [
323+
'name' => 'saeed',
324+
'email' => 'saeed@vitodeploy.com',
325+
],
326+
'message' => 'test commit message',
327+
'url' => 'https://github.com',
328+
],
329+
],
330+
true,
331+
],
332+
[
333+
'gitlab',
334+
[
335+
'ref' => 'main',
336+
],
337+
'gitlab.com/*',
338+
[
339+
[
340+
'id' => '123',
341+
'committer_name' => 'saeed',
342+
'committer_email' => 'saeed@vitodeploy.com',
343+
'title' => 'test',
344+
'web_url' => 'https://gitlab.com',
345+
],
346+
],
347+
false,
348+
],
349+
[
350+
'gitlab',
351+
[
352+
'ref' => 'other-branch',
353+
],
354+
'gitlab.com/*',
355+
[
356+
[
357+
'id' => '123',
358+
'committer_name' => 'saeed',
359+
'committer_email' => 'saeed@vitodeploy.com',
360+
'title' => 'test',
361+
'web_url' => 'https://gitlab.com',
362+
],
363+
],
364+
true,
365+
],
366+
[
367+
'bitbucket',
368+
[
369+
'push' => [
370+
'changes' => [
371+
[
372+
'new' => [
373+
'name' => 'main',
374+
],
375+
],
376+
],
377+
],
378+
],
379+
'bitbucket.org/*',
380+
[
381+
'values' => [
382+
[
383+
'hash' => '123',
384+
'author' => [
385+
'raw' => 'saeed <saeed@vitodeploy.com>',
386+
],
387+
'message' => 'test',
388+
'links' => [
389+
'html' => [
390+
'href' => 'https://bitbucket.org',
391+
],
392+
],
393+
],
394+
],
395+
],
396+
false,
397+
],
398+
[
399+
'bitbucket',
400+
[
401+
'push' => [
402+
'changes' => [
403+
[
404+
'new' => [
405+
'name' => 'other-branch',
406+
],
407+
],
408+
],
409+
],
410+
],
411+
'bitbucket.org/*',
412+
[
413+
'values' => [
414+
[
415+
'hash' => '123',
416+
'author' => [
417+
'raw' => 'saeed <saeed@vitodeploy.com>',
418+
],
419+
'message' => 'test',
420+
'links' => [
421+
'html' => [
422+
'href' => 'https://bitbucket.org',
423+
],
424+
],
425+
],
426+
],
427+
],
428+
true,
429+
],
430+
];
431+
}
274432
}

0 commit comments

Comments
 (0)