Skip to content

Commit 2a02c9d

Browse files
authored
Merge pull request #2 from creacoon/starter
Dashboard tile
2 parents 8417817 + 2998502 commit 2a02c9d

15 files changed

+422
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at https://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/tests export-ignore
10+
/.editorconfig export-ignore
11+
/.php.cs export-ignore
12+
/.github export-ignore
13+

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
build
2+
composer.lock
3+
docs
4+
vendor
5+
coverage
6+
.phpunit.result.cache
7+
.idea
8+
.php_cs.cache
9+

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to ` creacoon/laravel-dashboard-gitlab-user-counts-tile ` will be documented in this file
4+
5+
## 1.0.0 - 202X-XX-XX
6+
7+
- initial release

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) :vendor_name
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,67 @@
11
# laravel-dashboard-gitlab-user-counts-tile
2+
3+
---
4+
5+
GitLab User Counts Tile
6+
7+
---
8+
9+
This tile displays user counts from GitLab, including assigned merge requests, review requested merge requests, and todos.
10+
11+
12+
13+
## Installation
14+
15+
You can install the tile via composer:
16+
17+
```bash
18+
composer require creacoon/laravel-dashboard-gitlab-user-counts-tile
19+
```
20+
21+
In the `dashboard` config file, you must add this configuration in the `tiles` key.
22+
23+
Sign up at your `GitLab` instance to obtain GITLAB_API_TOKEN
24+
25+
```php
26+
// in config/dashboard.php
27+
28+
return [
29+
// ...
30+
'tiles' => [
31+
'gitlab' => [
32+
'api_token' => env('GITLAB_API_TOKEN'),
33+
'api_url' => env('GITLAB_API_URL', 'https://gitlab.com'),
34+
'specific_users' => explode(',', env('GITLAB_SPECIFIC_USERS')),
35+
],
36+
],
37+
];
38+
```
39+
40+
In app\Console\Kernel.php you should schedule the Creacoon\GitLabTile\FetchDataFromGitLabUserCountsCommand to run at your desired interval.
41+
42+
```php
43+
// in app/console/Kernel.php
44+
45+
protected function schedule(Schedule $schedule)
46+
{
47+
// ...
48+
$schedule->command(\Creacoon\GitLabTile\FetchDataFromGitLabUserCountsCommand::class)->everyMinute();
49+
}
50+
```
51+
52+
## Usage
53+
54+
In your dashboard view you use the `livewire:gitlab-user-counts-tile` component.
55+
56+
```html
57+
<x-dashboard>
58+
<livewire:gitlab-user-counts-tile position="a1" />
59+
</x-dashboard>
60+
```
61+
62+
### Customizing the view
63+
64+
If you want to customize the view used to render this tile, run this command:
65+
66+
```bash
67+
php artisan vendor:publish --provider="Creacoon\GitLabTile\GitLabUserCountsTileServiceProvider" --tag="dashboard-gitlab-user-counts-tile-views"```

composer.json

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "creacoon/laravel-dashboard-gitlab-user-counts-tile",
3+
"description": "GitLab Tile",
4+
"keywords": [
5+
"Laravel",
6+
"dashboard"
7+
],
8+
"homepage": "https://github.com/creacoon/laravel-dashboard-gitlab-user-counts-tile.git",
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Juliana Chiabai",
13+
"email": ":julianachiabai@gmail.com",
14+
"homepage": "https://gl.creacoon.nl",
15+
"role": "Developer"
16+
}
17+
],
18+
"require": {
19+
"php": "^8.0|^7.4",
20+
"spatie/laravel-dashboard": "^2.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^9.3",
24+
"squizlabs/php_codesniffer": "^3.6.2"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Creacoon\\GitLabTile\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Creacoon\\GitLabTile\\Tests\\": "tests"
34+
}
35+
},
36+
"scripts": {
37+
"test": "vendor/bin/phpunit",
38+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
39+
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
40+
},
41+
"config": {
42+
"sort-packages": true
43+
},
44+
"extra": {
45+
"laravel": {
46+
"providers": [
47+
"Creacoon\\GitLabTile\\GitLabUserCountsTileServiceProvider"
48+
]
49+
}
50+
},
51+
"minimum-stability": "dev",
52+
"prefer-stable": true
53+
}

phpcs.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="spatie">
3+
4+
<file>src</file>
5+
<file>tests</file>
6+
7+
<rule ref="PSR1" />
8+
<rule ref="PSR2" />
9+
<rule ref="PSR12" />
10+
11+
<rule ref="Generic.Files.LineLength">
12+
<severity>0</severity>
13+
</rule>
14+
15+
</ruleset>

phpunit.xml.dist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="junit" target="build/report.junit.xml"/>
24+
<log type="coverage-html" target="build/coverage"/>
25+
<log type="coverage-text" target="build/coverage.txt"/>
26+
<log type="coverage-clover" target="build/logs/clover.xml"/>
27+
</logging>
28+
</phpunit>

