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