-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgithub_actions.py
More file actions
92 lines (64 loc) · 2.46 KB
/
github_actions.py
File metadata and controls
92 lines (64 loc) · 2.46 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
import os
import re
import subprocess
from codecov_cli.helpers.ci_adapters.base import CIAdapterBase
class GithubActionsCIAdapter(CIAdapterBase):
# https://docs.github.com/en/actions/learn-github-actions/environment-variables
def detect(self) -> bool:
return bool(os.getenv("GITHUB_ACTIONS"))
def _get_commit_sha(self):
pr = self._get_pull_request_number()
commit = os.getenv("GITHUB_SHA")
if not pr:
return commit
# actions/checkout should be run with fetch-depth > 1 or set to 0 for this to work
completed_subprocess = subprocess.run(
["git", "rev-parse", "HEAD^@"], capture_output=True
)
parents_hash = completed_subprocess.stdout.decode().strip().splitlines()
if len(parents_hash) == 2:
return parents_hash[1]
return commit
def _get_build_url(self):
server_url = os.getenv("GITHUB_SERVER_URL")
slug = self._get_slug()
build_code = self._get_build_code()
if server_url and slug and build_code:
return f"{server_url}/{slug}/actions/runs/{build_code}"
return None
def _get_build_code(self):
return os.getenv("GITHUB_RUN_ID")
def _get_job_code(self):
return os.getenv("GITHUB_WORKFLOW")
def _get_pull_request_number(self):
if not os.getenv("GITHUB_HEAD_REF"):
return None
pr_ref = os.getenv("GITHUB_REF")
if not pr_ref:
return None
match = re.search(r"refs/pull/(\d+)/merge", pr_ref)
if match is None:
return None
pr = match.group(1)
return pr or None
def _get_slug(self):
return os.getenv("GITHUB_REPOSITORY")
def _get_branch(self):
def remove_prefix(s: str, prefix: str) -> str:
if s.startswith(prefix):
return s[len(prefix) :]
return ""
head_ref = os.getenv("GITHUB_HEAD_REF", "")
ref = remove_prefix(os.getenv("GITHUB_REF", ""), "refs/heads/")
branch = head_ref or ref
# branch format for merge queue CI runs:
# gh-readonly-queue/<branch-name>/<pr-number>-<pr-name>
if branch.startswith("gh-readonly-queue/"):
return branch.split("/")[1]
return branch or None
def _get_service(self):
return "github-actions"
def get_service_name(self):
return "GithubActions"
def _get_job_name(self):
return os.getenv("GITHUB_JOB")