Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add php-cs check action #25

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/lint-php-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# This workflow is provided via the organization template repository
#
# https://github.com/nextcloud/.github
# https://docs.github.com/en/actions/learn-github-actions/sharing-workflows-with-your-organization

name: Lint php-cs

on:
push:
branches: [ main, test ]
paths:
- 'lib/**'
- 'templates/**'
- 'tests/**'
- 'vendor/**'
- 'vendor-bin/**'
- composer.lock
- composer.json
pull_request:
paths:
- 'lib/**'
- 'templates/**'
- 'tests/**'
- 'vendor/**'
- 'vendor-bin/**'
- composer.lock
- composer.json

permissions:
contents: read

concurrency:
group: lint-php-cs-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest

name: php-cs

steps:
- name: Checkout
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2

- name: Set up php
uses: shivammathur/setup-php@c5fc0d8281aba02c7fda07d3a70cc5371548067d # v2
with:
php-version: 8.2
coverage: none
ini-file: development
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: composer i

- name: Lint
run: composer run cs:check || ( echo 'Please run `composer run cs:fix` to format your code' && exit 1 )
3 changes: 1 addition & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
tools: phpunit
extensions: mbstring, iconv, fileinfo, intl, sqlite, pdo_sqlite, gd, zip

- name: Set up PHPUnit
Expand All @@ -71,4 +70,4 @@ jobs:

- name: PHPUnit
working-directory: apps/${{ env.APP_NAME }}
run: phpunit -c tests/phpunit.xml
run: composer run test:unit
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ js/
.vscode-upload.json
.*.sw*
node_modules
.phpunit*.cache
.php*.cache
/composer.lock

/vendor/
17 changes: 17 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
declare(strict_types=1);

require_once './vendor/autoload.php';

use Nextcloud\CodingStandard\Config;

