Skip to content

Commit 7922cef

Browse files
committed
Add UpdateController for composer update via API
Introduces UpdateController to allow triggering 'composer update digitaldiff/diffbase' via an authenticated API endpoint. Updates README with cronjob instructions and roadmap, comments out releases slicing in ApiController, and fixes form action in index.twig.
1 parent a953723 commit 7922cef

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,25 @@ composer require "digitaldiff/diffbase" -w && php craft plugin/install diffbase
1010
- Test the API Key by going to ```yourdomain.com/api/info?key=```
1111
- Copy the API key to flow.diff.ch/admin and make a new entry with the API key and the site url
1212

13+
## Cronjob
14+
```
15+
cd /home/<CHANGEME>/public_html/<CRAFT_DIRECTORY> && composer update digitaldiff/diffbase --no-interaction --no-progress
16+
```
17+
1318
## Compatibility
14-
Only works/tested with Craft CMS 5.x
19+
Only works/tested with Craft CMS 5.x
20+
21+
## roadmap / ideas
22+
- domain check
23+
- spf check
24+
- licence infos
25+
- history (uptime)
26+
- default dashboard widgets
27+
- contact
28+
- support
29+
- urgent informations
30+
- diff.blog
31+
- start que from dashboard
32+
- empty cache from dashboard
33+
- go directly to /admin
34+
-

src/Plugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ function(RegisterTemplateRootsEvent $event) {
8181
UrlManager::class,
8282
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
8383
function (RegisterUrlRulesEvent $event) {
84-
$event->rules['api/info'] = 'diffbase/api/info'; // Maps 'api/info' to the ApiController
84+
$event->rules['api/info'] = 'diffbase/api/info'; // Bestehend
85+
// $event->rules['actions/diffbase/update/composer-update'] = 'diffbase/update/composer-update'; // Neu
8586
}
8687
);
8788

src/controllers/ApiController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ private function processCraftUpdates($cmsUpdate): array
414414
'package_name' => $cmsUpdate->packageName ?? 'craftcms/cms',
415415
'php_constraint' => $cmsUpdate->phpConstraint ?? null,
416416
'total_releases' => count($cmsUpdate->releases ?? []),
417-
'releases' => array_slice($cmsUpdate->releases ?? [], 0, 3)
417+
//'releases' => array_slice($cmsUpdate->releases ?? [], 0, 3)
418418
];
419419
}
420420

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace digitaldiff\diffbase\controllers;
4+
5+
use Craft;
6+
use craft\web\Controller;
7+
use yii\web\Response;
8+
9+
class UpdateController extends Controller
10+
{
11+
protected array|bool|int $allowAnonymous = ['composer-update'];
12+
public $enableCsrfValidation = false;
13+
14+
public function actionComposerUpdate(): Response
15+
{
16+
$plugin = Craft::$app->getPlugins()->getPlugin('diffbase');
17+
$settings = $plugin->getSettings();
18+
19+
$providedKey = Craft::$app->getRequest()->getParam('key') ??
20+
Craft::$app->getRequest()->getHeaders()->get('X-API-Key');
21+
22+
if (!$providedKey || $providedKey !== $settings->apiKey) {
23+
return $this->asJson(['error' => 'Invalid API key'], 401);
24+
}
25+
26+
set_time_limit(300);
27+
28+
$command = 'cd ' . CRAFT_BASE_PATH . ' && composer update digitaldiff/diffbase 2>&1';
29+
$output = shell_exec($command);
30+
31+
return $this->asJson([
32+
'success' => !str_contains($output, 'error'),
33+
'output' => $output,
34+
'timestamp' => date('Y-m-d H:i:s')
35+
]);
36+
}
37+
}

src/templates/index.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Notes:
4343
Kopieren
4444
</button>
4545

46-
<form method="post" action="{{ actionUrl('_diffbase/cp/generate-api-key') }}" style="display: inline;">
46+
<form method="post" action="{{ actionUrl('diffbase/cp/generate-api-key') }}" style="display: inline;">
4747
{{ csrfInput() }}
4848
{{ redirectInput('_diffbase') }}
4949
<button type="submit" class="btn submit">

0 commit comments

Comments
 (0)