Skip to content

Commit 9c8369b

Browse files
authored
Make Gogs Enhanced App (#742)
1 parent 3fed133 commit 9c8369b

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

Gogs/Gogs.php

+61-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,66 @@
22

33
namespace App\SupportedApps\Gogs;
44

5-
class Gogs extends \App\SupportedApps
5+
class Gogs extends \App\SupportedApps implements \App\EnhancedApps
66
{
7+
public $config;
8+
9+
//protected $login_first = true; // Uncomment if api requests need to be authed first
10+
//protected $method = 'POST'; // Uncomment if requests to the API should be set by POST
11+
12+
public function __construct()
13+
{
14+
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
15+
}
16+
17+
private function fetchApi($path) {
18+
$token = $this->config->apikey;
19+
$attrs = [
20+
"headers" => [
21+
"Accept" => "application/json",
22+
"Authorization" => "token " . $token
23+
],
24+
];
25+
$res = parent::execute($this->url($path), $attrs);
26+
switch ($res->getStatusCode()) {
27+
case 200:
28+
return json_decode($res->getBody());
29+
case 401:
30+
case 403:
31+
throw new \Exception("Invalid token");
32+
default:
33+
throw new \Exception("Could not connect to Gogs");
34+
}
35+
}
36+
37+
public function test()
38+
{
39+
try {
40+
$this->fetchApi("api/v1/user/repos");
41+
echo "Successfully communicated with the API";
42+
} catch (Exception $err) {
43+
echo $err->getMessage();
44+
}
45+
}
46+
47+
public function livestats()
48+
{
49+
$status = "inactive";
50+
51+
$reposDetails = $this->fetchApi("api/v1/user/repos");
52+
$orgsDetails = $this->fetchApi("api/v1/user/orgs");
53+
54+
$data = [
55+
"repositories" => count($reposDetails),
56+
"organizations" => count($orgsDetails),
57+
];
58+
return parent::getLiveStats($status, $data);
59+
}
60+
61+
public function url($endpoint)
62+
{
63+
$api_url = parent::normaliseurl($this->config->url) . $endpoint;
64+
65+
return $api_url;
66+
}
767
}

Gogs/app.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"website": "https://gogs.io",
55
"license": "MIT License",
66
"description": "A painless self-hosted Git service.",
7-
"enhanced": false,
7+
"enhanced": true,
88
"tile_background": "dark",
99
"icon": "gogs.png"
1010
}

Gogs/config.blade.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h2>{{ __('app.apps.config') }} ({{ __('app.optional') }}) @include('items.enable')</h2>
2+
<div class="items">
3+
<div class="input">
4+
<label>{{ strtoupper(__('app.url')) }}</label>
5+
{!! Form::text('config[override_url]', isset($item) ? $item->getconfig()->override_url : null, ['placeholder' => __('app.apps.override'), 'id' => 'override_url', 'class' => 'form-control']) !!}
6+
</div>
7+
<div class="input">
8+
<label>{{ __('app.apps.apikey') }} (<a href="https://www.jetbrains.com/help/youtrack/server/integrate-project-with-gogs.html#enable-youtrack-integration-gogs" target="_blank">help?</a>)</label>
9+
{!! Form::text('config[apikey]', isset($item) ? $item->getconfig()->apikey : null, ['placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item']) !!}
10+
</div>
11+
<div class="input">
12+
<button style="margin-top: 32px;" class="btn test" id="test_config">Test</button>
13+
</div>
14+
</div>

Gogs/livestats.blade.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<ul class="livestats">
2+
<li>
3+
<span class="title">Repos</span>
4+
<strong>{!! $repositories !!}</strong>
5+
</li>
6+
<li>
7+
<span class="title">Organizations</span>
8+
<strong>{!! $organizations !!}</strong>
9+
</li>
10+
</ul>

0 commit comments

Comments
 (0)