Skip to content

Commit c221578

Browse files
committed
feat(build): Support delete after build
1 parent 40ceb18 commit c221578

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

app/Support/PrefixerClient.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ public function build(int $projectId, int $buildId)
8080
);
8181
}
8282

83+
public function deleteBuild(int $projectId, int $buildId)
84+
{
85+
return $this->request(
86+
'DELETE',
87+
'/projects/'.$projectId.'/builds/'.$buildId
88+
);
89+
}
90+
8391
public function download(int $projectId, int $buildId, $targetDirectory)
8492
{
8593
return $this->downloadWithSink(
@@ -139,7 +147,7 @@ private function request(string $method, $relApiUri = '', array $options = [])
139147

140148
$statusCode = $res->getStatusCode();
141149

142-
if (200 === $statusCode || 201 === $statusCode) {
150+
if (\in_array($statusCode, [200, 201, 202, 204], true)) {
143151
if (\in_array('application/json', $res->getHeader('Content-Type'), true)) {
144152
return json_decode($res->getBody());
145153
}

app/Support/Processor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function run($sourcePath, $targetPath, int $projectId, $githubAccessToken
4949
return $build;
5050
}
5151

52+
public function deleteBuild(int $projectId, int $buildId)
53+
{
54+
return $this->prefixerClient->deleteBuild($projectId, $buildId);
55+
}
56+
5257
private function temporaryZipFilename($targetPath)
5358
{
5459
return $targetPath.'/'.basename($targetPath).'.zip';

env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ PROJECT_ID="..."
1414

1515
# GitHub Access Token
1616
GITHUB_ACCESS_TOKEN="..."
17+
18+
# Delete the build after the download
19+
# DELETE_BUILD="false"

tests/Commands/PrefixCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function testPrefix()
3131
'personal-access-token' => env('PERSONAL_ACCESS_TOKEN'),
3232
'project-id' => env('PROJECT_ID'),
3333
'--github-access-token' => null,
34+
'--delete-build' => true,
3435
]
3536
)
3637
->expectsOutput('Project prefixed successfully.')
@@ -53,6 +54,7 @@ public function testCancelledPrefix()
5354
'personal-access-token' => env('PERSONAL_ACCESS_TOKEN'),
5455
'project-id' => env('PROJECT_ID'),
5556
'--github-access-token' => null,
57+
'--delete-build' => true,
5658
]
5759
)
5860
->expectsOutput('Project prefixing cancelled.')

tests/Support/PrefixerClientTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ public function testLogDownload()
105105
$this->apiClient()->download($projectId, 404, $targetDirectory);
106106
}
107107

108+
public function testDeleteBuild()
109+
{
110+
$projectId = (int) env('PROJECT_ID');
111+
$sourceDirectory = env('SOURCE_DIRECTORY');
112+
$githubAccessToken = env('GITHUB_ACCESS_TOKEN');
113+
$projectZip = realpath($sourceDirectory.'/../Source-No-Composer.zip');
114+
115+
$response = $this->apiClient()->createBuild($projectId, $projectZip, $githubAccessToken);
116+
$build = $response->build;
117+
118+
$this->assertSame('initial-state', $build->state);
119+
120+
$response = $this->apiClient()->deleteBuild($projectId, $build->id);
121+
$this->assertSame(204, $response->getStatusCode());
122+
123+
$this->expectExceptionCode(404);
124+
$this->apiClient()->build($projectId, $build->id);
125+
}
126+
108127
private function apiClient()
109128
{
110129
$personalAccessToken = env('PERSONAL_ACCESS_TOKEN');

tests/Support/ProcessorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function testRun()
3636

3737
$this->assertSame('success', $build->state);
3838
$this->cleanTargetDirectory();
39+
40+
$this->deleteBuild($processor, $projectId, $build->id);
3941
}
4042

4143
public function testCancelledRun()
@@ -55,6 +57,8 @@ public function testCancelledRun()
5557
$this->assertSame('cancelled', $build->state);
5658
$this->assertSame('Prefixer schema definition error.', $build->state_message);
5759
$this->cleanTargetDirectory();
60+
61+
$this->deleteBuild($processor, $projectId, $build->id);
5862
}
5963

6064
/**
@@ -75,4 +79,11 @@ public function testFailedRun()
7579

7680
$this->assertSame('failed', $build->state);
7781
}
82+
83+
private function deleteBuild($processor, int $projectId, int $buildId)
84+
{
85+
$response = $processor->deleteBuild($projectId, $buildId);
86+
87+
$this->assertSame(204, $response->getStatusCode());
88+
}
7889
}

0 commit comments

Comments
 (0)