This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathconfiguration.py
246 lines (218 loc) · 9.1 KB
/
configuration.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# -*- coding: utf-8 -*-
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import configparser
import logging
import sys
from pathlib import Path
from typing import Optional, List, Dict
import yaml
from ogr import GithubService, PagureService, get_project
from ogr.abstract import GitProject
from release_bot.version import __version__
from release_bot.github import GitHubApp
class Configuration:
# note that required items need to reference strings as their length is checked
REQUIRED_ITEMS: Dict[str, List] = {"conf": [], "release-conf": []}
def __init__(self):
self.version = __version__
self.repository_name = ""
self.repository_owner = ""
self.github_token = ""
self.github_username = ""
self.pagure_token = ""
self.pagure_username = ""
self.refresh_interval = None
self.debug = False
self.configuration = ""
self.logger = None
self.init = False
self.set_logging()
self.dry_run = False
# used when PyPI project name != repository name
self.pypi_project = ""
# configuration when bot is deployed as github app
self.github_app_installation_id = ""
self.github_app_id = ""
self.github_app_cert_path = ""
self.clone_url = ""
# used for different pagure forges pagure.io by default
self.pagure_instance_url = "https://pagure.io"
self.webhook_handler = False
self.gitchangelog = False
self.project: Optional[GitProject] = None
def set_logging(
self,
logger_name="release-bot",
level=logging.INFO,
handler_class=logging.StreamHandler,
handler_kwargs=None,
msg_format="%(asctime)s.%(msecs).03d %(filename)-17s %(levelname)-6s %(message)s",
date_format="%H:%M:%S",
):
"""
Set personal logger for this library.
:param logger_name: str, name of the logger
:param level: int, see logging.{DEBUG,INFO,ERROR,...}: level of logger and handler
:param handler_class: logging.Handler instance, default is StreamHandler (/dev/stderr)
:param handler_kwargs: dict, keyword arguments to handler's constructor
:param msg_format: str, formatting style
:param date_format: str, date style in the logs
:return: logger instance
"""
logger = logging.getLogger(logger_name)
# do we want to propagate to root logger?
# logger.propagate = False
logger.setLevel(level)
if not [x for x in logger.handlers if isinstance(x, handler_class)]:
handler_kwargs = handler_kwargs or {}
handler = handler_class(**handler_kwargs)
formatter = logging.Formatter(msg_format, date_format)
handler.setFormatter(formatter)
logger.addHandler(handler)
self.logger = logger
def load_configuration(self):
"""Load bot configuration from .yaml file"""
if not self.configuration:
# configuration not supplied, look for conf.yaml in cwd
path = Path.cwd() / "conf.yaml"
if path.is_file():
self.configuration = path
else:
self.logger.error("Cannot find valid configuration")
sys.exit(1)
with self.configuration.open() as ymlfile:
file = yaml.safe_load(ymlfile)
for key, value in file.items():
if hasattr(self, key):
setattr(self, key, value)
# check if required items are present
for item in self.REQUIRED_ITEMS["conf"]:
if item not in file:
self.logger.error(f"Item {item!r} is required in configuration!")
sys.exit(1)
# if user hasn't specified clone_url, use default
if "clone_url" not in file:
self.clone_url = (
f"https://github.com/{self.repository_owner}"
f"/{self.repository_name}.git"
)
self.project = self.get_project()
self.logger.debug(
f"Loaded configuration for {self.repository_owner}/{self.repository_name}"
)
def load_release_conf(self, conf):
"""
Load items from release-conf.yaml
:param conf: contents of release-conf.yaml
:return dict with configuration
"""
if not conf:
self.logger.error(
"No release-conf.yaml found in "
f"{self.repository_owner}/{self.repository_name} repository root!\n"
"You have to add one for releasing to PyPi"
)
if self.REQUIRED_ITEMS["release-conf"]:
sys.exit(1)
parsed_conf = yaml.safe_load(conf) or {}
# Set defaults for options not specified in release-conf.yaml
parsed_conf.setdefault("pypi", True)
parsed_conf.setdefault("trigger_on_issue", True)
parsed_conf = {k: v for (k, v) in parsed_conf.items() if v}
for item in self.REQUIRED_ITEMS["release-conf"]:
if item not in parsed_conf:
self.logger.error(f"Item {item!r} is required in release-conf!")
sys.exit(1)
for index, label in enumerate(parsed_conf.get("labels", [])):
parsed_conf["labels"][index] = str(label)
if parsed_conf.get("trigger_on_issue"):
if not self.github_username and not self.pagure_username:
msg = (
"Can't trigger on issue if "
"'github_username/pagure_username' is not known, disabling"
)
self.logger.warning(msg)
parsed_conf["trigger_on_issue"] = False
self.logger.debug(f"Config: {parsed_conf}")
return parsed_conf
def set_pypi_project(self, parsed_conf, setup_cfg=None):
"""
Set pypi_project attribute either from release-conf.yaml or from setup.cfg.
Fall back to repository_name if neither file specifies it.
:param parsed_conf: parsed release-conf.yaml content, load_release_conf() result
:param setup_cfg: setup.cfg content
"""
if parsed_conf.get("pypi"):
self.pypi_project = parsed_conf.get(
"pypi_project"
) or self.get_pypi_project_from_setup_cfg(setup_cfg)
if self.pypi_project is None:
msg = "pypi_project is not set, falling back to repository_name"
self.logger.warning(msg)
self.pypi_project = self.repository_name
@staticmethod
def get_pypi_project_from_setup_cfg(setup_cfg):
"""
Get the name of PyPI project from the metadata section of setup.cfg
:param setup_cfg: str, setup.cfg content
:return str or None, PyPI project name
"""
if not setup_cfg:
return None
pypi_config = configparser.ConfigParser()
pypi_config.read_string(setup_cfg)
if pypi_config:
try:
metadata = pypi_config["metadata"]
return metadata.get("name", None)
except KeyError:
return None
return None
def get_project(self):
"""
Create ogr project instance based on provided configuration.
Project instance is used for manipulating with Github/Pagure repo.
:return: ogr Github/Pagure project instance or None
"""
# Return instance for github app
if self.github_app_id:
github_cert = Path(self.github_app_cert_path).read_text()
if self.github_app_installation_id:
# github token will be used as a credential over http (commit/push)
github_app = GitHubApp(self.github_app_id, self.github_app_cert_path)
self.github_token = github_app.get_installation_access_token(
self.github_app_installation_id
)
return get_project(
url=self.clone_url,
custom_instances=[
GithubService(
token=None,
github_app_id=self.github_app_id,
github_app_private_key=github_cert,
)
],
)
# Return instance for regular user (local machine)
return get_project(
url=self.clone_url,
custom_instances=[
GithubService(token=self.github_token),
PagureService(
token=self.pagure_token, instance_url=self.pagure_instance_url
),
],
)
configuration = Configuration()