Skip to content

Commit 8a03d19

Browse files
CopilotMeldiron
andcommitted
Add Gogs VCS adapter, tests, Docker service and CI config
Co-authored-by: Meldiron <19310830+Meldiron@users.noreply.github.com> Agent-Logs-Url: https://github.com/utopia-php/vcs/sessions/275932b7-e04b-49d9-8ba8-8df4655d9373
1 parent 1ec6598 commit 8a03d19

4 files changed

Lines changed: 193 additions & 1 deletion

File tree

docker-compose.yml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ services:
88
- ./phpunit.xml:/usr/local/src/phpunit.xml
99
- gitea-data:/data:ro
1010
- forgejo-data:/forgejo-data:ro
11+
- gogs-data:/gogs-data:ro
1112
environment:
1213
- TESTS_GITHUB_PRIVATE_KEY
1314
- TESTS_GITHUB_APP_IDENTIFIER
1415
- TESTS_GITHUB_INSTALLATION_ID
1516
- TESTS_GITEA_URL=http://gitea:3000
1617
- TESTS_GITEA_REQUEST_CATCHER_URL=http://request-catcher:5000
1718
- TESTS_FORGEJO_URL=http://forgejo:3000
19+
- TESTS_GOGS_URL=http://gogs:3000
1820
depends_on:
1921
gitea:
2022
condition: service_healthy
@@ -24,6 +26,10 @@ services:
2426
condition: service_healthy
2527
forgejo-bootstrap:
2628
condition: service_completed_successfully
29+
gogs:
30+
condition: service_healthy
31+
gogs-bootstrap:
32+
condition: service_completed_successfully
2733
request-catcher:
2834
condition: service_started
2935

