-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathProject.php
More file actions
348 lines (318 loc) · 10.1 KB
/
Copy pathProject.php
File metadata and controls
348 lines (318 loc) · 10.1 KB
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
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
/**
* @property int $id
* @property string $name
* @property ?string $description
* @property ?string $homeurl
* @property ?string $cvsurl
* @property ?string $bugtrackerurl
* @property ?string $bugtrackernewissueurl
* @property ?string $bugtrackertype
* @property ?string $documentationurl
* @property int $imageid
* @property int $public
* @property int $coveragethreshold
* @property ?string $testingdataurl
* @property string $nightlytime
* @property bool $emaillowcoverage
* @property bool $emailtesttimingchanged
* @property bool $emailbrokensubmission
* @property bool $emailredundantfailures
* @property ?string $cvsviewertype
* @property float $testtimestd
* @property float $testtimestdthreshold
* @property bool $showtesttime
* @property int $testtimemaxstatus
* @property int $emailmaxitems
* @property int $emailmaxchars
* @property bool $displaylabels
* @property int $autoremovetimeframe
* @property int $uploadquota Maximum sum of uploaded file sizes (in bytes)
* @property int $uploadquotagb Maximum sum of uploaded file sizes (in GiB)
* @property bool $showcoveragecode
* @property bool $authenticatesubmissions
* @property ?string $ldapfilter
* @property ?string $banner
* @property ?string $logoUrl
* @property ?string $cmakeprojectroot
*
* @method Builder<Project> forUser()
* @method Builder<Project> administeredByUser()
*
* @mixin Builder<Project>
*/
class Project extends Model
{
protected $table = 'project';
public $timestamps = false;
protected $fillable = [
'name',
'description',
'homeurl',
'cvsurl',
'bugtrackerurl',
'bugtrackernewissueurl',
'bugtrackertype',
'documentationurl',
'imageid',
'public',
'coveragethreshold',
'testingdataurl',
'nightlytime',
'emaillowcoverage',
'emailtesttimingchanged',
'emailbrokensubmission',
'emailredundantfailures',
'cvsviewertype',
'testtimestd',
'testtimestdthreshold',
'showtesttime',
'testtimemaxstatus',
'emailmaxitems',
'emailmaxchars',
'displaylabels',
'autoremovetimeframe',
'uploadquota',
'uploadquotagb',
'showcoveragecode',
'authenticatesubmissions',
'ldapfilter',
'banner',
'cmakeprojectroot',
];
protected $casts = [
'id' => 'integer',
'imageid' => 'integer',
'public' => 'integer',
'emaillowcoverage' => 'boolean',
'emailtesttimingchanged' => 'boolean',
'emailbrokensubmission' => 'boolean',
'emailredundantfailures' => 'boolean',
'showtesttime' => 'boolean',
'displaylabels' => 'boolean',
'coveragethreshold' => 'integer',
'showcoveragecode' => 'boolean',
'authenticatesubmissions' => 'boolean',
'testtimestd' => 'float',
'testtimestdthreshold' => 'float',
'testtimemaxstatus' => 'integer',
'emailmaxitems' => 'integer',
'emailmaxchars' => 'integer',
'autoremovetimeframe' => 'integer',
'uploadquota' => 'integer',
];
protected $attributes = [
'coveragethreshold' => 70,
'nightlytime' => '00:00:00',
'emaillowcoverage' => false,
'emailtesttimingchanged' => false,
'emailbrokensubmission' => true,
'emailredundantfailures' => false,
'testtimestd' => 4,
'testtimestdthreshold' => 1,
'showtesttime' => false,
'testtimemaxstatus' => 3,
'emailmaxitems' => 5,
'emailmaxchars' => 255,
'displaylabels' => true,
'autoremovetimeframe' => 90,
'uploadquota' => 10,
'showcoveragecode' => true,
'authenticatesubmissions' => false,
];
public const PROJECT_ADMIN = 2;
public const PROJECT_USER = 0;
public const ACCESS_PRIVATE = 0;
public const ACCESS_PUBLIC = 1;
public const ACCESS_PROTECTED = 2;
/**
* @return Attribute<?string,void>
*/
protected function logoUrl(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes): ?string {
if ((int) $attributes['imageid'] === 0) {
return null;
}
return url('/image/' . $attributes['imageid']);
},
);
}
/**
* @return Attribute<int,int>
*/
protected function uploadquotagb(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): int => (int) ((int) $attributes['uploadquota'] / (2 ** 30)),
set: fn (int $value): array => [
'uploadquota' => $value * (2 ** 30),
]
);
}
/**
* Get the users who have been added to this project. Note that this selects users with all roles.
*
* Note: This is *not* all of the users who have access to this project!
*
* @return BelongsToMany<User, $this>
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class, 'user2project', 'projectid', 'userid');
}
/**
* Get the users with the lowest user role.
*
* @return BelongsToMany<User, $this>
*/
public function basicUsers(): BelongsToMany
{
return $this->belongsToMany(User::class, 'user2project', 'projectid', 'userid')
->wherePivot('role', self::PROJECT_USER);
}
/**
* Get the users who have the administrator role for this project
*
* @return BelongsToMany<User, $this>
*/
public function administrators(): BelongsToMany
{
return $this->belongsToMany(User::class, 'user2project', 'projectid', 'userid')
->wherePivot('role', self::PROJECT_ADMIN);
}
/**
* Get the projects available to the specified user, or the current user if no user specified.
* Available as a query builder function: Project::forUser(?User)->...
*
* @param Builder<self> $query
*/
public function scopeForUser(Builder $query): void
{
$user = Auth::user();
if ($user === null) {
$query->where('public', self::ACCESS_PUBLIC);
} elseif (!$user->admin) {
$query->where(function ($subquery) use ($user): void {
$subquery->whereHas('users', function ($subquery2) use ($user): void {
$subquery2->where('users.id', $user->id);
})
->orWhere('public', self::ACCESS_PUBLIC)
->orWhere('public', self::ACCESS_PROTECTED);
});
}
// Else, this is an admin user, so we shouldn't apply any filters...
}
/**
* Get the projects the current user has admin access to.
*
* @param Builder<self> $query
*/
public function scopeAdministeredByUser(Builder $query): void
{
$user = Auth::user();
if ($user !== null && $user->admin) {
return;
}
$query->whereHas('administrators', function ($subquery) use ($user): void {
$subquery->where('users.id', $user?->id);
});
}
/**
* Get the subprojects as of a specified date, or the latest subprojects if no date specified.
*
* @return HasMany<SubProject, $this>
*/
public function subprojects(?Carbon $date = null): HasMany
{
if ($date === null) {
$date = Carbon::now()->setTimezone('UTC');
}
return $this->hasMany(SubProject::class, 'projectid', 'id')
->where('starttime', '<=', Carbon::now()->setTimezone('UTC'))
->where(function ($query) use ($date): void {
$query->where('endtime', '>', $date)
->orWhere('endtime', '=', Carbon::create(1980));
});
}
/**
* @return HasMany<PinnedTestMeasurement, $this>
*/
public function pinnedTestMeasurements(): HasMany
{
return $this->hasMany(PinnedTestMeasurement::class, 'projectid', 'id');
}
/**
* @return HasMany<BuildGroup, $this>
*/
public function buildgroups(): HasMany
{
return $this->hasMany(BuildGroup::class, 'projectid', 'id');
}
/**
* @return HasMany<Build, $this>
*/
public function builds(): HasMany
{
return $this->hasMany(Build::class, 'projectid');
}
/**
* @return HasManyThrough<Test, Build, $this>
*/
public function tests(): HasManyThrough
{
return $this->hasManyThrough(Test::class, Build::class, 'projectid', 'buildid');
}
/**
* @return HasOne<Build, $this>
*/
public function mostRecentBuild(): HasOne
{
return $this->hasOne(Build::class, 'projectid', 'id')
->ofMany(['submittime' => 'max'], function (Builder $query): void {
$query->onlyParents();
});
}
/**
* Queries the sites which have submitted builds to this project. A convenience method to
* get sites from all builds in aggregate form.
*
* @return BelongsToMany<Site, $this>
*/
public function sites(): BelongsToMany
{
return $this->belongsToMany(Site::class, Build::class, 'projectid', 'siteid')->distinct();
}
/**
* @return HasMany<ProjectInvitation, $this>
*/
public function invitations(): HasMany
{
return $this->hasMany(ProjectInvitation::class, 'project_id');
}
/**
* @return HasMany<SubProjectGroup, $this>
*/
public function subProjectGroups(): HasMany
{
return $this->hasMany(SubProjectGroup::class, 'projectid');
}
/**
* @return HasMany<Repository, $this>
*/
public function repositories(): HasMany
{
return $this->hasMany(Repository::class, 'projectid');
}
}