Skip to content

Commit

Permalink
Make Gitea Enhanced App (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
albinmedoc committed Jun 15, 2024
1 parent 0f493a4 commit 816e1b5
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
88 changes: 87 additions & 1 deletion Gitea/Gitea.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,92 @@

namespace App\SupportedApps\Gitea;

class Gitea extends \App\SupportedApps
class Gitea extends \App\SupportedApps implements \App\EnhancedApps // phpcs:ignore
{
public $config;

//protected $login_first = true; // Uncomment if api requests need to be authed first
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST

public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
}

private function fetchApi($path) {
$token = $this->config->apikey;
$attrs = [
"headers" => [
"Accept" => "application/json",
"Authorization" => "token " . $token
],
];
$res = parent::execute($this->url($path), $attrs);
switch ($res->getStatusCode()) {
case 200:
$data = json_decode($res->getBody());
if (gettype($data) == "array") {
return $data;
}
if (property_exists($data, "data")) {
return $data->data;
}
throw new Exception("Invalid response");
case 401:
case 403:
throw new \Exception("Invalid token");
default:
throw new \Exception("Could not connect to Gitea");
}
}

public function test()
{
try {
$this->fetchApi("api/v1/users/search");
echo "Successfully communicated with the API";
} catch (Exception $err) {
echo $err->getMessage();
}
}

public function livestats()
{
$status = "inactive";

$reposDetails = $this->fetchApi("api/v1/repos/search");
$orgsDetails = $this->fetchApi("api/v1/orgs");
$usersDetails = $this->fetchApi("api/v1/users/search");

$data = [
"Repos" => count($reposDetails),
"Orgs" => count($orgsDetails),
"Users" => count($usersDetails),
];

foreach ($this->config->availablestats as $stat) {
$newstat = new \stdClass();
$newstat->title = self::getAvailableStats()[$stat];
$newstat->value = $data[$stat];
$data["visiblestats"][] = $newstat;
}
$status = "active";
return parent::getLiveStats($status, $data);
}

public function url($endpoint)
{
$api_url = parent::normaliseurl($this->config->url) . $endpoint;

return $api_url;
}

public static function getAvailableStats()
{
return [
"Repos" => "Repos",
"Orgs" => "Orgs",
"Users" => "Users",
];
}
}
2 changes: 1 addition & 1 deletion Gitea/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"website": "https://gitea.io",
"license": "MIT License",
"description": "Gitea is a community managed fork of Gogs, lightweight code hosting solution written in Go and published under the MIT license.",
"enhanced": false,
"enhanced": true,
"tile_background": "dark",
"icon": "gitea.svg"
}
18 changes: 18 additions & 0 deletions Gitea/config.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
<div class="items">
<div class="input">
<label>{{ strtoupper(__('app.url')) }}</label>
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
</div>
<div class="input">
<label>{{ __('app.apps.apikey') }} (<a href="https://docs.gitea.com/next/development/api-usage#generating-and-listing-api-tokens" target="_blank">help?</a>)</label>
{!! Form::text('config[apikey]', isset($item) ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
</div>
<div class="input">
<label>Stats to show</label>
{!! Form::select('config[availablestats][]', App\SupportedApps\Gitea\Gitea::getAvailableStats(), isset($item) ? $item->getConfig()->availablestats ?? null : null, ['multiple' => 'multiple']) !!}
</div>
<div class="input">
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
</div>
</div>
8 changes: 8 additions & 0 deletions Gitea/livestats.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul class="livestats">
@foreach ($visiblestats as $stat)
<li>
<span class="title">{!! $stat->title !!}</span>
<strong>{!! $stat->value !!}</strong>
</li>
@endforeach
</ul>

0 comments on commit 816e1b5

Please sign in to comment.