From 836618eaaf8ab9af020ee2552f0592d0110babd5 Mon Sep 17 00:00:00 2001 From: mahdahar <89adham@gmail.com> Date: Thu, 23 Apr 2026 08:33:18 +0700 Subject: [PATCH] Add paginated git API and dashboard controls - Add shared pagination params and meta output for commits and pull requests API endpoints. - Switch dashboard lists to page-based loading with prev/next controls and total counters. - Add safer HTML escaping and initial empty-state placeholders in gitea dashboard. --- app/Controllers/Api/GitApi.php | 79 ++++++++----- app/Views/gitea_dashboard.php | 200 ++++++++++++++++++++++++++++----- 2 files changed, 223 insertions(+), 56 deletions(-) diff --git a/app/Controllers/Api/GitApi.php b/app/Controllers/Api/GitApi.php index 37322b4..a0d0970 100644 --- a/app/Controllers/Api/GitApi.php +++ b/app/Controllers/Api/GitApi.php @@ -108,6 +108,21 @@ class GitApi extends BaseController ], 200); } + private function getPaginationParams(): array + { + $page = (int) ($this->request->getGet('page') ?? 1); + if ($page <= 0) { + $page = 1; + } + + $perPage = (int) ($this->request->getGet('per_page') ?? $this->request->getGet('limit') ?? 25); + if ($perPage <= 0 || $perPage > 100) { + $perPage = 25; + } + + return [$page, $perPage]; + } + public function commits() { if ($response = $this->ensureLoggedIn()) { @@ -118,36 +133,37 @@ class GitApi extends BaseController $userId = $this->request->getGet('user_id'); $startDate = $this->request->getGet('start_date'); $endDate = $this->request->getGet('end_date'); - $limit = (int) ($this->request->getGet('limit') ?? 200); - if ($limit <= 0 || $limit > 1000) { - $limit = 200; - } + [$page, $perPage] = $this->getPaginationParams(); + $offset = ($page - 1) * $perPage; $db = \Config\Database::connect(); - $builder = $db->table('git_commits c') - ->select('c.id, c.sha, c.short_sha, c.message, c.author_name, c.author_email, c.committed_at, c.html_url, r.id as repository_id, r.full_name as repository_full_name, u.id as user_id, u.username as user_username') + $baseBuilder = $db->table('git_commits c') ->join('git_repositories r', 'r.id = c.repository_id', 'inner') ->join('git_users u', 'u.id = c.author_user_id', 'left'); if (!empty($repoId)) { - $builder->where('c.repository_id', (int) $repoId); + $baseBuilder->where('c.repository_id', (int) $repoId); } if (!empty($userId)) { - $builder->where('c.author_user_id', (int) $userId); + $baseBuilder->where('c.author_user_id', (int) $userId); } if (!empty($startDate)) { - $builder->where('c.committed_at >=', $startDate . ' 00:00:00'); + $baseBuilder->where('c.committed_at >=', $startDate . ' 00:00:00'); } if (!empty($endDate)) { - $builder->where('c.committed_at <=', $endDate . ' 23:59:59'); + $baseBuilder->where('c.committed_at <=', $endDate . ' 23:59:59'); } - $rows = $builder + $total = (int) (clone $baseBuilder) + ->countAllResults(); + + $rows = $baseBuilder + ->select('c.id, c.sha, c.short_sha, c.message, c.author_name, c.author_email, c.committed_at, c.html_url, r.id as repository_id, r.full_name as repository_full_name, u.id as user_id, u.username as user_username') ->orderBy('c.committed_at', 'DESC') - ->limit($limit) + ->limit($perPage, $offset) ->get() ->getResultArray(); @@ -155,6 +171,12 @@ class GitApi extends BaseController 'status' => 'success', 'message' => 'Commits fetched', 'data' => $rows, + 'meta' => [ + 'total' => $total, + 'page' => $page, + 'per_page' => $perPage, + 'total_pages' => $perPage > 0 ? (int) ceil($total / $perPage) : 0, + ], ], 200); } @@ -169,40 +191,41 @@ class GitApi extends BaseController $state = $this->request->getGet('state'); $startDate = $this->request->getGet('start_date'); $endDate = $this->request->getGet('end_date'); - $limit = (int) ($this->request->getGet('limit') ?? 200); - if ($limit <= 0 || $limit > 1000) { - $limit = 200; - } + [$page, $perPage] = $this->getPaginationParams(); + $offset = ($page - 1) * $perPage; $db = \Config\Database::connect(); - $builder = $db->table('git_pull_requests p') - ->select('p.id, p.number, p.title, p.state, p.is_draft, p.is_merged, p.created_at_gitea, p.updated_at_gitea, p.merged_at, p.closed_at, p.html_url, r.id as repository_id, r.full_name as repository_full_name, u.id as user_id, u.username as user_username') + $baseBuilder = $db->table('git_pull_requests p') ->join('git_repositories r', 'r.id = p.repository_id', 'inner') ->join('git_users u', 'u.id = p.author_user_id', 'left'); if (!empty($repoId)) { - $builder->where('p.repository_id', (int) $repoId); + $baseBuilder->where('p.repository_id', (int) $repoId); } if (!empty($userId)) { - $builder->where('p.author_user_id', (int) $userId); + $baseBuilder->where('p.author_user_id', (int) $userId); } if (!empty($state)) { - $builder->where('p.state', $state); + $baseBuilder->where('p.state', $state); } if (!empty($startDate)) { - $builder->where('p.updated_at_gitea >=', $startDate . ' 00:00:00'); + $baseBuilder->where('p.updated_at_gitea >=', $startDate . ' 00:00:00'); } if (!empty($endDate)) { - $builder->where('p.updated_at_gitea <=', $endDate . ' 23:59:59'); + $baseBuilder->where('p.updated_at_gitea <=', $endDate . ' 23:59:59'); } - $rows = $builder + $total = (int) (clone $baseBuilder) + ->countAllResults(); + + $rows = $baseBuilder + ->select('p.id, p.number, p.title, p.state, p.is_draft, p.is_merged, p.created_at_gitea, p.updated_at_gitea, p.merged_at, p.closed_at, p.html_url, r.id as repository_id, r.full_name as repository_full_name, u.id as user_id, u.username as user_username') ->orderBy('p.updated_at_gitea', 'DESC') - ->limit($limit) + ->limit($perPage, $offset) ->get() ->getResultArray(); @@ -210,6 +233,12 @@ class GitApi extends BaseController 'status' => 'success', 'message' => 'Pull requests fetched', 'data' => $rows, + 'meta' => [ + 'total' => $total, + 'page' => $page, + 'per_page' => $perPage, + 'total_pages' => $perPage > 0 ? (int) ceil($total / $perPage) : 0, + ], ], 200); } diff --git a/app/Views/gitea_dashboard.php b/app/Views/gitea_dashboard.php index ebed8cf..2f5e3a7 100644 --- a/app/Views/gitea_dashboard.php +++ b/app/Views/gitea_dashboard.php @@ -14,7 +14,6 @@ -
@@ -59,6 +58,11 @@
+
+ +
Page 0 of 0
+ +
@@ -66,7 +70,10 @@
-
Latest Pull Requests
+
+
Latest Pull Requests
+
Total Pull Requests: 0
+
@@ -81,6 +88,11 @@
+
+ +
Page 0 of 0
+ +
@@ -93,6 +105,7 @@