22
22
import re
23
23
import sys
24
24
import click
25
+ import logging
26
+
27
+ logger = logging .getLogger (__name__ )
25
28
26
29
27
30
@click .command ()
@@ -31,7 +34,7 @@ def add_approvals(ctx, commit_msg_file):
31
34
"""
32
35
Include approvals in a merge commit message.
33
36
34
- NOTE: This command should only be invoked as part of a
37
+ NOTE: This command should only be invoked as part of a
35
38
prepare-commit-msg hook.
36
39
37
40
\b
@@ -44,37 +47,38 @@ def add_approvals(ctx, commit_msg_file):
44
47
try :
45
48
merge_head_hash = repo .revparse_single ("MERGE_HEAD" ).id .__str__ ()
46
49
merge_head = Commit (merge_head_hash )
47
- except :
50
+ except Exception :
48
51
return
49
-
52
+
50
53
head = Commit (repo .revparse_single ("HEAD" ).id )
51
54
threshold = get_approval_threshold (head )
52
55
if not threshold :
53
56
return
54
-
57
+
55
58
approvals = get_approvals (merge_head , project )
56
59
if len (approvals ) < threshold :
57
60
raise CliFail (
58
- f"Found { len (approvals )} approvals for { merge_head .hash } but expected { threshold } ."
61
+ f"Found { len (approvals )} approvals for { merge_head .hash } "
62
+ "but expected {threshold}."
59
63
)
60
-
64
+
61
65
click .echo (f"Found { len (approvals )} approvals for { merge_head .hash } !" )
62
66
sys .stdin = open ("/dev/tty" , "r" )
63
67
click .confirm (
64
68
"Do you want to include them in the merge commit message?" ,
65
69
abort = True ,
66
- err = True
70
+ err = True ,
67
71
)
68
72
write_approvals_to_commit_msg (
69
- approvals ,
70
- commit_msg_file ,
71
- project .project_path ,
72
- merge_head
73
+ approvals , commit_msg_file , project .project_path , merge_head
73
74
)
74
75
75
76
76
77
def write_approvals_to_commit_msg (
77
- approvals : list [str ], commit_msg_file : str , project_path : str , merge_head : Commit
78
+ approvals : list [str ],
79
+ commit_msg_file : str ,
80
+ project_path : str ,
81
+ merge_head : Commit ,
78
82
):
79
83
with open (f"{ project_path } /{ commit_msg_file } " , "w" ) as f :
80
84
f .write ("\n " * 2 )
@@ -101,8 +105,8 @@ def get_approvals(merge_head: Commit, project):
101
105
# Try to fetch approvals from remote
102
106
try :
103
107
cmd ("git" , "origin" , "fetch" , "refs/signatures/*:refs/signatures/*" )
104
- except :
105
- pass
108
+ except Exception :
109
+ logger . error ( "Failed to fetch from 'refs/signatures'" )
106
110
107
111
references = repo .references .iterator ()
108
112
approvals = []
0 commit comments