feat: watch paths

This commit is contained in:
Andras Bacsai
2024-03-28 15:05:12 +01:00
parent 971b17b364
commit 51d716253f
7 changed files with 91 additions and 15 deletions

View File

@@ -204,6 +204,7 @@ class Github extends Controller
}
public function normal(Request $request)
{
ray('asdf');
try {
$return_payloads = collect([]);
$id = null;
@@ -266,6 +267,10 @@ class Github extends Controller
if (Str::isMatch('/refs\/heads\/*/', $branch)) {
$branch = Str::after($branch, 'refs/heads/');
}
$added_files = data_get($payload, 'commits.*.added');
$removed_files = data_get($payload, 'commits.*.removed');
$modified_files = data_get($payload, 'commits.*.modified');
$changed_files = collect($added_files)->concat($removed_files)->concat($modified_files)->unique()->flatten();
ray('Webhook GitHub Push Event: ' . $id . ' with branch: ' . $branch);
}
if ($x_github_event === 'pull_request') {
@@ -306,19 +311,32 @@ class Github extends Controller
}
if ($x_github_event === 'push') {
if ($application->isDeployable()) {
ray('Deploying ' . $application->name . ' with branch ' . $branch);
$deployment_uuid = new Cuid2(7);
queue_application_deployment(
application: $application,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
is_webhook: true
);
$return_payloads->push([
'application' => $application->name,
'status' => 'success',
'message' => 'Deployment queued.',
]);
$watch_files_trigger = $application->watchPathCheck($changed_files);
if (!$watch_files_trigger) {
$return_payloads->push([
'application' => $application->name,
'status' => 'failed',
'message' => 'Watch paths does not have the changed files. Deployment ignored.',
'details' => [
'changed_files' => $changed_files,
'watch_paths' => $application->watch_paths,
],
]);
} else {
ray('Deploying ' . $application->name . ' with branch ' . $branch);
$deployment_uuid = new Cuid2(7);
queue_application_deployment(
application: $application,
deployment_uuid: $deployment_uuid,
force_rebuild: false,
is_webhook: true,
);
$return_payloads->push([
'application' => $application->name,
'status' => 'success',
'message' => 'Deployment queued.',
]);
}
} else {
$return_payloads->push([
'application' => $application->name,

View File

@@ -73,6 +73,7 @@ class General extends Component
'application.settings.is_static' => 'boolean|required',
'application.settings.is_raw_compose_deployment_enabled' => 'boolean|required',
'application.settings.is_build_server_enabled' => 'boolean|required',
'application.watch_paths' => 'nullable',
];
protected $validationAttributes = [
'application.name' => 'name',
@@ -108,6 +109,7 @@ class General extends Component
'application.settings.is_static' => 'Is static',
'application.settings.is_raw_compose_deployment_enabled' => 'Is raw compose deployment enabled',
'application.settings.is_build_server_enabled' => 'Is build server enabled',
'application.watch_paths' => 'Watch paths',
];
public function mount()
{

View File

@@ -6,6 +6,7 @@ use App\Enums\ApplicationDeploymentStatus;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Support\Str;
use RuntimeException;
@@ -903,4 +904,24 @@ class Application extends BaseModel
: explode(',', $this->fqdn),
);
}
public function watchPaths(): Attribute
{
return Attribute::make(
set: function ($value) {
if ($value) {
return trim($value);
}
}
);
}
public function watchPathCheck(Collection $modified_files): bool
{
$watch_paths = collect(explode("\n", $this->watch_paths));
$matches = $modified_files->filter(function ($file) use ($watch_paths) {
return $watch_paths->contains(function ($glob) use ($file) {
return fnmatch($glob, $file);
});
});
return $matches->count() > 0;
}
}