Coverage for pyVersioning/Travis.py: 44%
41 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-05-30 22:21 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-05-30 22:21 +0000
1# ==================================================================================================================== #
2# __ __ _ _ #
3# _ __ _ \ \ / /__ _ __ ___(_) ___ _ __ (_)_ __ __ _ #
4# | '_ \| | | \ \ / / _ \ '__/ __| |/ _ \| '_ \| | '_ \ / _` | #
5# | |_) | |_| |\ V / __/ | \__ \ | (_) | | | | | | | | (_| | #
6# | .__/ \__, | \_/ \___|_| |___/_|\___/|_| |_|_|_| |_|\__, | #
7# |_| |___/ |___/ #
8# ==================================================================================================================== #
9# Authors: #
10# Patrick Lehmann #
11# #
12# License: #
13# ==================================================================================================================== #
14# Copyright 2020-2025 Patrick Lehmann - Bötzingen, Germany #
15# #
16# Licensed under the Apache License, Version 2.0 (the "License"); #
17# you may not use this file except in compliance with the License. #
18# You may obtain a copy of the License at #
19# #
20# http://www.apache.org/licenses/LICENSE-2.0 #
21# #
22# Unless required by applicable law or agreed to in writing, software #
23# distributed under the License is distributed on an "AS IS" BASIS, #
24# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
25# See the License for the specific language governing permissions and #
26# limitations under the License. #
27# #
28# SPDX-License-Identifier: Apache-2.0 #
29# ==================================================================================================================== #
30#
31"""Travis specific code to collect the build environment."""
32from os import environ
33from typing import Optional as Nullable, Dict
35from pyTooling.Decorators import export
36from pyTooling.Exceptions import ToolingException
37from pyTooling.GenericPath.URL import URL
39from pyVersioning.CIService import CIService, Platform, ServiceException
42@export
43class Travis(CIService):
44 """Collect Git and other platform and environment information from environment variables provided by Travis-CI."""
46 ENV_INCLUDE_FILTER = ("TRAVIS_", ) #: List of environment variable name pattern for inclusion.
47 ENV_EXCLUDE_FILTER = ("_TOKEN", ) #: List of environment variable name pattern for exclusion.
48 ENV_INCLUDES = ("CI", "CONTINUOUS_INTEGRATION", "TRAVIS") #: List of environment variable to include.
49 ENV_EXCLUDES = () #: List of environment variable to exclude.
51 def GetPlatform(self) -> Platform:
52 return Platform("travis")
54 def GetEnvironment(self) -> Dict[str, str]:
55 return {}
57 def GetGitHash(self) -> str:
58 """
59 Returns the Git hash (SHA1 - 160-bit) as a string.
61 :return: Git hash as a hex formated string (40 characters).
62 :raises ServiceException: If environment variable ``TRAVIS_COMMIT`` was not found.
63 """
64 try:
65 return environ["TRAVIS_COMMIT"]
66 except KeyError as ex:
67 raise ServiceException(f"Can't find Travis environment variable 'TRAVIS_COMMIT'.") from ex
69 def GetGitBranch(self) -> Nullable[str]:
70 """
71 Returns Git branch name or ``None`` is not checked out on a branch.
73 :return: Git branch name or ``None``.
74 :raises ServiceException: If environment variable ``TRAVIS_BRANCH`` was not found.
75 """
76 try:
77 return environ["TRAVIS_BRANCH"]
78 except KeyError:
79 return None
81 def GetGitTag(self) -> Nullable[str]:
82 """
83 Returns Git tag name or ``None`` is not checked out on a branch.
85 :return: Git tag name or ``None``.
86 :raises ServiceException: If environment variable ``TRAVIS_TAG`` was not found.
87 """
88 try:
89 return environ["TRAVIS_TAG"]
90 except KeyError:
91 return None
93 def GetGitRepository(self) -> str:
94 """
95 Returns the Git repository URL.
97 :return: Git repository URL.
98 :raises ServiceException: If environment variable ``TRAVIS_REPO_SLUG`` was not found.
99 :raises ServiceException: If repository URL from ``TRAVIS_REPO_SLUG`` couldn't be parsed.
100 """
101 try:
102 repositoryURL = environ["TRAVIS_REPO_SLUG"]
103 except KeyError as ex:
104 raise ServiceException(f"Can't find Travis environment variable 'TRAVIS_REPO_SLUG'.") from ex
106 try:
107 url = URL.Parse(repositoryURL)
108 except ToolingException as ex:
109 raise ServiceException(f"Syntax error in repository URL '{repositoryURL}'.") from ex
111 return str(url.WithoutCredentials())