gitutil.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #
  2. # Copyright 2018-2022 Elyra Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from enum import Enum
  17. from typing import List
  18. class SupportedGitTypes(Enum):
  19. GITHUB = "GitHub"
  20. GITLAB = "GitLab"
  21. @staticmethod
  22. def get_default_type() -> "SupportedGitTypes":
  23. """
  24. Returns the "default" enum member
  25. :return: default enum member
  26. :rtype: str
  27. """
  28. return SupportedGitTypes.GITHUB
  29. @staticmethod
  30. def get_enabled_types() -> List["SupportedGitTypes"]:
  31. """
  32. Returns all enabled types
  33. :return: List of enabled types
  34. :rtype: List[SupportedGitTypes]
  35. """
  36. enabled_types = [SupportedGitTypes.GITHUB]
  37. try:
  38. from elyra.util.gitlab import GitLabClient # noqa: F401
  39. enabled_types.append(SupportedGitTypes.GITLAB)
  40. except ImportError:
  41. pass # Gitlab package is not installed, ignore and use only GitHub
  42. return enabled_types
  43. @staticmethod
  44. def is_enabled(git_type: "SupportedGitTypes") -> bool:
  45. """
  46. :param git_type: A member of SupportedGitTypes
  47. :type git_type: SupportedGitTypes
  48. :return: True if git_type is enabled
  49. :rtype: bool
  50. """
  51. return git_type in SupportedGitTypes.get_enabled_types()
  52. @staticmethod
  53. def get_instance_by_name(name: str) -> "SupportedGitTypes":
  54. """
  55. Returns an enumeration member of SupportedGitTypes
  56. corresponding to the given name.
  57. :raises ValueError: name is not a valid enum member name
  58. :return: An enum member of SupportedGitTypes
  59. :rtype: SupportedGitTypes
  60. """
  61. try:
  62. return SupportedGitTypes[name]
  63. except KeyError:
  64. raise ValueError(f"'{name}' is not a valid {SupportedGitTypes.__name__}")