|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wulkanowy\BitriseRedirector\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
10
|
|
|
use Wulkanowy\BitriseRedirector\Service\ArtifactsService; |
|
11
|
|
|
use Wulkanowy\BitriseRedirector\Service\RequestFailedException; |
|
12
|
|
|
|
|
13
|
|
|
class ArtifactsController extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $corsRule; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ArtifactsService |
|
22
|
|
|
*/ |
|
23
|
|
|
private $artifacts; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct($corsRule, ArtifactsService $artifacts) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->corsRule = $corsRule; |
|
28
|
|
|
$this->artifacts = $artifacts; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $slug |
|
33
|
|
|
* @param string $branch |
|
34
|
|
|
* |
|
35
|
|
|
* @throws RuntimeException |
|
36
|
|
|
* @throws InvalidArgumentException |
|
37
|
|
|
* |
|
38
|
|
|
* @return JsonResponse |
|
39
|
|
|
*/ |
|
40
|
|
|
public function listAction(string $slug, string $branch): JsonResponse |
|
41
|
|
|
{ |
|
42
|
|
|
try { |
|
43
|
|
|
return $this->json($this->artifacts->getArtifactsListByBranch($branch, $slug)); |
|
44
|
|
|
} catch (RequestFailedException $e) { |
|
45
|
|
|
return $this->getErrorResponse($e); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $slug |
|
51
|
|
|
* @param string $branch |
|
52
|
|
|
* @param string $filename |
|
53
|
|
|
* |
|
54
|
|
|
* @throws RuntimeException |
|
55
|
|
|
* @throws RequestFailedException |
|
56
|
|
|
* @throws InvalidArgumentException |
|
57
|
|
|
* |
|
58
|
|
|
* @return RedirectResponse |
|
59
|
|
|
*/ |
|
60
|
|
|
public function artifactAction(string $slug, string $branch, string $filename): RedirectResponse |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->getArtifact($slug, $branch, $filename); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $slug |
|
67
|
|
|
* @param string $branch |
|
68
|
|
|
* @param int $index |
|
69
|
|
|
* |
|
70
|
|
|
* @throws RequestFailedException |
|
71
|
|
|
* @throws InvalidArgumentException |
|
72
|
|
|
* |
|
73
|
|
|
* @return RedirectResponse |
|
74
|
|
|
*/ |
|
75
|
|
|
public function artifactIndexAction(string $slug, string $branch, int $index): RedirectResponse |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->getArtifact($slug, $branch, $index); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $slug |
|
82
|
|
|
* @param string $branch |
|
83
|
|
|
* @param $key |
|
84
|
|
|
* |
|
85
|
|
|
* @throws RequestFailedException |
|
86
|
|
|
* @throws InvalidArgumentException |
|
87
|
|
|
* |
|
88
|
|
|
* @return RedirectResponse |
|
89
|
|
|
*/ |
|
90
|
|
|
private function getArtifact(string $slug, string $branch, $key): RedirectResponse |
|
91
|
|
|
{ |
|
92
|
|
|
$artifacts = $this->artifacts->getArtifactsListByBranch($branch, $slug); |
|
93
|
|
|
$artifact = $this->artifacts->getArtifact($artifacts, $key); |
|
94
|
|
|
$res = $this->artifacts->getArtifactInfo($slug, $branch, $artifact); |
|
95
|
|
|
|
|
96
|
|
|
return $this->redirect($res->public_install_page_url ?: $res->expiring_download_url); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $slug |
|
101
|
|
|
* @param string $branch |
|
102
|
|
|
* @param string $filename |
|
103
|
|
|
* |
|
104
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
|
105
|
|
|
* @throws \InvalidArgumentException |
|
106
|
|
|
* @throws RuntimeException |
|
107
|
|
|
* @throws InvalidArgumentException |
|
108
|
|
|
* |
|
109
|
|
|
* @return JsonResponse |
|
110
|
|
|
*/ |
|
111
|
|
|
public function artifactInfoAction(string $slug, string $branch, string $filename): JsonResponse |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->getArtifactsInfoResponse($slug, $branch, $filename); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $slug |
|
118
|
|
|
* @param string $branch |
|
119
|
|
|
* @param int $index |
|
120
|
|
|
* |
|
121
|
|
|
* @throws InvalidArgumentException |
|
122
|
|
|
* |
|
123
|
|
|
* @return JsonResponse |
|
124
|
|
|
*/ |
|
125
|
|
|
public function artifactInfoIndexAction(string $slug, string $branch, int $index): JsonResponse |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->getArtifactsInfoResponse($slug, $branch, $index); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param string $slug |
|
132
|
|
|
* @param string $branch |
|
133
|
|
|
* @param $key |
|
134
|
|
|
* |
|
135
|
|
|
* @throws InvalidArgumentException |
|
136
|
|
|
* |
|
137
|
|
|
* @return JsonResponse |
|
138
|
|
|
*/ |
|
139
|
|
|
private function getArtifactsInfoResponse(string $slug, string $branch, $key): JsonResponse |
|
140
|
|
|
{ |
|
141
|
|
|
try { |
|
142
|
|
|
$artifacts = $this->artifacts->getArtifactsListByBranch($branch, $slug); |
|
143
|
|
|
$artifact = $this->artifacts->getArtifact($artifacts, $key); |
|
144
|
|
|
$info = $this->artifacts->getArtifactInfo($slug, $branch, $artifact); |
|
145
|
|
|
} catch (RequestFailedException $e) { |
|
146
|
|
|
return $this->getErrorResponse($e); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->json( |
|
150
|
|
|
[ |
|
151
|
|
|
'error' => null, |
|
152
|
|
|
'build_number' => $info->build_number, |
|
153
|
|
|
'build_url' => 'https://app.bitrise.io/build/'.$artifact->build_slug, |
|
154
|
|
|
'commit_view_url' => $info->commit_view_url, |
|
155
|
|
|
'expiring_download_url' => $info->expiring_download_url, |
|
156
|
|
|
'file_size_bytes' => $info->file_size_bytes, |
|
157
|
|
|
'finished_at' => $info->finished_at, |
|
158
|
|
|
'public_install_page_url' => $info->public_install_page_url, |
|
159
|
|
|
], |
|
160
|
|
|
200, |
|
161
|
|
|
[ |
|
162
|
|
|
'Access-Control-Allow-Origin' => $this->corsRule, |
|
163
|
|
|
] |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
private function getErrorResponse(RequestFailedException $e): JsonResponse |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->json([ |
|
170
|
|
|
'error' => [ |
|
171
|
|
|
'code' => '404', |
|
172
|
|
|
'message' => $e->getMessage(), |
|
173
|
|
|
], |
|
174
|
|
|
], 404); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|