"""Latest public GitHub Actions, GitLab pipeline or AppVeyor build, with honest states.""" from urllib.parse import quote, urlencode from badger import entry, fetch_json _COLORS = { "success": "#2da44e", "failure": "#e05d44", "failed": "#e05d44", "timed_out": "#e05d44", "startup_failure": "#e05d44", "action_required": "#dfb317", "queued": "#dfb317", "in_progress": "#dfb317", "running": "#dfb317", "pending": "#dfb317", "waiting": "#dfb317", "requested": "#dfb317", "created": "#dfb317", "preparing": "#dfb317", "waiting_for_resource": "#dfb317", "waiting_for_callback": "#dfb317", "manual": "#dfb317", "scheduled": "#dfb317", "canceling": "#dfb317", } def _status(run, provider): if not isinstance(run, dict): raise ValueError("CI run must be an object") value = run.get("status") if provider == "github" and value == "completed": value = run.get("conclusion") if value is None or value == "": value = "unknown" if not isinstance(value, str): raise ValueError("CI status must be text") color = _COLORS.get(value, "#6a737d") if provider == "github" and run.get("status") != "completed": # Conclusions are meaningful only after the run is completed. active = ("queued", "in_progress", "waiting", "pending", "requested") color = "#dfb317" if value in active else "#6a737d" return entry(label="build", status=value.replace("_", " "), secondary_color=color) def run( repository: str, provider: str = "github", workflow: str = "", branch: str = "" ) -> entry: """Show the latest public run, not the last successful run. Args: repository: owner/repository, nested GitLab group/project, or AppVeyor account/project slug. provider: github, gitlab, or appveyor. workflow: GitHub workflow filename or numeric ID; empty means any workflow. branch: optional branch/ref filter; empty means any branch, not default branch. An empty result is no runs, missing conclusions are unknown, and HTTP errors propagate. Canceled, skipped, neutral and unfamiliar states remain neutral. Examples: >>> ci(repository="example/project", workflow="ci.yml") entry(label="build", status="success", secondary_color="#2da44e") >>> ci(repository="example/project", provider="gitlab", branch="main") entry(label="build", status="running", secondary_color="#dfb317") >>> ci(repository="example/project", provider="appveyor") entry(label="build", status="success", secondary_color="#2da44e") """ if provider not in ("github", "gitlab", "appveyor"): raise ValueError("provider must be github, gitlab, or appveyor") if not isinstance(repository, str): raise ValueError("repository must be an owner/repository path") parts = repository.split("/") if len(parts) < 2 or any(not p or p in (".", "..") for p in parts): raise ValueError("repository must be an owner/repository path") if not isinstance(workflow, str) or not isinstance(branch, str): raise ValueError("workflow and branch must be strings") if workflow and provider != "github": raise ValueError("workflow is only supported for github") if provider == "appveyor": if len(parts) != 2: raise ValueError("appveyor requires exactly account/project slug") url = "https://ci.appveyor.com/api/projects/" + "/".join( quote(p, safe="") for p in parts ) if branch: url += "/branch/" + quote(branch, safe="") data = fetch_json(url) if not isinstance(data, dict) or "build" not in data: raise ValueError("AppVeyor API must return a build field") return _status({} if data["build"] is None else data["build"], provider) params = {"per_page": 1} if provider == "github": if len(parts) != 2: raise ValueError("github requires exactly owner/repository") url = "https://api.github.com/repos/" + "/".join( quote(p, safe="") for p in parts ) url += ( "/actions/workflows/" + quote(workflow, safe="") + "/runs" if workflow else "/actions/runs" ) if branch: params["branch"] = branch data = fetch_json( url + "?" + urlencode(params), headers={"Accept": "application/vnd.github+json"}, ) if not isinstance(data, dict) or not isinstance( data.get("workflow_runs"), list ): raise ValueError("GitHub API must return a workflow_runs list") runs = data["workflow_runs"] else: url = ( "https://gitlab.com/api/v4/projects/" + quote(repository, safe="") + "/pipelines" ) params.update({"order_by": "id", "sort": "desc"}) if branch: params["ref"] = branch runs = fetch_json(url + "?" + urlencode(params)) if not isinstance(runs, list): raise ValueError("GitLab API must return a pipeline list") if not runs: return entry(label="build", status="no runs", secondary_color="#6a737d") return _status(runs[0], provider)