|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wulkanowy\BitriseRedirector\Service; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use function is_int; |
|
7
|
|
|
use function json_decode; |
|
8
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use stdClass; |
|
11
|
|
|
use Symfony\Component\Cache\Simple\FilesystemCache; |
|
12
|
|
|
|
|
13
|
|
|
class ArtifactsService |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Client |
|
17
|
|
|
*/ |
|
18
|
|
|
private $client; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var BuildsService |
|
22
|
|
|
*/ |
|
23
|
|
|
private $builds; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var FilesystemCache |
|
27
|
|
|
*/ |
|
28
|
|
|
private $cache; |
|
29
|
|
|
|
|
30
|
1 |
|
public function __construct(Client $client, BuildsService $builds) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$this->client = $client; |
|
33
|
1 |
|
$this->builds = $builds; |
|
34
|
1 |
|
$this->cache = new FilesystemCache(); |
|
|
|
|
|
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $slug |
|
39
|
|
|
* @param string $branch |
|
40
|
|
|
* |
|
41
|
|
|
* @throws RuntimeException |
|
42
|
|
|
* @throws RequestFailedException |
|
43
|
|
|
* @throws InvalidArgumentException |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getArtifactsListByBranch(string $branch, string $slug) : array |
|
48
|
|
|
{ |
|
49
|
|
|
$tag = 'artifacts.'.$slug.'.'.str_replace('/', '-', $branch); |
|
50
|
|
|
|
|
51
|
|
|
$lastBuild = $this->builds->getLastBuildInfoByBranch($branch, $slug); |
|
52
|
|
|
|
|
53
|
|
|
if ($this->cache->has($tag)) { |
|
54
|
|
|
$response = $this->cache->get($tag); |
|
55
|
|
|
} else { |
|
56
|
|
|
$response = $this->client->get('apps/'.$slug.'/builds/'.$lastBuild['slug'].'/artifacts') |
|
57
|
|
|
->getBody()->getContents(); |
|
58
|
|
|
$this->cache->set($tag, $response, 60); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$artifacts = json_decode($response)->data; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($artifacts as $key => $item) { |
|
64
|
|
|
$item->build_slug = $lastBuild['slug']; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $artifacts; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $slug |
|
72
|
|
|
* @param string $branch |
|
73
|
|
|
* @param stdClass $artifact |
|
74
|
|
|
* |
|
75
|
|
|
* @throws RuntimeException |
|
76
|
|
|
* @throws RequestFailedException |
|
77
|
|
|
* @throws InvalidArgumentException |
|
78
|
|
|
* |
|
79
|
|
|
* @return stdClass |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getArtifactInfo(string $slug, string $branch, ?stdClass $artifact): stdClass |
|
82
|
|
|
{ |
|
83
|
|
|
$buildInfo = $this->builds->getLastBuildInfoByBranch($branch, $slug); |
|
84
|
|
|
|
|
85
|
|
|
if (null === $artifact) { |
|
86
|
|
|
throw new RequestFailedException('Artifact not found.', 404); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$infoTag = 'artifact.'.str_replace('/', '-', $branch).'.'.$artifact->title.'.json'; |
|
90
|
|
|
|
|
91
|
|
|
if ($this->cache->has($infoTag)) { |
|
92
|
|
|
$response = $this->cache->get($infoTag); |
|
93
|
|
|
} else { |
|
94
|
|
|
$response = $this->client->get('apps/'.$slug.'/builds/'.$buildInfo['slug'].'/artifacts/'.$artifact->slug) |
|
95
|
|
|
->getBody()->getContents(); |
|
96
|
|
|
$this->cache->set($infoTag, $response, 60); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$info = json_decode($response)->data; |
|
100
|
|
|
|
|
101
|
|
|
$info->build_number = $buildInfo['build_number']; |
|
102
|
|
|
$info->commit_view_url = $buildInfo['commit_view_url']; |
|
103
|
|
|
$info->finished_at = $buildInfo['finished_at']; |
|
104
|
|
|
|
|
105
|
|
|
return $info; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getArtifact(array $artifacts, $key): ?stdClass |
|
109
|
|
|
{ |
|
110
|
|
|
if (is_int($key)) { |
|
111
|
|
|
return $this->getArtifactByIndex($artifacts, $key); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->getArtifactByFilename($artifacts, $key); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
public function getArtifactByFilename(array $artifacts, string $filename): ?stdClass |
|
118
|
|
|
{ |
|
119
|
1 |
|
foreach ($artifacts as $key => $item) { |
|
120
|
1 |
|
if ($filename === $item->title) { |
|
121
|
1 |
|
return $item; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
return null; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getArtifactByIndex(array $artifacts, int $index): ?stdClass |
|
129
|
|
|
{ |
|
130
|
|
|
return $artifacts[$index] ?? null; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|