generated from jCloud/repository-template
Add Gitea configuration processing in api.config
This commit is contained in:
@@ -12,6 +12,8 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from dataclasses import dataclass
|
||||
from .exceptions import Fail
|
||||
import os
|
||||
import jcloud_config_parser
|
||||
import ipaddress
|
||||
@@ -21,7 +23,9 @@ import argparse
|
||||
|
||||
__all__ = [
|
||||
'load_config',
|
||||
'process_host_and_port'
|
||||
'process_host_and_port',
|
||||
'GiteaWebhookConfig',
|
||||
'process_gitea_webhook_config',
|
||||
]
|
||||
|
||||
def load_config(config_directory: pathlib.Path) -> tuple[
|
||||
@@ -110,3 +114,57 @@ def process_host_and_port(
|
||||
port = default_configuration.server.port
|
||||
|
||||
return host, int(port)
|
||||
|
||||
@dataclass
|
||||
class GiteaWebhookConfig:
|
||||
enabled: bool
|
||||
secret_file_path: pathlib.Path
|
||||
|
||||
def _is_readable_file(path: pathlib.Path) -> bool:
|
||||
'''
|
||||
Returns whether the file is readable and exists.
|
||||
|
||||
:param path: The file path.
|
||||
:type path: pathlib.Path
|
||||
|
||||
:return: Whether the file is readable and exists.
|
||||
:rtype: bool
|
||||
'''
|
||||
|
||||
if not path.exists():
|
||||
return False
|
||||
|
||||
try:
|
||||
with open(str(path), 'rb'):
|
||||
pass
|
||||
except (OSError, PermissionError):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def process_gitea_webhook_config(
|
||||
configuration: jcloud_config_parser.ini.INIConfiguration,
|
||||
logger: logging.Logger
|
||||
) -> GiteaWebhookConfig:
|
||||
'''
|
||||
Processes the Gitea webhook configuration.
|
||||
|
||||
:param configuration: The configuration.
|
||||
:type configuration: jcloud_config_parser.ini.INIConfiguration
|
||||
:param logger: The logger.
|
||||
:type logger: logging.Logger
|
||||
|
||||
:return: The Gitea webhooks configuration.
|
||||
:rtype: GiteaWebhookConfig
|
||||
'''
|
||||
|
||||
if configuration['gitea'].enabled not in ('true', 'yes', 't', 'y'):
|
||||
return GiteaWebhookConfig(False, None)
|
||||
|
||||
secret_file_path = pathlib.Path(configuration['gitea'].webhook_secret_file)
|
||||
|
||||
if not _is_readable_file(secret_file_path):
|
||||
logger.critical(f'{secret_file_path}: Cannot read Gitea webhook secret file')
|
||||
raise Fail(f'{secret_file_path}: Cannot read Gitea webhook secret file', exit_code = 2)
|
||||
|
||||
return GiteaWebhookConfig(True, secret_file_path)
|
||||
Reference in New Issue
Block a user