Coverage for pyVersioning/AppVeyor.py: 44%

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

32from os import environ 

33from typing import Optional as Nullable 

34 

35from pyTooling.Decorators import export 

36from pyTooling.Exceptions import ToolingException 

37from pyTooling.GenericPath.URL import URL 

38 

39 

40from pyVersioning.CIService import CIService, Platform, ServiceException 

41 

42 

43@export 

44class AppVeyor(CIService): 

45 """Collect Git and other platform and environment information from environment variables provided by AppVeyor.""" 

46 

47 ENV_INCLUDE_FILTER = ("APPVEYOR_", ) #: 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", "APPVEYOR", "PLATFORM", "CONFIGURATION") #: List of environment variable to include. 

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

51 

52 def GetPlatform(self) -> Platform: 

53 return Platform("appveyor") 

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 ``APPVEYOR_REPO_COMMIT`` was not found. 

61 """ 

62 try: 

63 return environ["APPVEYOR_REPO_COMMIT"] 

64 except KeyError as ex: 

65 raise ServiceException(f"Can't find AppVeyor environment variable 'APPVEYOR_REPO_COMMIT'.") from ex 

66 

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

68 """ 

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

70 

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

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

73 """ 

74 try: 

75 return environ["APPVEYOR_REPO_BRANCH"] 

76 except KeyError: 

77 return None 

78 

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

80 """ 

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

82 

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

84 :raises ServiceException: If environment variable ``APPVEYOR_REPO_TAG_NAME`` was not found. 

85 """ 

86 try: 

87 return environ["APPVEYOR_REPO_TAG_NAME"] 

88 except KeyError: 

89 return None 

90 

91 def GetGitRepository(self) -> str: 

92 """ 

93 Returns the Git repository URL. 

94 

95 :return: Git repository URL. 

96 :raises ServiceException: If environment variable ``APPVEYOR_PROJECT_SLUG`` was not found. 

97 :raises ServiceException: If repository URL from ``APPVEYOR_PROJECT_SLUG`` couldn't be parsed. 

98 """ 

99 try: 

100 repositoryURL = environ["APPVEYOR_PROJECT_SLUG"] 

101 except KeyError as ex: 

102 raise ServiceException(f"Can't find AppVeyor environment variable 'APPVEYOR_PROJECT_SLUG'.") from ex 

103 

104 try: 

105 url = URL.Parse(repositoryURL) 

106 except ToolingException as ex: 

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

108 

109 return str(url.WithoutCredentials())