-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpre-commit.py
executable file
·54 lines (42 loc) · 1.69 KB
/
pre-commit.py
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
#!/usr/bin/env python
from pathlib import Path
import subprocess
import sys
OPENAI_KEY_PATTERN = 'sk-[a-zA-Z0-9]{48}'
YELLOW = '\033[93m'
RED = '\033[91m'
END = '\033[0m'
BOLD = '\033[1m'
PROJECT_DIR = Path('~/jabberwocky/').expanduser()
def warn(text):
print(YELLOW + BOLD + text + END)
def error(text):
print(RED + BOLD + text + END)
def main():
# Piping in subprocess requires str arg instead of list and shell=True.
print('Checking committed files for openai API keys...')
git_matches = subprocess.run(
f'git ls-files | ack -x {OPENAI_KEY_PATTERN}',
shell=True
)
# Ack returns return code of 0 when matches are found.
code = 1 - git_matches.returncode
if code:
error('\nERROR: FOUND POSSIBLE EXPOSED API KEY. \nCommit aborted. '
'Use the `--no-verify` to force commit anyway.\n')
else:
# Disabled checking venv dirs because it was making commits sooo slow.
# Use absolute paths for ignore dirs - seems like because we specify
# the main dir to search as an absolute path, ack understandably
# interprets all paths as absolute.
print('Checking all files for openai API keys...')
cmd = f'ack --ignore-dir={PROJECT_DIR/"alexa/venv"} --ignore-dir='\
f'{PROJECT_DIR/"gui/venv"} {OPENAI_KEY_PATTERN} {PROJECT_DIR}'
all_matches = subprocess.run(cmd.split())
if not all_matches.returncode:
warn('\nWARNING: FOUND POSSIBLE EXPOSED API KEY. \nAllowing '
'commit to proceed because it doesn\'t appear to be in a '
'file you\'ve committed to git, but be very careful.')
sys.exit(code)
if __name__ == '__main__':
main()