From f69a2199278a0e0ec9370d11bbcf5f3ed1f09d10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 12 Sep 2023 17:43:01 +0200 Subject: [PATCH] DGI fixes --- Makefile | 13 +++++---- www/commands/site_gitinfo.php | 15 ++++++----- www/internals/modules/selftest.php | 16 +++++++++-- www/internals/website.php | 43 +++++++++++++++++++++--------- www/pages/admin.php | 10 ++++--- 5 files changed, 68 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 01c074a..9da871f 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,14 @@ run: dgi: [ ! -f "DOCKER_GIT_INFO" ] || rm DOCKER_GIT_INFO - echo -n "VCSTYPE=" >> DOCKER_GIT_INFO ; { echo "git" ; } >> DOCKER_GIT_INFO - echo -n "BRANCH=" >> DOCKER_GIT_INFO ; { git rev-parse --abbrev-ref HEAD ; } >> DOCKER_GIT_INFO - echo -n "HASH=" >> DOCKER_GIT_INFO ; { git rev-parse HEAD ; } >> DOCKER_GIT_INFO - echo -n "COMMITTIME=" >> DOCKER_GIT_INFO ; { git log -1 --format=%cd --date=iso ; } >> DOCKER_GIT_INFO - echo -n "REMOTE=" >> DOCKER_GIT_INFO ; { git remote -v | awk '{print $$2}' | uniq | tr '\n' ';'; } >> DOCKER_GIT_INFO + echo -n "VCSTYPE=" >> DOCKER_GIT_INFO ; { echo -n "git" ; echo ""; } >> DOCKER_GIT_INFO + echo -n "BRANCH=" >> DOCKER_GIT_INFO ; { git rev-parse --abbrev-ref HEAD ; } >> DOCKER_GIT_INFO + echo -n "HASH=" >> DOCKER_GIT_INFO ; { git rev-parse HEAD ; } >> DOCKER_GIT_INFO + echo -n "COMMITTIME=" >> DOCKER_GIT_INFO ; { git log -1 --format=%cd --date=iso ; } >> DOCKER_GIT_INFO + echo -n "REMOTE=" >> DOCKER_GIT_INFO ; { git remote -v | awk '{print $$2}' | uniq | tr '\n' ';'; echo ""; } >> DOCKER_GIT_INFO + echo -n "MESSAGE=" >> DOCKER_GIT_INFO ; { git log -1 --format=%B | awk '{s=s $$0 "\n"}END{sub(/\n+$$/,"",s);printf "%s",s}' | base64 --wrap=0; echo ""; } >> DOCKER_GIT_INFO + + docker: dgi docker build \ diff --git a/www/commands/site_gitinfo.php b/www/commands/site_gitinfo.php index 5087673..63b3ea3 100644 --- a/www/commands/site_gitinfo.php +++ b/www/commands/site_gitinfo.php @@ -8,14 +8,17 @@ require_once (__DIR__ . '/../internals/website.php'); if (!isset($API_OPTIONS['field'])) { $FRAME_OPTIONS->forceResult(400, "Wrong parameters."); return; } +$info = $SITE->gitStatus(); + +function printInfo($v) { if ($v === false) throw new Exception('Failed to query field'); else echo $v; } + $field = strtolower($API_OPTIONS['field']); -if ($field === 'branch') { echo exec('git rev-parse --abbrev-ref HEAD'); return; } -if ($field === 'head') { echo exec('git rev-parse HEAD'); return; } -if ($field === 'timestamp') { echo (new DateTime(exec('git log -1 --format=%cd --date=iso')))->format('Y-m-d H:i:s'); return; } -if ($field === 'origin') { echo exec('git config --get remote.origin.url'); return; } -if ($field === 'message') { echo trim(shell_exec('git log -1 --format=%B')); return; } - +if ($field === 'branch') { printInfo($info[0]); return; } +if ($field === 'head') { printInfo($info[1]); return; } +if ($field === 'timestamp') { printInfo($info[3]); return; } +if ($field === 'origin') { printInfo(str_replace(';', "\n", $info[4])); return; } +if ($field === 'message') { printInfo($info[5]); return; } $FRAME_OPTIONS->statuscode = 400; echo 'Unknown field'; diff --git a/www/internals/modules/selftest.php b/www/internals/modules/selftest.php index b141993..3b31de3 100644 --- a/www/internals/modules/selftest.php +++ b/www/internals/modules/selftest.php @@ -546,7 +546,8 @@ class SelfTest implements IWebsiteModule 'exception' => null, ]; } - else if (!$r[2]) + + if (!$r[2]) { return [ @@ -556,7 +557,18 @@ class SelfTest implements IWebsiteModule 'exception' => null, ]; } - else + + if ($r[0] === false || $r[1] === false) + { + return + [ + 'result' => self::STATUS_ERROR, + 'message' => "{$xname} failed (failed to query branch/sha)", + 'long' => $r, + 'exception' => null, + ]; + } + { return [ diff --git a/www/internals/website.php b/www/internals/website.php index 7599717..9b64391 100644 --- a/www/internals/website.php +++ b/www/internals/website.php @@ -165,33 +165,50 @@ class Website if (file_exists('/DOCKER_GIT_INFO')) { $dgi = preg_split("/\r\n|\n|\r/", file_get_contents('/DOCKER_GIT_INFO')); - $branch = ''; - $sha = ''; + $branch = false; + $sha = false; + $time = false; + $remote = false; + $message = false; foreach ($dgi as $line) { $split = explode('=', $line, 2); if (count($split) !== 2) continue; - if ($split[0] === 'BRANCH') { $branch = $split[1]; continue; } - if ($split[0] === 'HASH') { $sha = $split[1]; continue; } + if ($split[0] === 'BRANCH') { $branch = $split[1]; continue; } + if ($split[0] === 'HASH') { $sha = $split[1]; continue; } + if ($split[0] === 'COMMITTIME') { $time = $split[1]; continue; } + if ($split[0] === 'REMOTE') { $remote = $split[1]; continue; } + if ($split[0] === 'MESSAGE') { $message = base64_decode($split[1]); continue; } } - if ($branch !== '' && $sha !== '') return [$branch, $sha, true]; - return false; + if ($branch === '') $branch = false; + if ($sha === '') $sha = false; + if ($time === '') $time = false; + if ($remote === '') $remote = false; + if ($message === '') $message = false; + + return [$branch, $sha, true, $time, $remote, $message]; } else { - $status = shell_exec('git status 2>&1'); - $branch = shell_exec('git rev-parse --abbrev-ref HEAD'); - $sha = shell_exec('git rev-parse HEAD'); + $status = shell_exec('git status 2>&1'); + $branch = shell_exec('git rev-parse --abbrev-ref HEAD'); + $sha = shell_exec('git rev-parse HEAD'); + $time = shell_exec('git log -1 --format=%cd --date=iso'); + $remote = shell_exec("git remote -v | awk '{print \$2}' | uniq | tr '\\n' ';'"); + $message = shell_exec("git log -1 --format=%B | awk '{s=s \$0 \"\\n\"}END{sub(/\\n+\$/,\"\",s);printf \"%s\",s}'"); - if ($status === false || $status === null || $status === '') return false; - if ($branch === false || $branch === null || $branch === '') return false; - if ($sha === false || $sha === null || $sha === '') return false; + if ($status === false || $status === null || $status === '') $status = false; + if ($branch === false || $branch === null || $branch === '') $branch = false; + if ($sha === false || $sha === null || $sha === '') $sha = false; + if ($time === false || $time === null || $time === '') $time = false; + if ($remote === false || $remote === null || $remote === '') $remote = false; + if ($message === false || $message === null || $message === '') $message = false; $clean = (str_contains($status, 'Your branch is up to date with')) && (str_contains($status, 'nothing to commit, working tree clean')); - return [$branch, $sha, $clean]; + return [$branch, $sha, $clean, $time, $remote, $message]; } } diff --git a/www/pages/admin.php b/www/pages/admin.php index d508ffb..e09851f 100644 --- a/www/pages/admin.php +++ b/www/pages/admin.php @@ -96,9 +96,13 @@ $connected = true; try { $SITE->modules->Database(); } catch (Exception $e) { $c
Project Lawful ebook (download count)
- modules->ProjectLawful()->listDownloadCounts() as $dlc): ?> -
:
- + + modules->ProjectLawful()->listDownloadCounts() as $dlc): ?> +
:
+ + +
Database not connected.
+