$config = new Config();
$config
->getFinder()
->notPath('build')
->notPath('l10n')
->notPath('src')
->notPath('node_modules')
->notPath('vendor')
->in(__DIR__);
return $config;
37 changes: 27 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
{
"name": "nextcloud/assistant",
"authors": [
{
"name": "Julien Veyssier",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0"
}
"name": "nextcloud/assistant",
"authors": [
{
"name": "Julien Veyssier",
"email": "[email protected]"
},
{
"name": "Sami Finnilä",
"email": "[email protected]"
}
],
"require": {
"php": "^8.0"
},
"scripts": {
"lint": "find . -name \\*.php -not -path './vendor/*' -print0 | xargs -0 -n1 php -l",
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix",
"psalm": "psalm.phar",
"test:unit": "phpunit --config tests/phpunit.xml"
},
"require-dev": {
"nextcloud/coding-standard": "^1.1",
"psalm/phar": "^5.16",
"nextcloud/ocp": "dev-master",
"phpunit/phpunit": "^9.5"
}
}
20 changes: 0 additions & 20 deletions composer.lock

This file was deleted.

19 changes: 8 additions & 11 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@
namespace OCA\TPAssistant\AppInfo;

use OCA\TPAssistant\Listener\BeforeTemplateRenderedListener;
use OCA\TPAssistant\Listener\FreePrompt\FreePromptReferenceListener;
use OCA\TPAssistant\Listener\TaskFailedListener;
use OCA\TPAssistant\Listener\TaskSuccessfulListener;
use OCA\TPAssistant\Listener\Text2Image\Text2ImageReferenceListener;
use OCA\TPAssistant\Reference\Text2ImageReferenceProvider;
use OCA\TPAssistant\Reference\FreePromptReferenceProvider;
use OCA\TPAssistant\Listener\FreePrompt\FreePromptReferenceListener;
use OCA\TPAssistant\Listener\FreePrompt\FreePromptResultListener;
use OCA\TPAssistant\Notification\Notifier;
use OCA\TPAssistant\Listener\Text2Image\Text2ImageResultListener;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Collaboration\Reference\RenderReferenceEvent;

use OCA\TPAssistant\Notification\Notifier;
use OCA\TPAssistant\Reference\FreePromptReferenceProvider;
use OCA\TPAssistant\Reference\Text2ImageReferenceProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext;

use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\TextProcessing\Events\TaskFailedEvent as TextTaskFailedEvent;
use OCP\TextProcessing\Events\TaskSuccessfulEvent as TextTaskSuccessfulEvent;
use OCP\TextToImage\Events\TaskFailedEvent as TextToImageTaskFailedEvent;
use OCP\TextToImage\Events\TaskSuccessfulEvent as TextToImageTaskSuccessfulEvent;


class Application extends App implements IBootstrap {

public const APP_ID = 'assistant';
Expand Down Expand Up @@ -62,4 +60,3 @@ public function register(IRegistrationContext $context): void {
public function boot(IBootContext $context): void {
}
}

4 changes: 2 additions & 2 deletions lib/Controller/AssistantController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\TPAssistant\Controller;

use OCA\TPAssistant\AppInfo\Application;
use OCA\TPAssistant\Service\AssistantService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
Expand All @@ -11,9 +12,8 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\IRequest;

use OCA\TPAssistant\Service\AssistantService;
use OCP\IRequest;

class AssistantController extends Controller {

Expand Down
8 changes: 4 additions & 4 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace OCA\TPAssistant\Controller;

use OCA\TPAssistant\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\IConfig;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCP\IConfig;

use OCA\TPAssistant\AppInfo\Application;
use OCP\IRequest;
use OCP\PreConditionNotMetException;

class ConfigController extends Controller {
Expand Down
8 changes: 3 additions & 5 deletions lib/Controller/FreePromptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
namespace OCA\TPAssistant\Controller;

use Exception;
use OCA\TPAssistant\AppInfo\Application;
use OCA\TPAssistant\Service\FreePrompt\FreePromptService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;

use OCP\AppFramework\Services\IInitialState;
use OCP\IRequest;

use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;

class FreePromptController extends Controller {
public function __construct(
string $appName,
Expand Down
25 changes: 13 additions & 12 deletions lib/Controller/Text2ImageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
namespace OCA\TPAssistant\Controller;

use Exception;
use OCP\Db\Exception as DbException;
use OCA\TPAssistant\AppInfo\Application;
use OCA\TPAssistant\Service\Text2Image\Text2ImageHelperService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;
use OCP\AppFramework\Http\Attribute\BruteForceProtection;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;

use OCP\Db\Exception as DbException;
use OCP\IRequest;
use OCP\TextToImage\Exception\TaskFailureException;
use OCP\AppFramework\Http\Attribute\AnonRateLimit;

use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
class Text2ImageController extends Controller {
public function __construct(
string $appName,
Expand Down Expand Up @@ -141,8 +142,8 @@ public function setVisibilityOfImageFiles(string $imageGenId, array $fileVisStat
} catch (Exception $e) {
$response = new DataResponse(['error' => $e->getMessage()], $e->getCode());
if($e->getCode() === Http::STATUS_BAD_REQUEST | Http::STATUS_UNAUTHORIZED) {
// Throttle brute force attempts
$response->throttle(['action' => 'imageGenId']);
// Throttle brute force attempts
$response->throttle(['action' => 'imageGenId']);
}
return $response;
}
Expand All @@ -152,8 +153,8 @@ public function setVisibilityOfImageFiles(string $imageGenId, array $fileVisStat

/**
* Notify when image generation is ready
*
* Does not need bruteforce protection since we respond with success anyways
*
* Does not need bruteforce protection since we respond with success anyways
* as we don't want to keep the front-end waiting.
* However, we still use rate limiting to prevent timing attacks.
*/
Expand All @@ -170,11 +171,11 @@ public function notifyWhenReady(string $imageGenId): DataResponse {
}
/**
* Cancel image generation
*
*
* Does not need bruteforce protection since we respond with success anyways
* (In theory bruteforce may be possible by a response timing attack but the attacker
* won't gain access to the generation since its deleted during the attack.)
*
*
* @param string $imageGenId
* @return DataResponse
*/
Expand Down Expand Up @@ -205,7 +206,7 @@ public function showGenerationPage(?string $imageGenId, ?bool $forceEditMode = f
$this->initialStateService->provideInitialState('generation-page-inputs', ['image_gen_id' => $imageGenId, 'force_edit_mode' => $forceEditMode]);
} else {
$this->initialStateService->provideInitialState('generation-page-inputs', ['image_gen_id' => $imageGenId, 'force_edit_mode' => $forceEditMode]);
}
}

return new TemplateResponse(Application::APP_ID, 'imageGenerationPage');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cron/CleanupImageGenerations.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace OCA\TPAssistant\Cron;

use Exception;
use OCA\TPAssistant\Db\Text2Image\ImageGenerationMapper;
use OCA\TPAssistant\Service\Text2Image\CleanUpService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\BackgroundJob\TimedJob;
use Psr\Log\LoggerInterface;
use Exception;

class CleanupImageGenerations extends TimedJob {
public function __construct(
Expand Down
3 changes: 2 additions & 1 deletion lib/Db/FreePrompt/PromptMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;

/**
* @implements QBMapper<Prompt>
*/
Expand Down Expand Up @@ -182,7 +183,7 @@ public function cleanupUserPrompts(string $userId): void {

$lastPromptTs = [];
/** @var mixed[] $row */
while ($row = $req->fetch()) {
while ($row = $req->fetch()) {
$lastPromptTs[] = (int)$row['timestamp'];
}
$req->closeCursor();
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/BeforeTemplateRenderedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Util;
use OCP\TextProcessing\IManager as ITextProcessingManager;
use OCP\Util;

class BeforeTemplateRenderedListener implements IEventListener {

Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/FreePrompt/FreePromptReferenceListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\TextProcessing\FreePromptTaskType;
use OCP\TextProcessing\IManager;
use OCP\Util;

use Psr\Log\LoggerInterface;

/**
* @implements IEventListener<RenderReferenceEvent>
*/
Expand Down
Loading
Loading