Skip to content

Commit a15c816

Browse files
committed
add ability to exclude topic wildcards
1 parent e38c456 commit a15c816

5 files changed

Lines changed: 26 additions & 4 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ configure:
1313
read -p "Enter JIT API client ID: " client_id; \
1414
read -p "Enter JIT API client secret: " client_secret; \
1515
read -p "Enter GitHub Personal token (PAT): " github_token; \
16+
read -p "Enter topic wildcards to exclude(example: *dev*): " topics_to_exclude; \
1617
echo "ORGANIZATION_NAME=$$org_name" > .env; \
1718
echo "JIT_CLIENT_ID=$$client_id" >> .env; \
1819
echo "JIT_CLIENT_SECRET=$$client_secret" >> .env; \
1920
echo "GITHUB_API_TOKEN=$$github_token" >> .env
21+
echo "TEAM_WILDCARD_TO_EXCLUDE=topics_to_exclude" >> .env
2022

2123
create-teams:
2224
source venv-jit/bin/activate && \

src/scripts/create_teams.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def parse_input_file() -> Organization:
5757
sys.exit(1)
5858

5959

60-
def update_assets(token, assets, organization):
60+
def update_assets(token, assets: List[Asset], organization):
6161
"""
6262
Update the assets with the teams specified in the organization.
6363
@@ -71,11 +71,20 @@ def update_assets(token, assets, organization):
7171
logger.info("Updating assets.")
7272

7373
asset_to_team_map = get_teams_for_assets(organization)
74+
existing_teams: List[str] = [t.name for t in get_existing_teams(token)]
7475
for asset in assets:
7576
teams_to_update = asset_to_team_map.get(asset.asset_name, [])
7677
if teams_to_update:
78+
excluded_teams = get_different_items_in_lists(teams_to_update, existing_teams)
79+
if excluded_teams:
80+
logger.info(f"Excluding team(s) {excluded_teams} for asset '{asset.asset_name}'")
81+
teams_to_update = list(set(teams_to_update) - set(excluded_teams))
7782
logger.info(f"Syncing team(s) {teams_to_update} to asset '{asset.asset_name}'")
7883
add_teams_to_asset(token, asset, teams_to_update)
84+
else:
85+
if asset.tags and "team" in [t.name for t in asset.tags]:
86+
logger.info(f"Removing all teams from asset '{asset.asset_name}'")
87+
add_teams_to_asset(token, asset, teams_to_update)
7988

8089

8190
def get_teams_to_create(topic_names: List[str], existing_team_names: List[str]) -> List[str]:
@@ -132,6 +141,7 @@ def get_desired_teams(assets: List[Asset], organization: Organization) -> List[s
132141
for team_name in desired_teams:
133142
exclude_team = False
134143
for wildcard in wildcards_to_exclude:
144+
wildcard = wildcard.strip().strip("*")
135145
if wildcard and wildcard in team_name:
136146
exclude_team = True
137147
break

src/shared/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class TeamStructure(BaseModel):
2929
resources: List[Resource] = []
3030

3131

32+
class Tag(BaseModel):
33+
name: str
34+
value: str
35+
36+
3237
class Asset(BaseModel):
3338
asset_id: str
3439
tenant_id: str
@@ -41,6 +46,7 @@ class Asset(BaseModel):
4146
is_archived: Optional[bool] = False
4247
created_at: str
4348
modified_at: str
49+
tags: Optional[List[Tag]] = []
4450

4551

4652
class Organization(BaseModel):

tests/scripts/test_create_teams.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def test_parse_input_file__with_invalid_json(invalid_file, json_data, should_rai
8080

8181
@pytest.fixture
8282
def data():
83+
Faker.seed(10)
8384
num_objects = 10
8485
organization = Organization(teams=TeamStructureFactory.batch(num_objects))
8586
assets = AssetFactory.batch(num_objects, asset_name=fake.word())
@@ -95,7 +96,7 @@ def data():
9596
@pytest.mark.parametrize(
9697
"label, existing_teams_indexes, asset_indexes, len_expected_teams_to_delete",
9798
[
98-
("No teams to create and no teams to delete", [], [], 0),
99+
("No teams no assets", [], [], 0),
99100
("No teams to create and no teams to delete", "all", "all", 0),
100101
("No teams to create and teams to delete", "all", [0, 5], 8),
101102
("Teams to create and no teams to delete", [0, 5], "all", 0),

tests/shared/test_clients.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import os
2+
13
import pytest
24

35
from src.shared.clients.github import get_teams_from_github_topics
46
from src.shared.clients.jit import list_assets, get_existing_teams, create_teams, add_teams_to_asset, delete_teams, \
5-
get_jit_jwt_token, JIT_API_ENDPOINT
7+
get_jit_jwt_token
8+
from src.shared.consts import JIT_DEFAULT_API_ENDPOINT
69
from src.shared.models import TeamAttributes, Asset, Organization, TeamStructure, Resource
710

811

@@ -90,7 +93,7 @@ def test_get_jwt_token(status_code, expected_result, mocker):
9093
token = get_jit_jwt_token()
9194

9295
requests_post_mock.assert_called_once_with(
93-
f"{JIT_API_ENDPOINT}/authentication/login",
96+
f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/authentication/login",
9497
json={"clientId": None, "secret": None},
9598
headers={"accept": "application/json", "content-type": "application/json"}
9699
)

0 commit comments

Comments
 (0)