-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdetector.py
More file actions
108 lines (79 loc) · 3.34 KB
/
detector.py
File metadata and controls
108 lines (79 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import json
import os
import pathlib
import re
import typing
from urllib import parse
from mergify_cli import utils
CIProviderT = typing.Literal["github_actions", "circleci"]
def get_ci_provider() -> CIProviderT | None:
if os.getenv("GITHUB_ACTIONS") == "true":
return "github_actions"
if os.getenv("CIRCLECI") == "true":
return "circleci"
return None
def get_job_name() -> str | None:
if get_ci_provider() == "github_actions":
return os.getenv("GITHUB_WORKFLOW")
if get_ci_provider() == "circleci":
return os.getenv("CIRCLE_JOB")
return None
def get_github_actions_head_sha() -> str | None:
if os.getenv("GITHUB_EVENT_NAME") == "pull_request":
# NOTE(leo): we want the head sha of pull request
event_raw_path = os.getenv("GITHUB_EVENT_PATH")
if event_raw_path and ((event_path := pathlib.Path(event_raw_path)).is_file()):
event = json.loads(event_path.read_bytes())
return str(event["pull_request"]["head"]["sha"])
return os.getenv("GITHUB_SHA")
async def get_circle_ci_head_sha() -> str | None:
if (pull_url := os.getenv("CIRCLE_PULL_REQUESTS")) and len(
pull_url.split(","),
) == 1:
if not (token := os.getenv("GITHUB_TOKEN")):
msg = (
"Failed to detect the head sha of the pull request associated"
" to this run. Please make sure to set a token in the env "
"variable 'GITHUB_TOKEN' for this purpose."
)
raise RuntimeError(msg)
parsed_url = parse.urlparse(pull_url)
if parsed_url.netloc == "github.com":
github_server = "https://api.github.com"
else:
github_server = f"{parsed_url.scheme}://{parsed_url.netloc}/api/v3"
async with utils.get_github_http_client(github_server, token) as client:
resp = await client.get(f"/repos{parsed_url.path}")
return str(resp.json()["head"]["sha"])
return os.getenv("CIRCLE_SHA1")
async def get_head_sha() -> str | None:
if get_ci_provider() == "github_actions":
return get_github_actions_head_sha()
if get_ci_provider() == "circleci":
return await get_circle_ci_head_sha()
return None
def get_cicd_pipeline_run_id() -> int | None:
if get_ci_provider() == "github_actions" and "GITHUB_RUN_ID" in os.environ:
return int(os.environ["GITHUB_RUN_ID"])
if get_ci_provider() == "circleci" and "CIRCLE_WORKFLOW_ID" in os.environ:
return int(os.environ["CIRCLE_WORKFLOW_ID"])
return None
def get_cicd_pipeline_run_attempt() -> int | None:
if get_ci_provider() == "github_actions" and "GITHUB_RUN_ATTEMPT" in os.environ:
return int(os.environ["GITHUB_RUN_ATTEMPT"])
if get_ci_provider() == "circleci" and "CIRCLE_BUILD_NUM" in os.environ:
return int(os.environ["CIRCLE_BUILD_NUM"])
return None
def get_github_repository() -> str | None:
if get_ci_provider() == "github_actions":
return os.getenv("GITHUB_REPOSITORY")
if get_ci_provider() == "circleci":
repository_url = os.getenv("CIRCLE_REPOSITORY_URL")
if repository_url and (
match := re.match(
r"(https?://[\w.-]+/)?(?P<full_name>[\w.-]+/[\w.-]+)/?$",
repository_url,
)
):
return match.group("full_name")
return None