2024-01-18 14:12:59 +02:00
|
|
|
{ lib, ... }:
|
|
|
|
let
|
2024-01-18 21:59:24 +02:00
|
|
|
inherit (lib)
|
|
|
|
mkIf
|
|
|
|
mkOption
|
|
|
|
types
|
|
|
|
;
|
|
|
|
|
|
|
|
boolPyLiteral = b: if b then "True" else "False";
|
2024-01-18 14:12:59 +02:00
|
|
|
|
|
|
|
testCaseExtension = { config, ... }: {
|
2024-01-18 21:59:24 +02:00
|
|
|
options = {
|
|
|
|
repo.enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "Whether to provide a repo variable - automatic repo creation.";
|
|
|
|
};
|
|
|
|
repo.private = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = "Whether the repo should be private.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = mkIf config.repo.enable {
|
|
|
|
setupScript = ''
|
|
|
|
repo = Repo("${config.name}", private=${boolPyLiteral config.repo.private})
|
|
|
|
'';
|
|
|
|
};
|
2024-01-18 14:12:59 +02:00
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
testCases = mkOption {
|
|
|
|
type = types.listOf (types.submodule testCaseExtension);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
config = {
|
|
|
|
setupScript = ''
|
2024-01-18 21:59:24 +02:00
|
|
|
def boolToJSON(b):
|
|
|
|
return "true" if b else "false"
|
|
|
|
|
2024-01-18 14:12:59 +02:00
|
|
|
class Repo:
|
|
|
|
"""
|
|
|
|
A class to create a git repository on the gitea server and locally.
|
|
|
|
"""
|
2024-01-18 21:59:24 +02:00
|
|
|
def __init__(self, name, private=False):
|
2024-01-18 14:12:59 +02:00
|
|
|
self.name = name
|
|
|
|
self.path = "/tmp/repos/" + name
|
|
|
|
self.remote = "http://gitea:3000/test/" + name
|
|
|
|
self.remote_ssh = "ssh://gitea/root/" + name
|
|
|
|
self.git = f"git -C {self.path}"
|
2024-01-18 21:59:24 +02:00
|
|
|
self.private = private
|
2024-01-18 14:12:59 +02:00
|
|
|
self.create()
|
|
|
|
|
|
|
|
def create(self):
|
|
|
|
# create ssh remote repo
|
|
|
|
gitea.succeed(f"""
|
|
|
|
git init --bare -b main /root/{self.name}
|
|
|
|
""")
|
|
|
|
# create http remote repo
|
|
|
|
gitea.succeed(f"""
|
|
|
|
curl --fail -X POST http://{gitea_admin}:{gitea_admin_password}@gitea:3000/api/v1/user/repos \
|
|
|
|
-H 'Accept: application/json' -H 'Content-Type: application/json' \
|
2024-01-18 21:59:24 +02:00
|
|
|
-d {shlex.quote( f'{{"name":"{self.name}", "default_branch": "main", "private": {boolToJSON(self.private)}}}' )}
|
2024-01-18 14:12:59 +02:00
|
|
|
""")
|
|
|
|
# setup git remotes on client
|
|
|
|
client.succeed(f"""
|
|
|
|
mkdir -p {self.path} \
|
|
|
|
&& git init -b main {self.path} \
|
|
|
|
&& {self.git} remote add origin {self.remote} \
|
|
|
|
&& {self.git} remote add origin-ssh root@gitea:{self.name}
|
|
|
|
""")
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|