Skip to content

Commit

Permalink
Fixes and missing Project/Egg fields (#122)
Browse files Browse the repository at this point in the history
* JS fixes for Dropzone

* Default license MIT, int cast of file size

* License Model

* License Model

* typehint + 2FA tests

* Do less double CI

* Adhere to allow_team_fixes

* Do more Project updates

* Form magic

* Needs at-least a badge to create a project
  • Loading branch information
annejan authored Jun 27, 2022
1 parent 43a8ea6 commit 58117cf
Show file tree
Hide file tree
Showing 21 changed files with 6,612 additions and 55 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Laravel PHPStan, Swagger and Pest

on: [push, pull_request]
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]

jobs:
laravel-tests:
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/node.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
name: Node.js build and lint

on: [push, pull_request]
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main, develop ]


jobs:
build:
Expand Down
47 changes: 47 additions & 0 deletions app/Console/Commands/UpdateLicenses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class UpdateLicenses extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'licenses:update';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update licenses from SPDX';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$contents = Http::get('https://github.com/spdx/license-list-data/raw/master/json/licenses.json')->body();
file_put_contents(resource_path('assets/licenses.json'), $contents);
return 0;
}
}
2 changes: 2 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Console;

use App\Console\Commands\GenerateSitemap;
use App\Console\Commands\UpdateLicenses;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -22,6 +23,7 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
GenerateSitemap::class,
UpdateLicenses::class,
];

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/TwoFAController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function generate2faSecret()
/** @var User $user */
$user = Auth::guard()->user();

if ( $user->google2fa_secret !== null) {
if ($user->google2fa_secret !== null) {
return redirect()->route('2fa')->with('error', 'User already has OTP secret.');
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/MchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public function app(string $device, string $type, string $category, string $app)
'app' => $project->slug,
'file' => $file->name
]);
$fileData->size = $file->size_of_content;
$fileData->size = (int)$file->size_of_content;

$files[] = $fileData;
}
Expand Down
62 changes: 53 additions & 9 deletions app/Http/Controllers/ProjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Models\BadgeProject;
use App\Models\Category;
use App\Models\File;
use App\Models\License;
use App\Models\Project;
use App\Models\User;
use App\Models\Version;
Expand Down Expand Up @@ -120,6 +121,7 @@ public function store(ProjectStoreRequest $request): RedirectResponse

try {
$project = $this->storeProjectInfo($request);

} catch (Exception $e) {
return redirect()->route('projects.create')->withInput()->withErrors([$e->getMessage()]);
}
Expand All @@ -145,7 +147,7 @@ public function edit(Project $project): View
* Update the specified resource in storage.
*
* @param ProjectUpdateRequest $request
* @param Project $project
* @param Project $project
*
* @return RedirectResponse
*/
Expand All @@ -156,10 +158,13 @@ public function update(ProjectUpdateRequest $request, Project $project): Redirec
try {
$project->min_firmware = $request->min_firmware;
$project->max_firmware = $request->max_firmware;
$project->allow_team_fixes = $request->has('allow_team_fixes');
$project->save();
$this->manageDependencies($project, $request);
$this->manageBadges($project, $request);
$this->manageCollaborators($project, $request);
$this->manageLicense($project, $request);
$this->manageType($project, $request);
} catch (Exception $e) {
return redirect()->route('projects.edit', ['project' => $project->slug])
->withInput()->withErrors([$e->getMessage()]);
Expand Down Expand Up @@ -197,7 +202,7 @@ public function destroy(Project $project): RedirectResponse
$name = $project->name;

try {
$project->name = 'Deleted ' . rand() . ' ' . $name;
$project->name = 'Deleted ' . mt_rand() . ' ' . $name;
$project->slug = Str::slug($project->name);
$project->save();
$project->delete();
Expand Down Expand Up @@ -226,7 +231,7 @@ public function show(Project $project): View
/**
* Notify badge.team of broken or dangerous app.
*
* @param Project $project
* @param Project $project
* @param ProjectNotificationRequest $request
*
* @return RedirectResponse
Expand All @@ -246,9 +251,9 @@ public function notify(Project $project, ProjectNotificationRequest $request): R
*
* @param Project $project
*
* @return View
* @throws AuthorizationException
*
* @return View
*/
public function renameForm(Project $project): View
{
Expand All @@ -262,11 +267,11 @@ public function renameForm(Project $project): View
* Update the specified resource in storage.
*
* @param ProjectRenameRequest $request
* @param Project $project
* @param Project $project
*
* @return RedirectResponse
* @throws AuthorizationException
*
* @return RedirectResponse
*/
public function rename(ProjectRenameRequest $request, Project $project): RedirectResponse
{
Expand Down Expand Up @@ -310,11 +315,11 @@ public function pull(Project $project): RedirectResponse
* Store a newly created resource in storage.
*
* @param ProjectStoreRequest $request
* @param Git $repo
* @param Git $repo
*
* @return RedirectResponse
* @throws Exception
*
* @return RedirectResponse
*/
public function import(ProjectStoreRequest $request, Git $repo): RedirectResponse
{
Expand Down Expand Up @@ -355,7 +360,7 @@ public function import(ProjectStoreRequest $request, Git $repo): RedirectRespons
private function storeProjectInfo(Request $request): Project
{
$project = Project::create([
'name' => $request->name,
'name' => $request->name,
'category_id' => $request->category_id,
]);

Expand All @@ -373,9 +378,48 @@ private function storeProjectInfo(Request $request): Project
$file->save();
}

$project->allow_team_fixes = $request->has('allow_team_fixes');

$this->manageLicense($project, $request);
$this->manageType($project, $request);

return $project;
}

/**
* @param Project $project
* @param Request $request
*
* @return void
*/
private function manageLicense(Project $project, Request $request): void
{
if ($request->license) {
if (!License::where('licenseId', $request->license)->exists()) {
throw new \RuntimeException('Unknown license');
}
$project->license = $request->license;
$project->save();
}
}

/**
* @param Project $project
* @param Request $request
*
* @return void
*/
private function manageType(Project $project, Request $request): void
{
if ($request->project_type) {
if (!in_array($request->project_type, $project->types, true)) {
throw new \RuntimeException('Unknown type');
}
$project->project_type = (string)$request->project_type;
$project->save();
}
}

/**
* @param Project $project
* @param Request $request
Expand Down
31 changes: 17 additions & 14 deletions app/Models/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ class Badge extends Model
{
use HasFactory;

/**
* @var array<string, string> $types
*/
public static array $types = [
'esp32' => 'Espressif ESP32 binary',
'python' => 'MicroPython egg',
'ice40' => 'Lattice iCE40 bitstream',
];

/**
* Generate a slug on save.
*/
Expand Down Expand Up @@ -94,19 +103,13 @@ public function getRouteKeyName(): string
*/
public function getTypesAttribute(): array
{
return [
[
'name' => 'Espressif ESP32 binary',
'slug' => 'esp32',
],
[
'name' => 'MicroPython egg',
'slug' => 'python',
],
[
'name' => 'Lattice iCE40 bitstream',
'slug' => 'ice40',
],
];
$types = [];
foreach (self::$types as $slug => $name) {
$types[] = [
'slug' => $slug,
'name' => $name,
];
}
return $types;
}
}
Loading

0 comments on commit 58117cf

Please sign in to comment.