1
1
import os
2
2
import re
3
+ import tempfile
3
4
4
5
import pytest
5
6
@@ -24,34 +25,39 @@ def tmp_commitizen_project(tmp_git_project):
24
25
yield tmp_git_project
25
26
26
27
27
- @pytest .fixture (scope = "function" )
28
- def tmp_commitizen_project_with_gpg (tmp_commitizen_project ):
29
- _gpg_file = tmp_commitizen_project .join ("gpg_setup" )
30
- _signer_mail = "action@github.com"
31
- with open (_gpg_file , "w" , newline = "" ) as f :
32
- f .write ("Key-Type: RSA" + os .linesep )
33
- f .write ("Key-Length: 2048" + os .linesep )
34
- f .write ("Subkey-Type: RSA" + os .linesep )
35
- f .write ("Subkey-Length: 2048" + os .linesep )
36
- f .write ("Name-Real: GitHub Action" + os .linesep )
37
- f .write ("Name-Comment: with stupid passphrase" + os .linesep )
38
- f .write (f"Name-Email: { _signer_mail } " + os .linesep )
39
- f .write ("Expire-Date: 1" + os .linesep )
40
-
41
- cmd .run (
42
- f"gpg --batch --passphrase '' --pinentry-mode loopback --generate-key { _gpg_file } "
43
- )
44
-
45
- _new_key = cmd .run (f"gpg --list-secret-keys { _signer_mail } " )
28
+ def _get_gpg_keyid (signer_mail ):
29
+ _new_key = cmd .run (f"gpg --list-secret-keys { signer_mail } " )
46
30
_m = re .search (
47
31
rf"[a-zA-Z0-9 \[\]-_]*{ os .linesep } [ ]*([0-9A-Za-z]*){ os .linesep } [{ os .linesep } a-zA-Z0-9 \[\]-_<>@]*" ,
48
32
_new_key .out ,
49
33
)
34
+ return _m .group (1 ) if _m else None
35
+
50
36
51
- if _m :
52
- _key_id = _m .group (1 )
53
- cmd .run ("git config commit.gpgsign true" )
54
- cmd .run (f"git config user.signingkey { _key_id } " )
37
+ @pytest .fixture (scope = "function" )
38
+ def tmp_commitizen_project_with_gpg (tmp_commitizen_project ):
39
+ signer = "GitHub Action"
40
+ signer_mail = "action@github.com"
41
+
42
+ # create a temporary GPGHOME to store a temporary keyring.
43
+ # Home path must be less than 104 characters
44
+ gpg_home = tempfile .TemporaryDirectory (suffix = "_cz" )
45
+ os .environ ["GNUPGHOME" ] = gpg_home .name # tempdir = temp keyring
46
+
47
+ # create a key (a keyring will be generated within GPUPGHOME)
48
+ c = cmd .run (
49
+ f"gpg --batch --yes --debug-quick-random --passphrase '' --quick-gen-key '{ signer } { signer_mail } '"
50
+ )
51
+ if c .return_code != 0 :
52
+ raise Exception (f"gpg keygen failed with err: '{ c .err } '" )
53
+ key_id = _get_gpg_keyid (signer_mail )
54
+ assert key_id
55
+
56
+ # configure git
57
+ cmd .run ("git config commit.gpgsign true" )
58
+ cmd .run (f"git config user.name { signer } " )
59
+ cmd .run (f"git config user.email { signer_mail } " )
60
+ cmd .run (f"git config user.signingkey { key_id } " )
55
61
56
62
yield tmp_commitizen_project
57
63
0 commit comments