11import re
22
33from abc import ABC , abstractmethod
4- from argparse import Namespace
4+ from git import Repo
5+
6+ from src .utils .github_environment_variables import GitHubContext
57
68
79class EnvironmentSelectorInterface (ABC ):
810 @abstractmethod
9- def select_environment (self , branch_name : str ) -> str :
11+ def select_environment (self , github_context : GitHubContext ) -> str :
1012 pass
1113
1214
@@ -26,25 +28,29 @@ class EnvironmentSelector(EnvironmentSelectorInterface):
2628 SELECT_ALL_BRANCHES = fr"{ SELECT_FEATURE_BRANCHES } |{ SELECT_RELEASE_BRANCHES } "
2729 SELECT_ORIGIN_ALL_BRANCHES = fr"{ SELECT_ORIGIN_FEATURE_BRANCHES } |{ SELECT_ORIGIN_RELEASE_BRANCHES } "
2830
29- def select_environment (self , branch_name : str ) -> str :
30- if re .match (r"^(develop|staging|production)$" , branch_name , re .IGNORECASE ):
31- return branch_name
31+ def select_environment (self , github_context : GitHubContext ) -> str :
32+ if re .match (r"^(develop|staging|production)$" , github_context . ref_name , re .IGNORECASE ):
33+ return github_context . ref_name
3234
33- if re .match (EnvironmentSelector .SELECT_FEATURE_BRANCHES , branch_name , re .IGNORECASE ):
35+ if re .match (EnvironmentSelector .SELECT_FEATURE_BRANCHES , github_context . ref_name , re .IGNORECASE ):
3436 return "develop"
3537
36- if re .match (EnvironmentSelector .SELECT_RELEASE_BRANCHES , branch_name , re .IGNORECASE ):
37- if re .search (EnvironmentSelector .SEMVER_REGEXP , branch_name , re .IGNORECASE ):
38- if "-rc" in branch_name :
38+ if re .match (EnvironmentSelector .SELECT_RELEASE_BRANCHES , github_context . ref_name , re .IGNORECASE ):
39+ if re .search (EnvironmentSelector .SEMVER_REGEXP , github_context . ref_name , re .IGNORECASE ):
40+ if "-rc" in github_context . ref_name :
3941 return "staging"
4042 return "production"
4143 return "staging"
4244
43- raise ValueError (f"environment '{ branch_name } ' not allowed for environment selection" )
45+ raise ValueError (f"environment '{ github_context . ref_name } ' not allowed for environment selection" )
4446
4547
4648class ExtendedEnvironmentSelector (EnvironmentSelector ):
47- def select_environment (self , branch_name : str ) -> str :
48- if branch_name .startswith ("hotfix/" ):
49+ def select_environment (self , github_context : GitHubContext ) -> str :
50+ if github_context .event_name == "pull_request" :
51+ repo = Repo ("." )
52+ github_context .ref_name = repo .active_branch .name
53+
54+ if github_context .ref_name .startswith ("hotfix/" ):
4955 return "production"
50- return super ().select_environment (branch_name )
56+ return super ().select_environment (github_context )
0 commit comments