|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import os |
| 4 | +import pathlib |
| 5 | +import re |
| 6 | +import typing |
| 7 | +from urllib import parse |
| 8 | + |
| 9 | +import click |
| 10 | + |
| 11 | +from mergify_cli import console |
| 12 | +from mergify_cli import utils |
| 13 | +from mergify_cli.ci import junit_upload as junit_upload_mod |
| 14 | + |
| 15 | + |
| 16 | +ci = click.Group( |
| 17 | + "ci", |
| 18 | + help="Mergify's CI related commands", |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +CIProviderT = typing.Literal["github_action", "circleci"] |
| 23 | + |
| 24 | + |
| 25 | +def get_ci_provider() -> CIProviderT | None: |
| 26 | + if os.getenv("GITHUB_ACTIONS") == "true": |
| 27 | + return "github_action" |
| 28 | + if os.getenv("CIRCLECI") == "true": |
| 29 | + return "circleci" |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +def get_job_name() -> str | None: |
| 34 | + if get_ci_provider() == "github_action": |
| 35 | + return os.getenv("GITHUB_WORKFLOW") |
| 36 | + if get_ci_provider() == "circleci": |
| 37 | + return os.getenv("CIRCLE_JOB") |
| 38 | + |
| 39 | + console.log("Error: failed to get the job's name from env", style="red") |
| 40 | + return None |
| 41 | + |
| 42 | + |
| 43 | +def get_github_actions_head_sha() -> str | None: |
| 44 | + if os.getenv("GITHUB_EVENT_NAME") == "pull_request": |
| 45 | + # NOTE(leo): we want the head sha of pull request |
| 46 | + event_raw_path = os.getenv("GITHUB_EVENT_PATH") |
| 47 | + if event_raw_path and ((event_path := pathlib.Path(event_raw_path)).is_file()): |
| 48 | + event = json.loads(event_path.read_bytes()) |
| 49 | + return str(event["pull_request"]["head"]["sha"]) |
| 50 | + return os.getenv("GITHUB_SHA") |
| 51 | + |
| 52 | + |
| 53 | +async def get_circle_ci_head_sha() -> str | None: |
| 54 | + if (pull_url := os.getenv("CIRCLE_PULL_REQUESTS")) and len( |
| 55 | + pull_url.split(","), |
| 56 | + ) == 1: |
| 57 | + if not (token := os.getenv("GITHUB_TOKEN")): |
| 58 | + msg = ( |
| 59 | + "Failed to detect the head sha of the pull request associated" |
| 60 | + " to this run. Please make sure to set a token in the env " |
| 61 | + "variable 'GITHUB_TOKEN' for this purpose." |
| 62 | + ) |
| 63 | + raise RuntimeError(msg) |
| 64 | + |
| 65 | + parsed_url = parse.urlparse(pull_url) |
| 66 | + if parsed_url.netloc == "github.com": |
| 67 | + github_server = "https://api.github.com" |
| 68 | + else: |
| 69 | + github_server = f"{parsed_url.scheme}://{parsed_url.netloc}/api/v3" |
| 70 | + |
| 71 | + async with utils.get_github_http_client(github_server, token) as client: |
| 72 | + resp = await client.get(f"/repos{parsed_url.path}") |
| 73 | + |
| 74 | + return str(resp.json()["head"]["sha"]) |
| 75 | + |
| 76 | + return os.getenv("CIRCLE_SHA1") |
| 77 | + |
| 78 | + |
| 79 | +async def get_head_sha() -> str | None: |
| 80 | + if get_ci_provider() == "github_action": |
| 81 | + return get_github_actions_head_sha() |
| 82 | + if get_ci_provider() == "circleci": |
| 83 | + return await get_circle_ci_head_sha() |
| 84 | + |
| 85 | + console.log("Error: failed to get the head SHA from env", style="red") |
| 86 | + return None |
| 87 | + |
| 88 | + |
| 89 | +def get_github_repository() -> str | None: |
| 90 | + if get_ci_provider() == "github_action": |
| 91 | + return os.getenv("GITHUB_REPOSITORY") |
| 92 | + if get_ci_provider() == "circleci": |
| 93 | + repository_url = os.getenv("CIRCLE_REPOSITORY_URL") |
| 94 | + if repository_url and ( |
| 95 | + match := re.match( |
| 96 | + r"(https?://[\w.-]+/)?(?P<full_name>[\w.-]+/[\w.-]+)/?$", |
| 97 | + repository_url, |
| 98 | + ) |
| 99 | + ): |
| 100 | + return match.group("full_name") |
| 101 | + |
| 102 | + console.log("Error: failed to get the GitHub repository from env", style="red") |
| 103 | + return None |
| 104 | + |
| 105 | + |
| 106 | +@ci.command(help="Upload JUnit XML reports") |
| 107 | +@click.option( |
| 108 | + "--api-url", |
| 109 | + "-u", |
| 110 | + help="URL of the Mergify API", |
| 111 | + required=True, |
| 112 | + envvar="MERGIFY_API_URL", |
| 113 | + default="https://api.mergify.com", |
| 114 | + show_default=True, |
| 115 | +) |
| 116 | +@click.option( |
| 117 | + "--token", |
| 118 | + "-t", |
| 119 | + help="CI Issues Application Key", |
| 120 | + required=True, |
| 121 | + envvar="MERGIFY_TOKEN", |
| 122 | +) |
| 123 | +@click.option( |
| 124 | + "--repository", |
| 125 | + "-r", |
| 126 | + help="Repository full name (owner/repo)", |
| 127 | + required=True, |
| 128 | + default=get_github_repository, |
| 129 | +) |
| 130 | +@click.option( |
| 131 | + "--head-sha", |
| 132 | + "-s", |
| 133 | + help="Head SHA of the triggered job", |
| 134 | + required=True, |
| 135 | + default=lambda: asyncio.run(get_head_sha()), |
| 136 | +) |
| 137 | +@click.option( |
| 138 | + "--job-name", |
| 139 | + "-j", |
| 140 | + help="Job's name", |
| 141 | + required=True, |
| 142 | + default=get_job_name, |
| 143 | +) |
| 144 | +@click.option( |
| 145 | + "--provider", |
| 146 | + "-p", |
| 147 | + help="CI provider", |
| 148 | + default=get_ci_provider, |
| 149 | +) |
| 150 | +@click.argument( |
| 151 | + "files", |
| 152 | + nargs=-1, |
| 153 | + required=True, |
| 154 | + type=click.Path(exists=True, dir_okay=False), |
| 155 | +) |
| 156 | +@utils.run_with_asyncio |
| 157 | +async def junit_upload( # noqa: PLR0913, PLR0917 |
| 158 | + api_url: str, |
| 159 | + token: str, |
| 160 | + repository: str, |
| 161 | + head_sha: str, |
| 162 | + job_name: str, |
| 163 | + provider: str | None, |
| 164 | + files: tuple[str, ...], |
| 165 | +) -> None: |
| 166 | + await junit_upload_mod.upload( |
| 167 | + api_url=api_url, |
| 168 | + token=token, |
| 169 | + repository=repository, |
| 170 | + head_sha=head_sha, |
| 171 | + job_name=job_name, |
| 172 | + provider=provider, |
| 173 | + files=files, |
| 174 | + ) |
0 commit comments