1
1
import contextlib
2
2
import json
3
+ import os
3
4
import pathlib
4
5
import shutil
5
6
import subprocess
@@ -27,11 +28,18 @@ def package_name(wheel_file: pathlib.Path) -> str:
27
28
context_info : dict = {
28
29
"timestamp" : ts ,
29
30
}
31
+
32
+ # Get branch info.
30
33
with contextlib .suppress (Exception ):
31
- branch_info = subprocess .check_output (
32
- ["git" , "branch" , "--show-current" ], text = True
33
- )
34
+ if branch_info := os .getenv ("GITHUB_HEAD_REF" ):
35
+ pass
36
+ else :
37
+ branch_info = subprocess .check_output (
38
+ ["git" , "branch" , "--show-current" ], text = True
39
+ )
34
40
context_info ["branch" ] = branch_info .strip ()
41
+
42
+ # Get commit info.
35
43
with contextlib .suppress (Exception ):
36
44
commit_info = subprocess .check_output (
37
45
["git" , "log" , "-1" , "--pretty=%H%n%B" ], text = True
@@ -41,11 +49,28 @@ def package_name(wheel_file: pathlib.Path) -> str:
41
49
"hash" : commit_hash ,
42
50
"message" : commit_msg .strip (),
43
51
}
52
+
53
+ # Get PR info.
44
54
with contextlib .suppress (Exception ):
45
- pr_info = subprocess .check_output (
46
- ["gh" , "pr" , "view" , "--json" , "title,number,url" ], text = True
47
- )
48
- pr_info = json .loads (pr_info )
55
+ pr_info = "unknown"
56
+ if github_ref := os .getenv ("GITHUB_REF" ):
57
+ # e.g. GITHUB_REF=refs/pull/12157/merge
58
+ parts = github_ref .split ("/" )[- 1 ]
59
+ if parts [1 ] == "pull" :
60
+ pull_number = parts [2 ]
61
+ pr_info = json .loads (
62
+ subprocess .check_output (
63
+ ["gh" , "pr" , "view" , pull_number , "--json" , "title,number,url" ],
64
+ text = True ,
65
+ )
66
+ )
67
+ else :
68
+ # The `gh` CLI might be able to figure it out.
69
+ pr_info = json .loads (
70
+ subprocess .check_output (
71
+ ["gh" , "pr" , "view" , "--json" , "title,number,url" ], text = True
72
+ )
73
+ )
49
74
context_info ["pr" ] = pr_info
50
75
51
76
0 commit comments