-
Notifications
You must be signed in to change notification settings - Fork 2
/
update_repository.py
116 lines (88 loc) · 3.73 KB
/
update_repository.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from __future__ import annotations
from pathlib import Path
import tempfile
import subprocess
def main() -> None:
current_repo = GitRepository(get_repository_root_directory())
with tempfile.TemporaryDirectory() as _tempdir:
tempdir = Path(_tempdir)
temp_repo = current_repo.clone_to(tempdir)
update_branch = "update-flake-inputs"
github_remote = "github-temp"
temp_repo.create_remote(
name=github_remote,
url="git@github.com:arbeitszeit/arbeitszeitapp-deployment.git",
)
temp_repo.disable_detached_head_advice()
temp_repo.fetch(github_remote)
temp_repo.checkout_branch(f"{github_remote}/development")
temp_repo.checkout_new_branch(update_branch)
flake = NixFlake(tempdir)
flake.update_inputs()
assert temp_repo.diff().has_changes
assert flake.run_checks().is_success
temp_repo.add_changes()
temp_repo.commit_changes("Update flake inputs")
temp_repo.push_branch(branch=update_branch, remote=github_remote, force=True)
def get_repository_root_directory() -> Path:
return Path(__file__).parent
class DiffResult:
def __init__(self, exit_code: int) -> None:
self.exit_code = exit_code
@property
def has_changes(self) -> bool:
return self.exit_code == 1
class FlakeCheckResult:
def __init__(self, exit_code: int) -> None:
self.exit_code = exit_code
@property
def is_success(self) -> bool:
return self.exit_code == 0
class GitRepository:
def __init__(self, directory: Path) -> None:
self._directory = directory
def checkout_new_branch(self, name: str) -> None:
subprocess.run(["git", "checkout", "-b", name], check=True, cwd=self._directory)
def create_remote(self, *, name: str, url: str) -> None:
subprocess.run(
["git", "remote", "add", name, url], check=True, cwd=self._directory
)
def push_branch(self, *, branch: str, remote: str, force: bool = False) -> None:
command = ["git", "push"] + (["-f"] if force else []) + [remote, branch]
subprocess.run(command, check=True, cwd=self._directory)
def commit_changes(self, message: str) -> None:
subprocess.run(
["git", "commit", "-m", message], check=True, cwd=self._directory
)
def add_changes(self) -> None:
subprocess.run(["git", "add", "."], cwd=self._directory)
def checkout_branch(self, name: str) -> None:
subprocess.run(["git", "checkout", name], cwd=self._directory, check=True)
def fetch(self, remote: str) -> None:
subprocess.run(["git", "fetch", remote], cwd=self._directory, check=True)
def clone_to(self, directory: Path) -> GitRepository:
subprocess.run(["git", "clone", "--", self._directory, directory], check=True)
return GitRepository(directory)
def diff(self) -> DiffResult:
result = subprocess.run(["git", "diff", "--exit-code"], cwd=self._directory)
return DiffResult(
exit_code=result.returncode,
)
def disable_detached_head_advice(self) -> None:
subprocess.run(
["git", "config", "advice.detachedHead", "false"],
cwd=self._directory,
check=True,
)
class NixFlake:
def __init__(self, directory: Path) -> None:
self._directory = directory
def update_inputs(self) -> None:
subprocess.run(["nix", "flake", "update"], cwd=self._directory, check=True)
def run_checks(self) -> FlakeCheckResult:
result = subprocess.run(
["nix", "flake", "check", "--print-build-logs"], cwd=self._directory
)
return FlakeCheckResult(exit_code=result.returncode)
if __name__ == "__main__":
main()