2121logger .add (sys .stderr , format = logger_format )
2222
2323
24- def parse_input_file () -> Organization :
24+ def parse_input_file () -> Tuple [ Organization , bool , bool ] :
2525 """
26- Parse the input JSON file and return an Organization object.
26+ Parse the input JSON file and return an Organization object, the skip_no_resources flag,
27+ and the verify_github_membership flag.
2728
2829 Returns:
29- Organization: The parsed organization object.
30+ Tuple[Organization, bool, bool]: The parsed organization object, the skip_no_resources flag,
31+ and the verify_github_membership flag.
3032 """
31- # Create the argument parser
3233 parser = argparse .ArgumentParser (description = "Retrieve teams and assets" )
33-
34- # Add the file argument
3534 parser .add_argument ("file" , help = "Path to a JSON file" )
3635
37- # Parse the command line arguments
36+ # Default behavior is True, override with --no-skip-no-resources and --no-verify-github-membership
37+ parser .add_argument ("--skip-no-resources" , dest = 'skip_no_resources' , action = "store_true" ,
38+ help = "Skip teams with no active resources" , default = True )
39+ parser .add_argument ("--no-skip-no-resources" , dest = 'skip_no_resources' , action = "store_false" ,
40+ help = "Do not skip teams with no active resources" )
41+
42+ parser .add_argument ("--verify-github-membership" , dest = 'verify_github_membership' , action = "store_true" ,
43+ help = "Verify GitHub membership when setting team members" , default = True )
44+ parser .add_argument ("--no-verify-github-membership" , dest = 'verify_github_membership' , action = "store_false" ,
45+ help = "Do not verify GitHub membership when setting team members" )
46+
3847 args = parser .parse_args ()
3948
40- # Check if the file exists and is a JSON file
4149 if not os .path .isfile (args .file ):
4250 logger .error ("Error: File does not exist." )
4351 sys .exit (1 )
@@ -46,14 +54,13 @@ def parse_input_file() -> Organization:
4654 sys .exit (1 )
4755 logger .info (f"Reading file: { args .file } " )
4856
49- # Read the JSON file
5057 with open (args .file , "r" ) as file :
5158 json_data = file .read ()
52-
53- # Parse the JSON data
59+ logger .info (f"JSON data: { json_data } " )
5460 try :
5561 data = json .loads (json_data )
56- return Organization (teams = [TeamStructure (** team ) for team in data ["teams" ]])
62+ return Organization (teams = [TeamStructure (** team ) for team in data ["teams" ]]), \
63+ args .skip_no_resources , args .verify_github_membership
5764 except (ValidationError , KeyError ) as e :
5865 logger .error (f"Failed to validate input file: { e } " )
5966 sys .exit (1 )
@@ -125,14 +132,15 @@ def get_teams_to_delete(topic_names: List[str], existing_team_names: List[str])
125132 return get_different_items_in_lists (existing_team_names , topic_names )
126133
127134
128- def get_desired_teams (assets : List [Asset ], organization : Organization ) -> List [str ]:
135+ def get_desired_teams (assets : List [Asset ], organization : Organization , skip_no_resources : bool ) -> List [str ]:
129136 """
130137 Get the desired teams based on the assets and organization.
131138 Also filter out teams that match the TEAM_WILDCARD_TO_EXCLUDE environment variable.
132139
133140 Args:
134141 assets (List[Asset]): The list of assets.
135142 organization (Organization): The organization object.
143+ skip_no_resources (bool): Whether to skip teams with no active resources.
136144
137145 Returns:
138146 List[str]: The names of the desired teams.
@@ -143,7 +151,7 @@ def get_desired_teams(assets: List[Asset], organization: Organization) -> List[s
143151 for resource in team .resources :
144152 if resource .type == ResourceType .GithubRepo and resource .name in [asset .asset_name for asset in assets ]:
145153 team_resources .append (resource .name )
146- if team_resources :
154+ if team_resources or not skip_no_resources :
147155 desired_teams .append (team .name )
148156 else :
149157 logger .info (
@@ -165,7 +173,8 @@ def get_desired_teams(assets: List[Asset], organization: Organization) -> List[s
165173
166174
167175def process_teams (token , organization , assets : List [Asset ],
168- existing_teams : List [TeamAttributes ]) -> Tuple [List [str ], List [TeamAttributes ]]:
176+ existing_teams : List [TeamAttributes ],
177+ skip_no_resources : bool ) -> Tuple [List [str ], List [TeamAttributes ]]:
169178 """
170179 Process the teams in the organization and create or delete teams as necessary.
171180 We will delete the teams at a later stage to avoid possible synchronization issues.
@@ -174,13 +183,13 @@ def process_teams(token, organization, assets: List[Asset],
174183 token (str): The JWT token.
175184 organization (Organization): The organization object.
176185 existing_teams (List[TeamAttributes]): The existing teams.
177-
186+ skip_no_resources (bool): Whether to skip teams with no active resources.
178187 Returns:
179188 Tuple[List[str], List[TeamAttributes]]: The names of the teams to delete and the created teams.
180189 """
181190 logger .info ("Determining required changes in teams." )
182191
183- desired_teams = get_desired_teams (assets , organization )
192+ desired_teams = get_desired_teams (assets , organization , skip_no_resources )
184193 existing_team_names = [team .name for team in existing_teams ]
185194 teams_to_create = get_teams_to_create (desired_teams , existing_team_names )
186195 teams_to_delete = get_teams_to_delete (desired_teams , existing_team_names )
@@ -193,7 +202,7 @@ def process_teams(token, organization, assets: List[Asset],
193202
194203
195204def process_members (token : str , organization : Organization , existing_teams : List [TeamAttributes ],
196- desired_teams : List [str ]) -> None :
205+ desired_teams : List [str ], verify_github_membership : bool ) -> None :
197206 logger .info ("Processing team members." )
198207 for team_structure in organization .teams :
199208 try :
@@ -210,7 +219,7 @@ def process_members(token: str, organization: Organization, existing_teams: List
210219 f"Only the first { MAX_MEMBERS_PER_TEAM } members will be set." )
211220 team_members = team_members [:MAX_MEMBERS_PER_TEAM ]
212221 set_manual_team_members (
213- token , team_id , team_members , team_name )
222+ token , team_id , team_members , team_name , verify_github_membership )
214223 else :
215224 logger .warning (
216225 f"Team '{ team_name } ' not found in existing teams. Skipping member processing." )
@@ -248,19 +257,24 @@ def main():
248257 logger .error ("Failed to retrieve JWT token. Exiting..." )
249258 return
250259
251- organization : Organization = parse_input_file ()
260+ organization , skip_no_resources , verify_github_membership = parse_input_file ()
261+ logger .info (
262+ f"Running with { skip_no_resources = } , { verify_github_membership = } " )
252263 if not organization :
253264 logger .error ("Failed to parse input file. Exiting..." )
254265 return
255266
256267 assets : List [Asset ] = list_assets (jit_token )
257268
258269 existing_teams = get_existing_teams (jit_token )
270+ logger .info (
271+ f"Found { len (existing_teams )} existing team(s) in the organization: { [team .name for team in existing_teams ]} " )
259272 teams_to_delete , created_teams = process_teams (
260- jit_token , organization , assets , existing_teams )
273+ jit_token , organization , assets , existing_teams , skip_no_resources )
261274 existing_teams : List [TeamAttributes ] = existing_teams + created_teams
262- desired_teams = get_desired_teams (assets , organization )
263- process_members (jit_token , organization , existing_teams , desired_teams )
275+ desired_teams = get_desired_teams (assets , organization , skip_no_resources )
276+ process_members (jit_token , organization , existing_teams ,
277+ desired_teams , verify_github_membership )
264278 update_assets (jit_token , assets , organization , existing_teams )
265279
266280 if teams_to_delete :
0 commit comments