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
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from .exceptions import Fail
|
||||||
import os
|
import os
|
||||||
import jcloud_config_parser
|
import jcloud_config_parser
|
||||||
import ipaddress
|
import ipaddress
|
||||||
@@ -21,7 +23,9 @@ import argparse
|
|||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'load_config',
|
'load_config',
|
||||||
'process_host_and_port'
|
'process_host_and_port',
|
||||||
|
'GiteaWebhookConfig',
|
||||||
|
'process_gitea_webhook_config',
|
||||||
]
|
]
|
||||||
|
|
||||||
def load_config(config_directory: pathlib.Path) -> tuple[
|
def load_config(config_directory: pathlib.Path) -> tuple[
|
||||||
@@ -109,4 +113,58 @@ def process_host_and_port(
|
|||||||
logger.error(f'Error in configuration: [server]port: {port} is not between 0 and 65535, using default value.')
|
logger.error(f'Error in configuration: [server]port: {port} is not between 0 and 65535, using default value.')
|
||||||
port = default_configuration.server.port
|
port = default_configuration.server.port
|
||||||
|
|
||||||
return host, int(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