Skip to content

Commit 9da4555

Browse files
MagicRBMic92
authored andcommitted
Support using SSH to fetch repositories on Gitea
Signed-off-by: magic_rb <richard@brezak.sk>
1 parent 8a59d60 commit 9da4555

File tree

7 files changed

+67
-1
lines changed

7 files changed

+67
-1
lines changed

buildbot_nix/buildbot_nix/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,8 @@ def nix_eval_config(
10601060
submodules=True,
10611061
haltOnFailure=True,
10621062
logEnviron=False,
1063+
sshPrivateKey=project.private_key_path.read_text() if project.private_key_path else None,
1064+
sshKnownHosts=project.known_hosts_path.read_text() if project.known_hosts_path else None,
10631065
),
10641066
)
10651067
drv_gcroots_dir = util.Interpolate(
@@ -1417,6 +1419,8 @@ def buildbot_effects_config(
14171419
method="clean",
14181420
submodules=True,
14191421
haltOnFailure=True,
1422+
sshPrivateKey=project.private_key_path.read_text() if project.private_key_path else None,
1423+
sshKnownHosts=project.known_hosts_path.read_text() if project.known_hosts_path else None,
14201424
),
14211425
)
14221426
secrets_list = []

buildbot_nix/buildbot_nix/gitea_projects.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ def __init__(
6161

6262
def get_project_url(self) -> str:
6363
url = urlparse(self.config.instance_url)
64-
return f"{url.scheme}://git:%(secret:{self.config.token_file})s@{url.hostname}/{self.name}"
64+
if self.config.ssh_private_key_file:
65+
return self.data.ssh_url
66+
else:
67+
return f"{url.scheme}://git:%(secret:{self.config.token_file})s@{url.hostname}/{self.name}"
6568

6669
def create_change_source(self) -> ChangeSource | None:
6770
return None
@@ -113,6 +116,15 @@ def belongs_to_org(self) -> bool:
113116
# TODO Gitea doesn't include this information
114117
return False # self.data["owner"]["type"] == "Organization"
115118

119+
@property
120+
def private_key_path(self) -> Path | None:
121+
return self.config.ssh_private_key_file
122+
123+
@property
124+
def known_hosts_path(self) -> Path | None:
125+
return self.config.ssh_known_hosts_file
126+
127+
116128

117129
class GiteaBackend(GitBackend):
118130
config: GiteaConfig

buildbot_nix/buildbot_nix/github_projects.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,14 @@ def topics(self) -> list[str]:
770770
def belongs_to_org(self) -> bool:
771771
return self.data.owner.ttype == "Organization"
772772

773+
@property
774+
def private_key_path(self) -> Path | None:
775+
return None
776+
777+
@property
778+
def known_hosts_path(self) -> Path | None:
779+
return None
780+
773781

774782
def refresh_projects(
775783
github_token: str,

buildbot_nix/buildbot_nix/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class GiteaConfig(BaseModel):
6767
oauth_id: str | None
6868
oauth_secret_file: Path | None
6969

70+
ssh_private_key_file: Path | None
71+
ssh_known_hosts_file: Path | None
72+
7073
@property
7174
def token(self) -> str:
7275
return read_secret_file(self.token_file)

buildbot_nix/buildbot_nix/projects.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC, abstractmethod
22
from typing import Any
3+
from pathlib import Path
34

45
from buildbot.changes.base import ChangeSource
56
from buildbot.config.builder import BuilderConfig
@@ -125,3 +126,13 @@ def topics(self) -> list[str]:
125126
@abstractmethod
126127
def belongs_to_org(self) -> bool:
127128
pass
129+
130+
@property
131+
@abstractmethod
132+
def private_key_path(self) -> Path | None:
133+
pass
134+
135+
@property
136+
@abstractmethod
137+
def known_hosts_path(self) -> Path | None:
138+
pass

buildbot_nix/buildbot_nix/pull_based/project.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any
22
from urllib.parse import ParseResult, urlparse
3+
from pathlib import Path
34

45
from buildbot.changes.base import ChangeSource
56
from buildbot.changes.gitpoller import GitPoller
@@ -99,3 +100,11 @@ def topics(self) -> list[str]:
99100
@property
100101
def belongs_to_org(self) -> bool:
101102
return False
103+
104+
@property
105+
def private_key_path(self) -> Path | None:
106+
return None
107+
108+
@property
109+
def known_hosts_path(self) -> Path | None:
110+
return None

nix/master.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,23 @@ in
313313
If null, all projects that the buildbot Gitea user has access to, are built.
314314
'';
315315
};
316+
317+
sshPrivateKeyFile = lib.mkOption {
318+
type = lib.types.nullOr lib.types.path;
319+
default = null;
320+
description = ''
321+
If non-null the specified SSH key will be used to fetch all configured repositories.
322+
'';
323+
};
324+
325+
sshKnownHostsFile = lib.mkOption {
326+
type = lib.types.nullOr lib.types.path;
327+
default = null;
328+
description = ''
329+
If non-null the specified known hosts file will be matched against when connecting to
330+
repositories over SSH.
331+
'';
332+
};
316333
};
317334
github = {
318335
enable = lib.mkEnableOption "Enable GitHub integration" // {
@@ -702,6 +719,8 @@ in
702719
instance_url = cfg.gitea.instanceUrl;
703720
oauth_id = cfg.gitea.oauthId;
704721
topic = cfg.gitea.topic;
722+
ssh_private_key_file = cfg.gitea.sshPrivateKeyFile;
723+
ssh_known_hosts_file = cfg.gitea.sshKnownHostsFile;
705724
};
706725
github =
707726
if !cfg.github.enable then

0 commit comments

Comments
 (0)