forked from REBELinBLUE/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject.php
442 lines (384 loc) · 11.5 KB
/
Project.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
namespace REBELinBLUE\Deployer;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
use REBELinBLUE\Deployer\Traits\BroadcastChanges;
use REBELinBLUE\Deployer\Traits\ProjectRelations;
use REBELinBLUE\Deployer\View\Presenters\ProjectPresenter;
use UnexpectedValueException;
use Version\Compare as VersionCompare;
/**
* Project model.
*/
class Project extends Model implements HasPresenter
{
use SoftDeletes, BroadcastChanges, ProjectRelations;
const FINISHED = 0;
const PENDING = 1;
const DEPLOYING = 2;
const FAILED = 3;
const NOT_DEPLOYED = 4;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'repository', 'branch', 'group_id', 'include_dev',
'builds_to_keep', 'url', 'build_url', 'allow_other_branch',
'private_key', ];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['private_key', 'created_at', 'deleted_at', 'updated_at', 'hash',
'updated_at', 'servers', 'commands', 'hash',
'group', 'servers', 'commands', 'heartbeats', 'checkUrls',
'notifications', 'deployments', 'shareFiles', 'configFiles', 'is_mirroring', ];
/**
* Additional attributes to include in the JSON representation.
*
* @var array
*/
protected $appends = ['group_name', 'webhook_url', 'repository_path', 'repository_url',
'branch_url', 'tags', 'branches', ];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'group_id' => 'integer',
'status' => 'integer',
'builds_to_keep' => 'integer',
'allow_other_branch' => 'boolean',
'include_dev' => 'boolean',
'is_mirroring' => 'boolean',
];
/**
* The fields which should be treated as Carbon instances.
*
* @var array
*/
protected $dates = ['last_run', 'last_mirrored'];
/**
* The heart beats status count.
*
* @var array
*/
protected $heartbeatStatus = [];
/**
* The check url's status count.
*
* @var array
*/
protected $checkurlStatus = [];
/**
* Determines whether the project is currently being deployed.
*
* @return bool
*/
public function isDeploying()
{
return ($this->status === self::DEPLOYING || $this->status === self::PENDING);
}
/**
* Generates a hash for use in the webhook URL.
*/
public function generateHash()
{
$this->attributes['hash'] = token(60);
}
/**
* Parses the repository URL to get the user, domain, port and path parts.
*
* @return array
*/
public function accessDetails()
{
$info = [];
if (preg_match('#^(.+)://(.+)@(.+):([0-9]*)\/?(.+)\.git$#', $this->repository, $matches)) {
$info['scheme'] = strtolower($matches[1]);
$info['user'] = $matches[2];
$info['domain'] = $matches[3];
$info['port'] = $matches[4];
$info['reference'] = $matches[5];
} elseif (preg_match('#^(.+)@(.+):([0-9]*)\/?(.+)\.git$#', $this->repository, $matches)) {
$info['scheme'] = 'git';
$info['user'] = $matches[1];
$info['domain'] = $matches[2];
$info['port'] = $matches[3];
$info['reference'] = $matches[4];
} elseif (preg_match('#^https?://#i', $this->repository)) {
$data = parse_url($this->repository);
if (!$data) {
return $info;
}
$info['scheme'] = strtolower($data['scheme']);
$info['user'] = isset($data['user']) ? $data['user'] : '';
$info['domain'] = $data['host'];
$info['port'] = isset($data['port']) ? $data['port'] : '';
$info['reference'] = substr($data['path'], 1, -4);
}
return $info;
}
/**
* Gets the repository path.
*
* @return string|false
*
* @see Project::accessDetails()
*/
public function getRepositoryPathAttribute()
{
$info = $this->accessDetails();
if (isset($info['reference'])) {
return $info['reference'];
}
return false;
}
/**
* Gets the HTTP URL to the repository.
*
* @return string|false
*
* @see Project::accessDetails()
*/
public function getRepositoryUrlAttribute()
{
$info = $this->accessDetails();
if (isset($info['domain']) && isset($info['reference'])) {
if (!isset($info['scheme']) || !starts_with($info['scheme'], 'http')) {
$info['scheme'] = 'http';
}
// Always serve github links over HTTPS
if (ends_with($info['domain'], 'github.com')) {
$info['scheme'] = 'https';
}
return $info['scheme'] . '://' . $info['domain'] . '/' . $info['reference'];
}
return false;
}
/**
* Gets the view presenter.
*
* @return string
*/
public function getPresenterClass()
{
return ProjectPresenter::class;
}
/**
* Gets the HTTP URL to the branch.
*
* @param string $alternative
*
* @return string|false
*
* @see Project::accessDetails()
*/
public function getBranchUrlAttribute($alternative = null)
{
$info = $this->accessDetails();
if (isset($info['domain']) && isset($info['reference'])) {
$path = 'tree';
if (str_contains($info['domain'], 'bitbucket')) {
$path = 'commits/branch';
}
if (!isset($info['scheme']) || !starts_with($info['scheme'], 'http')) {
$info['scheme'] = 'http';
}
// Always serve github links over HTTPS
if (ends_with($info['domain'], 'github.com')) {
$info['scheme'] = 'https';
}
$branch = (is_null($alternative) ? $this->branch : $alternative);
return $info['scheme'] . '://' . $info['domain'] . '/' . $info['reference'] . '/' . $path . '/' . $branch;
}
return false;
}
/**
* Count the missed heartbeat.
*
* @return array
*/
public function heartbeatsStatus()
{
if (empty($this->heartbeatStatus)) {
$length = count($this->heartbeats);
$missed = 0;
foreach ($this->heartbeats as $beat) {
if (!$beat->isHealthy()) {
$missed++;
}
}
$this->heartbeatStatus = ['missed' => $missed, 'length' => $length];
}
return $this->heartbeatStatus;
}
/**
* Count the application url check status.
*
* @return array
*/
public function applicationCheckUrlStatus()
{
if (empty($this->checkurlStatus)) {
$length = count($this->checkUrls);
$missed = 0;
foreach ($this->checkUrls as $link) {
if (!$link->isHealthy()) {
$missed++;
}
}
$this->checkurlStatus = ['missed' => $missed, 'length' => $length];
}
return $this->checkurlStatus;
}
/**
* Define a accessor for the group name.
*
* @return string
*/
public function getGroupNameAttribute()
{
return $this->group->name;
}
/**
* Define an accessor for the webhook URL.
*
* @return string
*/
public function getWebhookUrlAttribute()
{
return route('webhook.deploy', $this->hash);
}
/**
* Gets the list of all tags for the project.
*
* @return \Illuminate\Support\Collection
*/
public function getTagsAttribute()
{
$tags = $this->refs()
->where('is_tag', true)
->pluck('name')
->toArray();
$compare = new VersionCompare();
// Sort the tags, if compare throws an exception it isn't a value version string so just do a strnatcmp
// See #258 - Can remove the @ when dropping PHP 5 support
@usort($tags, function ($first, $second) use ($compare) {
try {
return $compare->compare($first, $second);
} catch (UnexpectedValueException $error) {
return strnatcmp($second, $first); // Move unknown versions to be bottle, swap round to move to top
}
});
return collect($tags)->reverse()->values();
}
/**
* Gets the list of all branches for the project which are not the default.
*
* @return array
*/
public function getBranchesAttribute()
{
return $this->refs()
->where('is_tag', false)
->where('name', '<>', $this->branch)
->orderBy('name')
->pluck('name');
}
/**
* Generate a friendly path for the mirror of the repository.
* Use the repository rather than the project ID, so if a single
* repo is used in multiple projects it is not duplicated.
*
* @return string
*/
public function mirrorPath()
{
return storage_path('app/mirrors/' . preg_replace('/[^_\-.\-a-zA-Z0-9\s]/u', '_', $this->repository));
}
/**
* Belongs to relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo(Group::class);
}
/**
* Has many relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servers()
{
return $this->hasMany(Server::class)
->orderBy('order', 'ASC');
}
/**
* Has many relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function heartbeats()
{
return $this->hasMany(Heartbeat::class)
->orderBy('name');
}
/**
* Has many relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function deployments()
{
return $this->hasMany(Deployment::class)
->orderBy('started_at', 'DESC');
}
/**
* Has many relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function channels()
{
return $this->hasMany(Channel::class)
->orderBy('name');
}
/**
* Has many urls to check.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function checkUrls()
{
return $this->hasMany(CheckUrl::class);
}
/**
* Has many relationship for git references.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*
* @see Project::getTagsAttribute()
* @see Project::getBranchesAttribute()
*/
public function refs()
{
return $this->hasMany(Ref::class);
}
/**
* Has many relationship for users.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany(User::class)->withPivot('role');
}
}