@@ -115,6 +121,46 @@ services:
115121
fi
116122
"
117123
124+
gogs:
125+
image: gogs/gogs:0.13
126+
environment:
127+
- GOGS_CUSTOM=/data/gogs
128+
volumes:
129+
- gogs-data:/data
130+
- ./tests/resources/gogs-custom/conf/app.ini:/data/gogs/conf/app.ini
131+
ports:
132+
- "3002:3000"
133+
healthcheck:
134+
test: ["CMD", "curl", "-f", "http://localhost:3000/"]
135+
interval: 10s
136+
timeout: 5s
137+
retries: 10
138+
start_period: 15s
139+
140+
gogs-bootstrap:
141+
image: gogs/gogs:0.13
142+
volumes:
143+
- gogs-data:/data
144+
depends_on:
145+
gogs:
146+
condition: service_healthy
147+
entrypoint: /bin/sh
148+
environment:
149+
- GOGS_ADMIN_USERNAME=${GOGS_ADMIN_USERNAME:-utopia}
150+
- GOGS_ADMIN_PASSWORD=${GOGS_ADMIN_PASSWORD:-password}
151+
- GOGS_ADMIN_EMAIL=${GOGS_ADMIN_EMAIL:-utopia@example.com}
152+
command: >
153+
-c "
154+
su git -c \"/opt/gogs/gogs admin create-user --name=$$GOGS_ADMIN_USERNAME --password=$$GOGS_ADMIN_PASSWORD --email=$$GOGS_ADMIN_EMAIL --admin\" || true &&
155+
if [ ! -f /data/gogs/token.txt ]; then
156+
apk add --no-cache curl jq &&
157+
TOKEN=$$(curl -s -X POST http://gogs:3000/api/v1/users/$$GOGS_ADMIN_USERNAME/tokens -H 'Content-Type: application/json' -d '{\"name\":\"bootstrap-token\"}' -u $$GOGS_ADMIN_USERNAME:$$GOGS_ADMIN_PASSWORD | jq -r '.sha1') &&
158+
mkdir -p /data/gogs &&
159+
echo $$TOKEN > /data/gogs/token.txt;
160+
fi
161+
"
162+
118163
volumes:
119164
gitea-data:
120-
forgejo-data:
165+
forgejo-data:
166+
gogs-data:

src/VCS/Adapter/Git/Gogs.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Utopia\VCS\Adapter\Git;
4+
5+
class Gogs extends Gitea
6+
{
7+
protected string $endpoint = 'http://gogs:3000/api/v1';
8+
9+
/**
10+
* Get Adapter Name
11+
*
12+
* @return string
13+
*/
14+
public function getName(): string
15+
{
16+
return 'gogs';
17+
}
18+
19+
protected function getHookType(): string
20+
{
21+
return 'gogs';
22+
}
23+
24+
/**
25+
* Get commit statuses
26+
*
27+
* Overrides the Gitea implementation to normalise the 'state' field
28+
* returned by Gogs into the 'status' field used by the rest of the
29+
* adapter interface (Gitea changed the JSON key from 'state' to
30+
* 'status', but Gogs still uses 'state').
31+
*
32+
* @param string $owner Owner of the repository
33+
* @param string $repositoryName Name of the repository
34+
* @param string $commitHash SHA of the commit
35+
* @return array<mixed> List of commit statuses
36+
*/
37+
public function getCommitStatuses(string $owner, string $repositoryName, string $commitHash): array
38+
{
39+
$statuses = parent::getCommitStatuses($owner, $repositoryName, $commitHash);
40+
41+
return array_map(function ($status) {
42+
if (isset($status['state']) && !isset($status['status'])) {
43+
$status['status'] = $status['state'];
44+
}
45+
return $status;
46+
}, $statuses);
47+
}
48+
}

tests/VCS/Adapter/GogsTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Utopia\Tests\Adapter;
4+
5+
use Utopia\Cache\Adapter\None;
6+
use Utopia\Cache\Cache;
7+
use Utopia\System\System;
8+
use Utopia\VCS\Adapter\Git;
9+
use Utopia\VCS\Adapter\Git\Gogs;
10+
11+
class GogsTest extends GiteaTest
12+
{
13+
protected static string $accessToken = '';
14+
15+
protected static string $owner = '';
16+
17+
protected string $webhookEventHeader = 'X-Gogs-Event';
18+
protected string $webhookSignatureHeader = 'X-Gogs-Signature';
19+
protected string $avatarDomain = 'gravatar.com';
20+
21+
protected function createVCSAdapter(): Git
22+
{
23+
return new Gogs(new Cache(new None()));
24+
}
25+
26+
public function setUp(): void
27+
{
28+
if (empty(static::$accessToken)) {
29+
$this->setupGogs();
30+
}
31+
32+
$adapter = new Gogs(new Cache(new None()));
33+
$gogsUrl = System::getEnv('TESTS_GOGS_URL', 'http://gogs:3000') ?? '';
34+
35+
$adapter->initializeVariables(
36+
installationId: '',
37+
privateKey: '',
38+
appId: '',
39+
accessToken: static::$accessToken,
40+
refreshToken: ''
41+
);
42+
$adapter->setEndpoint($gogsUrl);
43+
if (empty(static::$owner)) {
44+
$orgName = 'test-org-' . \uniqid();
45+
static::$owner = $adapter->createOrganization($orgName);
46+
}
47+
48+
$this->vcsAdapter = $adapter;
49+
}
50+
51+
protected function setupGogs(): void
52+
{
53+
$tokenFile = '/gogs-data/gogs/token.txt';
54+
55+
if (file_exists($tokenFile)) {
56+
$contents = file_get_contents($tokenFile);
57+
if ($contents !== false) {
58+
static::$accessToken = trim($contents);
59+
}
60+
}
61+
}
62+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
APP_NAME = Gogs
2+
RUN_MODE = prod
3+
RUN_USER = git
4+
5+
[server]
6+
DOMAIN = gogs
7+
HTTP_PORT = 3000
8+
ROOT_URL = http://gogs:3000/
9+
LOCAL_ROOT_URL = http://gogs:3000/
10+
DISABLE_SSH = true
11+
12+
[database]
13+
DB_TYPE = sqlite3
14+
PATH = /data/gogs.db
15+
16+
[repository]
17+
ROOT = /data/repositories
18+
19+
[security]
20+
INSTALL_LOCK = true
21+
# SECRET_KEY is intentionally hardcoded — this config is for local/CI testing only.
22+
SECRET_KEY = verySecretGogsKey1234567890
23+
24+
[service]
25+
REGISTER_EMAIL_CONFIRM = false
26+
ENABLE_NOTIFY_MAIL = false
27+
DISABLE_REGISTRATION = false
28+
ENABLE_CAPTCHA = false
29+
30+
[webhook]
31+
DELIVER_TIMEOUT = 10
32+
SKIP_TLS_VERIFY = true
33+
34+
[log]
35+
MODE = console
36+
LEVEL = Info

0 commit comments

Comments
 (0)