2023-09-10 08:11:56 +00:00
|
|
|
import json
|
2024-04-22 13:58:38 +00:00
|
|
|
import os
|
2024-04-27 15:06:17 +00:00
|
|
|
import signal
|
2024-04-22 13:58:38 +00:00
|
|
|
from collections.abc import Generator
|
2024-04-27 15:06:17 +00:00
|
|
|
from dataclasses import dataclass
|
2023-09-10 08:11:56 +00:00
|
|
|
from pathlib import Path
|
2023-10-12 21:01:07 +00:00
|
|
|
from tempfile import NamedTemporaryFile
|
2024-04-30 15:38:50 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2024-04-22 13:58:38 +00:00
|
|
|
|
|
|
|
from buildbot.config.builder import BuilderConfig
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.plugins import util
|
2024-04-22 13:58:38 +00:00
|
|
|
from buildbot.process.buildstep import BuildStep
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.process.properties import Interpolate
|
2024-04-22 13:58:38 +00:00
|
|
|
from buildbot.reporters.base import ReporterBase
|
|
|
|
from buildbot.reporters.github import GitHubStatusPush
|
|
|
|
from buildbot.www.auth import AuthBase
|
2024-04-27 15:06:17 +00:00
|
|
|
from buildbot.www.avatar import AvatarBase, AvatarGitHub
|
2024-04-22 13:58:38 +00:00
|
|
|
from buildbot.www.oauth2 import GitHubAuth
|
2024-04-27 15:06:17 +00:00
|
|
|
from twisted.internet import defer, threads
|
|
|
|
from twisted.python import log
|
|
|
|
from twisted.python.failure import Failure
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2024-04-30 15:38:50 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from buildbot.process.log import StreamLog
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
from .common import (
|
|
|
|
http_request,
|
|
|
|
paginated_github_request,
|
|
|
|
slugify_project_name,
|
2024-04-22 13:58:38 +00:00
|
|
|
)
|
2024-04-27 15:06:17 +00:00
|
|
|
from .projects import GitBackend, GitProject
|
|
|
|
from .secrets import read_secret_file
|
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
|
|
|
|
class ReloadGithubProjects(BuildStep):
|
|
|
|
name = "reload_github_projects"
|
|
|
|
|
|
|
|
def __init__(self, token: str, project_cache_file: Path, **kwargs: Any) -> None:
|
|
|
|
self.token = token
|
|
|
|
self.project_cache_file = project_cache_file
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
|
|
|
|
def reload_projects(self) -> None:
|
|
|
|
refresh_projects(self.token, self.project_cache_file)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def run(self) -> Generator[Any, object, Any]:
|
|
|
|
d = threads.deferToThread(self.reload_projects) # type: ignore[no-untyped-call]
|
|
|
|
|
|
|
|
self.error_msg = ""
|
|
|
|
|
|
|
|
def error_cb(failure: Failure) -> int:
|
|
|
|
self.error_msg += failure.getTraceback()
|
|
|
|
return util.FAILURE
|
|
|
|
|
|
|
|
d.addCallbacks(lambda _: util.SUCCESS, error_cb)
|
|
|
|
res = yield d
|
|
|
|
if res == util.SUCCESS:
|
|
|
|
# reload the buildbot config
|
|
|
|
os.kill(os.getpid(), signal.SIGHUP)
|
|
|
|
return util.SUCCESS
|
|
|
|
else:
|
2024-04-30 15:38:50 +00:00
|
|
|
log: StreamLog = yield self.addLog("log")
|
|
|
|
log.addStderr(f"Failed to reload project list: {self.error_msg}")
|
2024-04-22 13:58:38 +00:00
|
|
|
return util.FAILURE
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
@dataclass
|
|
|
|
class GithubConfig:
|
2024-04-30 13:45:39 +00:00
|
|
|
oauth_id: str | None
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
# TODO unused
|
2024-04-22 13:58:38 +00:00
|
|
|
buildbot_user: str
|
|
|
|
oauth_secret_name: str = "github-oauth-secret"
|
|
|
|
token_secret_name: str = "github-token"
|
2024-04-27 15:06:17 +00:00
|
|
|
webhook_secret_name: str = "github-webhook-secret"
|
2024-04-22 13:58:38 +00:00
|
|
|
project_cache_file: Path = Path("github-project-cache.json")
|
|
|
|
topic: str | None = "build-with-buildbot"
|
|
|
|
|
|
|
|
def token(self) -> str:
|
|
|
|
return read_secret_file(self.token_secret_name)
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
@dataclass
|
|
|
|
class GithubBackend(GitBackend):
|
|
|
|
config: GithubConfig
|
2024-04-27 15:06:17 +00:00
|
|
|
webhook_secret: str
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
def __init__(self, config: GithubConfig) -> None:
|
2024-04-22 13:58:38 +00:00
|
|
|
self.config = config
|
2024-04-27 15:06:17 +00:00
|
|
|
self.webhook_secret = read_secret_file(self.config.webhook_secret_name)
|
2024-04-22 13:58:38 +00:00
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
def create_reload_builder(self, worker_names: list[str]) -> BuilderConfig:
|
2024-04-22 13:58:38 +00:00
|
|
|
"""Updates the flake an opens a PR for it."""
|
|
|
|
factory = util.BuildFactory()
|
|
|
|
factory.addStep(
|
2024-04-27 15:06:17 +00:00
|
|
|
ReloadGithubProjects(self.config.token(), self.config.project_cache_file),
|
2024-04-22 13:58:38 +00:00
|
|
|
)
|
|
|
|
return util.BuilderConfig(
|
|
|
|
name=self.reload_builder_name,
|
|
|
|
workernames=worker_names,
|
|
|
|
factory=factory,
|
|
|
|
)
|
|
|
|
|
|
|
|
def create_reporter(self) -> ReporterBase:
|
2024-04-27 15:06:17 +00:00
|
|
|
return GitHubStatusPush(
|
|
|
|
token=self.config.token(),
|
|
|
|
# Since we dynamically create build steps,
|
|
|
|
# we use `virtual_builder_name` in the webinterface
|
|
|
|
# so that we distinguish what has beeing build
|
|
|
|
context=Interpolate("buildbot/%(prop:status_name)s"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def create_change_hook(self) -> dict[str, Any]:
|
2024-04-22 13:58:38 +00:00
|
|
|
return {
|
2024-04-27 15:06:17 +00:00
|
|
|
"secret": self.webhook_secret,
|
2024-04-22 13:58:38 +00:00
|
|
|
"strict": True,
|
2024-04-27 15:06:17 +00:00
|
|
|
"token": self.config.token(),
|
2024-04-22 13:58:38 +00:00
|
|
|
"github_property_whitelist": "*",
|
|
|
|
}
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
def create_avatar_method(self) -> AvatarBase | None:
|
2024-04-22 13:58:38 +00:00
|
|
|
return AvatarGitHub(token=self.config.token())
|
|
|
|
|
|
|
|
def create_auth(self) -> AuthBase:
|
2024-04-30 13:45:39 +00:00
|
|
|
assert self.config.oauth_id is not None, "GitHub OAuth ID is required"
|
2024-04-27 15:06:17 +00:00
|
|
|
return GitHubAuth(
|
|
|
|
self.config.oauth_id,
|
|
|
|
read_secret_file(self.config.oauth_secret_name),
|
|
|
|
apiVersion=4,
|
|
|
|
)
|
2024-04-22 13:58:38 +00:00
|
|
|
|
|
|
|
def load_projects(self) -> list["GitProject"]:
|
|
|
|
if not self.config.project_cache_file.exists():
|
|
|
|
return []
|
|
|
|
|
|
|
|
repos: list[dict[str, Any]] = sorted(
|
2024-04-27 15:06:17 +00:00
|
|
|
json.loads(self.config.project_cache_file.read_text()),
|
|
|
|
key=lambda x: x["full_name"],
|
|
|
|
)
|
|
|
|
return list(
|
|
|
|
filter(
|
|
|
|
lambda project: self.config.topic is not None
|
|
|
|
and self.config.topic in project.topics,
|
|
|
|
[
|
|
|
|
GithubProject(self.config, self.webhook_secret, repo)
|
|
|
|
for repo in repos
|
|
|
|
],
|
|
|
|
)
|
2024-04-22 13:58:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def are_projects_cached(self) -> bool:
|
|
|
|
return self.config.project_cache_file.exists()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self) -> str:
|
|
|
|
return "github"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pretty_type(self) -> str:
|
|
|
|
return "GitHub"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def reload_builder_name(self) -> str:
|
|
|
|
return "reload-github-projects"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def change_hook_name(self) -> str:
|
|
|
|
return "github"
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
class GithubProject(GitProject):
|
|
|
|
config: GithubConfig
|
|
|
|
|
2024-04-27 15:06:17 +00:00
|
|
|
def __init__(
|
|
|
|
self, config: GithubConfig, webhook_secret: str, data: dict[str, Any]
|
|
|
|
) -> None:
|
2024-04-22 13:58:38 +00:00
|
|
|
self.config = config
|
2024-04-27 15:06:17 +00:00
|
|
|
self.webhook_secret = webhook_secret
|
2024-04-22 13:58:38 +00:00
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def create_project_hook(
|
|
|
|
self,
|
|
|
|
owner: str,
|
|
|
|
repo: str,
|
|
|
|
webhook_url: str,
|
|
|
|
) -> None:
|
|
|
|
hooks = paginated_github_request(
|
|
|
|
f"https://api.github.com/repos/{owner}/{repo}/hooks?per_page=100",
|
|
|
|
self.config.token(),
|
|
|
|
)
|
|
|
|
config = dict(
|
|
|
|
url=webhook_url + "change_hook/github",
|
|
|
|
content_type="json",
|
|
|
|
insecure_ssl="0",
|
2024-04-27 15:06:17 +00:00
|
|
|
secret=self.webhook_secret,
|
|
|
|
)
|
|
|
|
data = dict(
|
|
|
|
name="web", active=True, events=["push", "pull_request"], config=config
|
2024-04-22 13:58:38 +00:00
|
|
|
)
|
|
|
|
headers = {
|
|
|
|
"Authorization": f"Bearer {self.config.token()}",
|
|
|
|
"Accept": "application/vnd.github+json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"X-GitHub-Api-Version": "2022-11-28",
|
|
|
|
}
|
|
|
|
for hook in hooks:
|
|
|
|
if hook["config"]["url"] == webhook_url + "change_hook/github":
|
|
|
|
log.msg(f"hook for {owner}/{repo} already exists")
|
|
|
|
return
|
|
|
|
|
|
|
|
http_request(
|
|
|
|
f"https://api.github.com/repos/{owner}/{repo}/hooks",
|
|
|
|
method="POST",
|
|
|
|
headers=headers,
|
|
|
|
data=data,
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_project_url(self) -> str:
|
|
|
|
return f"https://git:%(secret:{self.config.token_secret_name})s@github.com/{self.name}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def pretty_type(self) -> str:
|
|
|
|
return "GitHub"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def type(self) -> str:
|
|
|
|
return "github"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def repo(self) -> str:
|
|
|
|
return self.data["name"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def owner(self) -> str:
|
|
|
|
return self.data["owner"]["login"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return self.data["full_name"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def url(self) -> str:
|
|
|
|
return self.data["html_url"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def project_id(self) -> str:
|
|
|
|
return slugify_project_name(self.data["full_name"])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def default_branch(self) -> str:
|
|
|
|
return self.data["default_branch"]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def topics(self) -> list[str]:
|
|
|
|
return self.data["topics"]
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2024-04-22 13:58:38 +00:00
|
|
|
@property
|
|
|
|
def belongs_to_org(self) -> bool:
|
|
|
|
return self.data["owner"]["type"] == "Organization"
|
2023-09-10 08:11:56 +00:00
|
|
|
|
2023-10-26 12:54:01 +00:00
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
def refresh_projects(github_token: str, repo_cache_file: Path) -> None:
|
2023-10-26 09:08:07 +00:00
|
|
|
repos = []
|
|
|
|
|
|
|
|
for repo in paginated_github_request(
|
2023-10-27 08:49:40 +00:00
|
|
|
"https://api.github.com/user/repos?per_page=100",
|
|
|
|
github_token,
|
2023-10-26 09:08:07 +00:00
|
|
|
):
|
|
|
|
if not repo["permissions"]["admin"]:
|
2023-10-27 08:49:40 +00:00
|
|
|
name = repo["full_name"]
|
|
|
|
log.msg(
|
2023-12-26 20:56:36 +00:00
|
|
|
f"skipping {name} because we do not have admin privileges, needed for hook management",
|
2023-10-27 08:49:40 +00:00
|
|
|
)
|
2023-10-26 09:08:07 +00:00
|
|
|
else:
|
|
|
|
repos.append(repo)
|
|
|
|
|
2023-10-12 21:01:07 +00:00
|
|
|
with NamedTemporaryFile("w", delete=False, dir=repo_cache_file.parent) as f:
|
2023-12-26 20:56:36 +00:00
|
|
|
path = Path(f.name)
|
2023-10-12 21:01:07 +00:00
|
|
|
try:
|
|
|
|
f.write(json.dumps(repos))
|
|
|
|
f.flush()
|
2023-12-26 20:56:36 +00:00
|
|
|
path.rename(repo_cache_file)
|
2023-10-12 21:01:07 +00:00
|
|
|
except OSError:
|
2023-12-26 20:56:36 +00:00
|
|
|
path.unlink()
|
2023-10-12 21:01:07 +00:00
|
|
|
raise
|