Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

daily-status.py and dispatch-build: Replace PyGithub with ghreq and Pydantic #187

Merged
merged 11 commits into from
Nov 20, 2023
Prev Previous commit
Next Next commit
Type-check dispatch-build
jwodder committed Nov 14, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d74185443e1a484cc7bcc3579cbd471ceaad0132
48 changes: 35 additions & 13 deletions .github/workflows/tools/dispatch-build
Original file line number Diff line number Diff line change
@@ -1,31 +1,48 @@
#!/usr/bin/env python3
__requires__ = ["click ~= 8.0", "PyGithub == 2.*"]
from __future__ import annotations
from collections.abc import Sequence
import os
import subprocess
import click
from github import Auth, Github
from github.Workflow import Workflow

__python_requires__ = ">= 3.8"
__requires__ = ["click ~= 8.0", "PyGithub == 2.*"]

REPO = "datalad/git-annex"
ALL_OS_TYPES = ("macos", "ubuntu", "windows")


class BuildDispatcher:
def __init__(self, token):
def __init__(self, token: str) -> None:
self.gh = Github(auth=Auth.Token(token))
self.repo = self.gh.get_repo(REPO)

def get_os_workflows(self, ostypes):
def get_os_workflows(self, ostypes: Sequence[str]) -> list[Workflow]:
return [self.repo.get_workflow(f"build-{o}.yaml") for o in ostypes]

def build_pr(self, pr, ostypes=ALL_OS_TYPES, workflow_ref="master"):
def build_pr(
self,
pr: int,
ostypes: Sequence[str] = ALL_OS_TYPES,
workflow_ref: str = "master",
) -> None:
for w in self.get_os_workflows(ostypes):
w.create_dispatch(ref=workflow_ref, inputs={"pr": str(pr)})

def build_commitish(self, commitish, ostypes=ALL_OS_TYPES, workflow_ref="master"):
def build_commitish(
self,
commitish: str,
ostypes: Sequence[str] = ALL_OS_TYPES,
workflow_ref: str = "master",
) -> None:
for w in self.get_os_workflows(ostypes):
w.create_dispatch(ref=workflow_ref, inputs={"commitish": commitish})

def build_latest(self, ostypes=ALL_OS_TYPES, workflow_ref="master"):
def build_latest(
self, ostypes: Sequence[str] = ALL_OS_TYPES, workflow_ref: str = "master"
) -> None:
for w in self.get_os_workflows(ostypes):
w.create_dispatch(ref=workflow_ref, inputs={})

@@ -48,7 +65,9 @@ class BuildDispatcher:
show_default=True,
)
@click.argument("ref", required=False)
def main(ostypes, pr, ref, workflow_ref):
def main(
ostypes: tuple[str, ...], pr: bool, ref: str | None, workflow_ref: str
) -> None:
"""
Trigger builds of datalad/git-annex.

@@ -91,16 +110,19 @@ def main(ostypes, pr, ref, workflow_ref):
token = os.environ.get("GITHUB_TOKEN")
if token is None:
token = subprocess.check_output(
["git", "config", "hub.oauthtoken"], universal_newlines=True
["git", "config", "hub.oauthtoken"], text=True
).strip()
dispatcher = BuildDispatcher(token)
if pr:
try:
int(ref)
except ValueError:
if ref is None:
raise click.UsageError("--pr requires a PR number")
dispatcher.build_pr(pr, ostypes, workflow_ref=workflow_ref)
elif ref:
else:
try:
pr_num = int(ref)
except ValueError:
raise click.UsageError("--pr requires a PR number")
dispatcher.build_pr(pr_num, ostypes, workflow_ref=workflow_ref)
elif ref is not None:
dispatcher.build_commitish(ref, ostypes, workflow_ref=workflow_ref)
else:
dispatcher.build_latest(ostypes, workflow_ref=workflow_ref)
10 changes: 10 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -17,6 +17,16 @@ def typing_daily_status(session: nox.Session) -> None:
session.run("mypy", path)


@nox.session
def typing_dispatch_build(session: nox.Session) -> None:
path = ".github/workflows/tools/dispatch-build"
install_requires(session, path)
# PyGithub uses python-dateutil and requests, so apparently their typing
# stubs have to be installed in order for mypy to analyze PyGithub
session.install("mypy", "types-python-dateutil", "types-requests")
session.run("mypy", path)


def install_requires(session: nox.Session, path: str) -> None:
tmpdir = session.create_tmp()
reqfile = os.path.join(tmpdir, "requirements.txt")