forked from dimagi/required-labels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
80 lines (58 loc) · 2.68 KB
/
config.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
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
import os
import sys
from configparser import ConfigParser, NoSectionError
from pathlib import Path
from exceptions import NoGitHubTokenException
APP_BASEDIR = Path(os.path.abspath(__file__)).parent
APP_NAME = "dimagi/required-labels"
CONFIG_FILENAME = "custom.conf"
class ConfigException(Exception):
pass
def generate_config():
config = {}
conf = ConfigParser()
conf.read(_get_config_file())
try:
config['required_any'] = conf.get('Labels', 'required-labels-any')
config['required_all'] = conf.get('Labels', 'required-labels-all')
config['banned'] = conf.get('Labels', 'banned-labels')
config['github_user'] = conf.get('GitHub', 'user')
config['github_pw'] = conf.get('GitHub', 'password')
config['github_token'] = conf.get('GitHub', 'token')
except NoSectionError:
config['required_any'] = os.environ.get('REQUIRED_LABELS_ANY', None)
config['required_all'] = os.environ.get('REQUIRED_LABELS_ALL', None)
config['banned'] = os.environ.get('BANNED_LABELS', None)
config['github_user'] = os.environ.get('GITHUB_USER', None)
config['github_pw'] = os.environ.get('GITHUB_PW', None)
config['github_token'] = os.environ.get('GITHUB_TOKEN', None)
for label in ['required_any', 'required_all', 'banned']:
config[label] = config[label].split(',') if config[label] else None
return config
def _get_config_file():
if 'CONFIG_FILE' in os.environ:
return os.environ['CONFIG_FILE']
return os.path.join(APP_BASEDIR, CONFIG_FILENAME)
CONFIG = generate_config()
def get_token():
if not CONFIG['github_token']:
raise NoGitHubTokenException
return CONFIG['github_token']
def get_credentials():
if CONFIG['github_user'] == '' or CONFIG['github_pw'] == '':
return None
return CONFIG['github_user'], CONFIG['github_pw']
UNIT_TESTING = any([arg for arg in sys.argv if 'test' in arg])
if not UNIT_TESTING:
labels_configured = any([CONFIG['required_any'], CONFIG['required_all'],
CONFIG['banned']])
credentials_configured = all([CONFIG['github_pw'], CONFIG['github_user']])
if not labels_configured or not credentials_configured:
raise ConfigException(
"Please ensure your config file has a [Labels] and [Github] section.\n"
"Did you forget to create a configuration file?\n"
"You can do this by running\033[1m cp {0}.template {0} \033[0m\n"
"You can also add REQUIRED_LABELS_ALL, REQUIRED_LABELS_ANY, or BANNED_LABELS along with "
"GITHUB_TOKEN or GITHUB_USER and GITHUB_PW as environment variables"
"".format(CONFIG_FILENAME)
)