Make code testable
This commit is contained in:
45
nixprstatus/pr.py
Normal file
45
nixprstatus/pr.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import requests
|
||||
from pydantic import BaseModel
|
||||
|
||||
DEFAULT_HEADERS = {
|
||||
"Accept": "application/vnd.github.text+json",
|
||||
}
|
||||
DEFAULT_BRANCHES = ["master", "nixos-unstable-small", "nixos-unstable", "nixos-24.05"]
|
||||
|
||||
|
||||
class PRStatus(BaseModel):
|
||||
merged: bool
|
||||
branches: dict[str, bool]
|
||||
|
||||
|
||||
def commit_in_branch(commit_sha: str, branch: str) -> bool:
|
||||
url = f"https://api.github.com/repos/NixOS/nixpkgs/compare/{branch}...{commit_sha}"
|
||||
commit_response = requests.get(url, headers=DEFAULT_HEADERS)
|
||||
commit_response.raise_for_status()
|
||||
status = commit_response.json().get("status")
|
||||
|
||||
if status in ["identical", "behind"]:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def pr_merge_status(pr: int, branches: list[str] = DEFAULT_BRANCHES) -> PRStatus:
|
||||
url = f"https://api.github.com/repos/NixOS/nixpkgs/pulls/{pr}"
|
||||
pr_response = requests.get(url, headers=DEFAULT_HEADERS)
|
||||
pr_response.raise_for_status()
|
||||
|
||||
pr_data = pr_response.json()
|
||||
|
||||
merged = pr_data["merged"]
|
||||
if merged is False:
|
||||
return PRStatus(merged=False, branches={branch: False for branch in branches})
|
||||
|
||||
commit_sha = pr_data.get("merge_commit_sha")
|
||||
|
||||
results = {}
|
||||
|
||||
for branch in branches:
|
||||
in_branch = commit_in_branch(commit_sha, branch)
|
||||
results[branch] = in_branch
|
||||
|
||||
return PRStatus(merged=True, branches=results)
|
||||
Reference in New Issue
Block a user