1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Ktomk\Pipelines\Runner\Reference; |
9
|
|
|
use Ktomk\Pipelines\Yaml\Yaml; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Bitbucket Pipelines file |
13
|
|
|
*/ |
14
|
|
|
class File |
15
|
|
|
{ |
16
|
|
|
const FILE_NAME = 'bitbucket-pipelines.yml'; |
17
|
|
|
|
18
|
|
|
const DEFAULT_IMAGE = 'atlassian/default-image:latest'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* default clone depth |
22
|
|
|
*/ |
23
|
|
|
const DEFAULT_CLONE = 50; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $array; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Pipelines |
32
|
|
|
*/ |
33
|
|
|
private $pipelines; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string|null path to file (the visual one to re-present it back to the user) |
37
|
|
|
* @see File::createFromFile() |
38
|
|
|
*/ |
39
|
|
|
private $path; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $path |
43
|
|
|
* |
44
|
|
|
* @throws ParseException |
45
|
|
|
* |
46
|
|
|
* @return File |
47
|
|
|
*/ |
48
|
12 |
|
public static function createFromFile($path) |
49
|
|
|
{ |
50
|
12 |
|
$result = Yaml::file($path); |
51
|
12 |
|
if (null === $result) { |
52
|
1 |
|
throw new ParseException(sprintf('YAML error: %s; verify the file contains valid YAML', $path)); |
53
|
|
|
} |
54
|
|
|
|
55
|
11 |
|
$file = new self($result); |
56
|
11 |
|
$file->path = $path; |
57
|
|
|
|
58
|
11 |
|
return $file; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* File constructor. |
63
|
|
|
* |
64
|
|
|
* @param array $array |
65
|
|
|
* |
66
|
|
|
* @throws ParseException |
67
|
|
|
*/ |
68
|
18 |
|
public function __construct(array $array) |
69
|
|
|
{ |
70
|
|
|
// quick validation: pipelines require |
71
|
18 |
|
if (!isset($array['pipelines']) || !is_array($array['pipelines'])) { |
72
|
1 |
|
throw new ParseException("Missing required property 'pipelines'"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// quick validation: image name |
76
|
17 |
|
Image::validate($array); |
77
|
|
|
|
78
|
15 |
|
$this->pipelines = $this->parsePipelines($array['pipelines']); |
79
|
|
|
|
80
|
15 |
|
$this->array = $array; |
81
|
15 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @throws ParseException |
85
|
|
|
* |
86
|
|
|
* @return Image |
87
|
|
|
*/ |
88
|
3 |
|
public function getImage() |
89
|
|
|
{ |
90
|
3 |
|
$imageData = isset($this->array['image']) |
91
|
1 |
|
? $this->array['image'] |
92
|
3 |
|
: self::DEFAULT_IMAGE; |
93
|
|
|
|
94
|
3 |
|
return new Image($imageData); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array|int |
99
|
|
|
*/ |
100
|
2 |
|
public function getClone() |
101
|
|
|
{ |
102
|
2 |
|
return isset($this->array['clone']) |
103
|
1 |
|
? $this->array['clone'] |
104
|
2 |
|
: self::DEFAULT_CLONE; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return null|Pipeline |
109
|
|
|
*/ |
110
|
2 |
|
public function getDefault() |
111
|
|
|
{ |
112
|
2 |
|
return $this->pipelines->getDefault(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Searches the pipeline that matches the reference |
117
|
|
|
* |
118
|
|
|
* @param Reference $reference |
119
|
|
|
* |
120
|
|
|
* @throws \UnexpectedValueException |
121
|
|
|
* @throws InvalidArgumentException |
122
|
|
|
* |
123
|
|
|
* @return null|string id if found, null otherwise |
124
|
|
|
*/ |
125
|
1 |
|
public function searchIdByReference(Reference $reference) |
126
|
|
|
{ |
127
|
1 |
|
return $this->pipelines->searchIdByReference($reference); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return Pipelines |
132
|
|
|
*/ |
133
|
1 |
|
public function getPipelines() |
134
|
|
|
{ |
135
|
1 |
|
return $this->pipelines; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $id |
140
|
|
|
* |
141
|
|
|
* @throws InvalidArgumentException |
142
|
|
|
* @throws ParseException |
143
|
|
|
* |
144
|
|
|
* @return null|Pipeline |
145
|
|
|
*/ |
146
|
1 |
|
public function getById($id) |
147
|
|
|
{ |
148
|
1 |
|
return $this->pipelines->getById($id); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return Definitions |
153
|
|
|
*/ |
154
|
1 |
|
public function getDefinitions() |
155
|
|
|
{ |
156
|
1 |
|
return new Definitions( |
157
|
1 |
|
isset($this->array['definitions']) |
158
|
1 |
|
? $this->array['definitions'] : array() |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
1 |
|
public function getArray() |
166
|
|
|
{ |
167
|
1 |
|
return $this->array; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* path to the file, null if created from array, string if created from file |
172
|
|
|
* |
173
|
|
|
* in its original form, e.g. '-' could represent from stdin |
174
|
|
|
* |
175
|
|
|
* @return string|null |
176
|
|
|
* @see File::createFromFile() |
177
|
|
|
*/ |
178
|
9 |
|
public function getPath() |
179
|
|
|
{ |
180
|
9 |
|
return $this->path; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $array |
185
|
|
|
* |
186
|
|
|
* @return Pipelines |
187
|
|
|
*/ |
188
|
15 |
|
private function parsePipelines(array $array) |
189
|
|
|
{ |
190
|
15 |
|
return new Pipelines($array, $this); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|