resources/views/tile.blade.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<x-dashboard-tile :position="$position" :refresh-interval="$refreshIntervalInSeconds">
2+
<div class="p-4 h-full flex flex-col">
3+
<div class="flex items-center justify-center mb-4">
4+
<div class="font-medium text-dimmed text-sm uppercase tracking-wide tabular-nums">
5+
GitLab User Counts
6+
</div>
7+
</div>
8+
<div wire:poll.{{ $refreshIntervalInSeconds }}s class="flex-grow">
9+
<table class="w-full">
10+
<thead>
11+
<tr>
12+
@foreach(['', 'To-dos', 'MRs', 'Reviews'] as $header)
13+
<th scope="col" class="px-2 py-2 text-left text-base font-medium text-gray-100 uppercase tracking-wider">{{ $header }}</th>
14+
@endforeach
15+
</tr>
16+
</thead>
17+
<tbody>
18+
@foreach($userCounts as $user => $counts)
19+
<tr class="bg-transparent">
20+
<td class="px-2 py-2 whitespace-nowrap">
21+
<div class="flex items-center">
22+
<div class="w-6 h-6 rounded-full bg-gray-200 flex items-center justify-center mr-2 overflow-hidden">
23+
@if(isset($counts['avatar_url']))
24+
<img src="{{ $counts['avatar_url'] }}" alt="{{ $counts['name'] ?? strtoupper(substr($user, 0, 1)) }}" class="w-full h-full object-cover">
25+
@else
26+
<span class="text-gray-500 text-sm">{{ strtoupper(substr($counts['name'] ?? $user, 0, 1)) }}</span>
27+
@endif
28+
</div>
29+
<div class="font-medium text-gray-200 text-base">{{ $counts['name'] ?? strtoupper(substr($user, 0, 1)) }}</div>
30+
</div>
31+
</td>
32+
<td class="text-gray-200 px-2 py-2 whitespace-nowrap text-base">{{ $counts['todos'] }}</td>
33+
<td class="text-gray-200 px-2 py-2 whitespace-nowrap text-base">{{ $counts['assigned_merge_requests'] }}</td>
34+
<td class="text-gray-200 px-2 py-2 whitespace-nowrap text-base">{{ $counts['review_requested_merge_requests'] }}</td>
35+
</tr>
36+
@endforeach
37+
</tbody>
38+
</table>
39+
</div>
40+
</div>
41+
</x-dashboard-tile>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Creacoon\GitLabTile;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Http;
7+
8+
class FetchDataFromGitLabUserCountsCommand extends Command
9+
{
10+
protected $signature = 'dashboard:fetch-data-from-gitlab-api';
11+
protected $description = 'Fetch data for GitLab tile';
12+
13+
public function handle()
14+
{
15+
$tile_data = [];
16+
17+
foreach (config('dashboard.tiles.gitlab.specific_users') as $username) {
18+
$userResponse = Http::withHeaders([
19+
'PRIVATE-TOKEN' => config('dashboard.tiles.gitlab.api_token'),
20+
])->get(config('dashboard.tiles.gitlab.api_url') . "/api/v4/users?username={$username}");
21+
22+
if ($userResponse->successful()) {
23+
$userData = $userResponse->json()[0] ?? null;
24+
if ($userData) {
25+
$userProfile = [
26+
'avatar_url' => $userData['avatar_url'] ?? null,
27+
'name' => preg_filter('/[^A-Z]/', '', $userData['name']),
28+
'assigned_merge_requests' => 0,
29+
'review_requested_merge_requests' => 0,
30+
'todos' => 0,
31+
];
32+
33+
$userCountResponse = Http::withHeaders([
34+
'PRIVATE-TOKEN' => config('dashboard.tiles.gitlab.api_token'),
35+
'Sudo' => $username
36+
])->get(config('dashboard.tiles.gitlab.api_url') . "/api/v4/user_counts");
37+
38+
if ($userCountResponse->successful()) {
39+
$userCountData = $userCountResponse->json();
40+
$userProfile['assigned_merge_requests'] = $userCountData['assigned_merge_requests'] ?? 0;
41+
$userProfile['review_requested_merge_requests'] = $userCountData['review_requested_merge_requests'] ?? 0;
42+
$userProfile['todos'] = $userCountData['todos'] ?? 0;
43+
44+
$tile_data[$username] = $userProfile;
45+
} else {
46+
$this->error('Failed to fetch user count for: ' . $username . '. Status: ' . $userCountResponse->status() . ', Body: ' . $userCountResponse->body());
47+
}
48+
}
49+
} else {
50+
$this->error('Failed to fetch user data for: ' . $username . '. Status: ' . $userResponse->status() . ', Body: ' . $userResponse->body());
51+
}
52+
}
53+
GitLabUserCountsStore::make()->setData($tile_data);
54+
55+
$this->info('Data fetched successfully!');
56+
}
57+
}

src/GitLabUserCountsStore.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Creacoon\GitLabTile;
4+
5+
use Spatie\Dashboard\Models\Tile;
6+
7+
class GitLabUserCountsStore
8+
{
9+
private Tile $tile;
10+
11+
public static function make()
12+
{
13+
return new static();
14+
}
15+
16+
public function __construct()
17+
{
18+
$this->tile = Tile::firstOrCreateForName("GitLabTile");
19+
}
20+
21+
public function setData(array $data): self
22+
{
23+
$this->tile->putData('GitLabUserCounts', $data);
24+
return $this;
25+
}
26+
27+
public function getData(): array
28+
{
29+
return $this->tile->getData('GitLabUserCounts') ?? [];
30+
}
31+
}

src/GitLabUserCountsTileComponent.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Creacoon\GitLabTile;
4+
5+
use Livewire\Component;
6+
7+
class GitLabUserCountsTileComponent extends Component
8+
{
9+
public $position;
10+
11+
public function mount(string $position)
12+
{
13+
$this->position = $position;
14+
}
15+
16+
public function render()
17+
{
18+
return view('dashboard-gitlab-tile::tile', [
19+
'refreshIntervalInSeconds' => config('dashboard.tiles.gitlab.refresh_interval_in_seconds') ?? 60,
20+
'userCounts' => GitLabUserCountsStore::make()->getData(),
21+
]);
22+
}
23+
}

0 commit comments

Comments
 (0)