Coverage for pyVersioning/GitLab.py: 41%

46 statements  

« 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"""GitLab specific code to collect the build environment.""" 

32from datetime import datetime 

33from os import environ 

34from typing import Optional as Nullable 

35 

36from pyTooling.Decorators import export 

37from pyTooling.Exceptions import ToolingException 

38from pyTooling.GenericPath.URL import URL 

39 

40from pyVersioning.CIService import CIService, Platform, ServiceException 

41 

42 

43@export 

44class GitLab(CIService): 

45 """Collect Git and other platform and environment information from environment variables provided by GitLab-CI.""" 

46 

47 ENV_INCLUDE_FILTER = ("CI_", "GITLAB_") #: List of environment variable name pattern for inclusion. 

48 ENV_EXCLUDE_FILTER = ("_TOKEN", ) #: List of environment variable name pattern for exclusion. 

49 ENV_INCLUDES = ("CI", ) #: List of environment variable to include. 

50 ENV_EXCLUDES = ("CI_JOB_TOKEN", ) #: List of environment variable to exclude. 

51 

52 def GetPlatform(self) -> Platform: 

53 return Platform("gitlab") 

54 

55 def GetGitHash(self) -> str: 

56 """ 

57 Returns the Git hash (SHA1 - 160-bit) as a string. 

58 

59 :return: Git hash as a hex formated string (40 characters). 

60 :raises ServiceException: If environment variable ``CI_COMMIT_SHA`` was not found. 

61 """ 

62 try: 

63 return environ["CI_COMMIT_SHA"] 

64 except KeyError as ex: 

65 raise ServiceException(f"Can't find GitLab-CI environment variable 'CI_COMMIT_SHA'.") from ex 

66 

67 def GetCommitDate(self) -> datetime: 

68 """ 

69 Returns the commit date as a :class:`~datetime.datetime`. 

70 

71 :return: Git commit date as :class:`~datetime.datetime`. 

72 :raises ServiceException: If environment variable ``CI_COMMIT_TIMESTAMP`` was not found. 

73 """ 

74 try: 

75 iso8601 = environ["CI_COMMIT_TIMESTAMP"] 

76 return datetime.fromisoformat(iso8601) 

77 except KeyError as ex: 

78 raise ServiceException(f"Can't find GitLab-CI environment variable 'CI_COMMIT_TIMESTAMP'.") from ex 

79 

80 def GetGitBranch(self) -> Nullable[str]: 

81 """ 

82 Returns Git branch name or ``None`` is not checked out on a branch. 

83 

84 :return: Git branch name or ``None``. 

85 :raises ServiceException: If environment variable ``CI_COMMIT_BRANCH`` was not found. 

86 """ 

87 try: 

88 return environ["CI_COMMIT_BRANCH"] 

89 except KeyError: 

90 return None 

91 

92 def GetGitTag(self) -> Nullable[str]: 

93 """ 

94 Returns Git tag name or ``None`` is not checked out on a tag. 

95 

96 :return: Git tag name or ``None``. 

97 :raises ServiceException: If environment variable ``CI_COMMIT_TAG`` was not found. 

98 """ 

99 try: 

100 return environ["CI_COMMIT_TAG"] 

101 except KeyError: 

102 return None 

103 

104 def GetGitRepository(self) -> str: 

105 """ 

106 Returns the Git repository URL. 

107 

108 :return: Git repository URL. 

109 :raises ServiceException: If environment variable ``CI_REPOSITORY_URL`` was not found. 

110 :raises ServiceException: If repository URL from ``CI_REPOSITORY_URL`` couldn't be parsed. 

111 """ 

112 try: 

113 repositoryURL = environ["CI_REPOSITORY_URL"] 

114 except KeyError as ex: 

115 raise ServiceException(f"Can't find GitLab-CI environment variable 'CI_REPOSITORY_URL'.") from ex 

116 

117 try: 

118 url = URL.Parse(repositoryURL) 

119 except ToolingException as ex: 

120 raise ServiceException(f"Syntax error in repository URL '{repositoryURL}'.") from ex 

121 

122 return str(url.WithoutCredentials())