|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import re |
| 4 | +from argparse import ArgumentParser, Namespace |
| 5 | +from dataclasses import dataclass, field |
| 6 | +from itertools import chain |
| 7 | +from pathlib import Path |
| 8 | +from typing import Any, Pattern |
| 9 | + |
| 10 | +from gql import Client, gql |
| 11 | +from gql.transport.aiohttp import AIOHTTPTransport |
| 12 | +from graphql import DocumentNode |
| 13 | + |
| 14 | +Dag = dict[str, list[str]] |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class GitlabGQL: |
| 19 | + _transport: Any = field(init=False) |
| 20 | + client: Client = field(init=False) |
| 21 | + url: str = "https://gitlab.freedesktop.org/api/graphql" |
| 22 | + |
| 23 | + def __post_init__(self): |
| 24 | + self._setup_gitlab_gql_client() |
| 25 | + |
| 26 | + def _setup_gitlab_gql_client(self) -> Client: |
| 27 | + # Select your transport with a defined url endpoint |
| 28 | + self._transport = AIOHTTPTransport(url=self.url) |
| 29 | + |
| 30 | + # Create a GraphQL client using the defined transport |
| 31 | + self.client = Client( |
| 32 | + transport=self._transport, fetch_schema_from_transport=True |
| 33 | + ) |
| 34 | + |
| 35 | + def query(self, gql_file: Path | str, params: dict[str, Any]) -> dict[str, Any]: |
| 36 | + # Provide a GraphQL query |
| 37 | + source_path = Path(__file__).parent |
| 38 | + pipeline_query_file = source_path / gql_file |
| 39 | + |
| 40 | + query: DocumentNode |
| 41 | + with open(pipeline_query_file, "r") as f: |
| 42 | + pipeline_query = f.read() |
| 43 | + query = gql(pipeline_query) |
| 44 | + |
| 45 | + # Execute the query on the transport |
| 46 | + return self.client.execute(query, variable_values=params) |
| 47 | + |
| 48 | + |
| 49 | +def create_job_needs_dag( |
| 50 | + gl_gql: GitlabGQL, params |
| 51 | +) -> tuple[Dag, dict[str, dict[str, Any]]]: |
| 52 | + |
| 53 | + result = gl_gql.query("pipeline_details.gql", params) |
| 54 | + dag = {} |
| 55 | + jobs = {} |
| 56 | + pipeline = result["project"]["pipeline"] |
| 57 | + if not pipeline: |
| 58 | + raise RuntimeError(f"Could not find any pipelines for {params}") |
| 59 | + |
| 60 | + for stage in pipeline["stages"]["nodes"]: |
| 61 | + for stage_job in stage["groups"]["nodes"]: |
| 62 | + for job in stage_job["jobs"]["nodes"]: |
| 63 | + needs = job.pop("needs")["nodes"] |
| 64 | + jobs[job["name"]] = job |
| 65 | + dag[job["name"]] = {node["name"] for node in needs} |
| 66 | + |
| 67 | + for job, needs in dag.items(): |
| 68 | + needs: set |
| 69 | + partial = True |
| 70 | + |
| 71 | + while partial: |
| 72 | + next_depth = {n for dn in needs for n in dag[dn]} |
| 73 | + partial = not needs.issuperset(next_depth) |
| 74 | + needs = needs.union(next_depth) |
| 75 | + |
| 76 | + dag[job] = needs |
| 77 | + |
| 78 | + return dag, jobs |
| 79 | + |
| 80 | + |
| 81 | +def filter_dag(dag: Dag, regex: Pattern) -> Dag: |
| 82 | + return {job: needs for job, needs in dag.items() if re.match(regex, job)} |
| 83 | + |
| 84 | + |
| 85 | +def print_dag(dag: Dag) -> None: |
| 86 | + for job, needs in dag.items(): |
| 87 | + print(f"{job}:") |
| 88 | + print(f"\t{' '.join(needs)}") |
| 89 | + print() |
| 90 | + |
| 91 | + |
| 92 | +def parse_args() -> Namespace: |
| 93 | + parser = ArgumentParser() |
| 94 | + parser.add_argument("-pp", "--project-path", type=str, default="mesa/mesa") |
| 95 | + parser.add_argument("--sha", type=str, required=True) |
| 96 | + parser.add_argument("--regex", type=str, required=False) |
| 97 | + parser.add_argument("--print-dag", action="store_true") |
| 98 | + |
| 99 | + return parser.parse_args() |
| 100 | + |
| 101 | + |
| 102 | +def main(): |
| 103 | + args = parse_args() |
| 104 | + gl_gql = GitlabGQL() |
| 105 | + |
| 106 | + if args.print_dag: |
| 107 | + dag, jobs = create_job_needs_dag( |
| 108 | + gl_gql, {"projectPath": args.project_path, "sha": args.sha} |
| 109 | + ) |
| 110 | + |
| 111 | + if args.regex: |
| 112 | + dag = filter_dag(dag, re.compile(args.regex)) |
| 113 | + print_dag(dag) |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments