Skip to content

Commit 4b337c2

Browse files
committed
cr fixes
1 parent 5ea73d7 commit 4b337c2

7 files changed

Lines changed: 42 additions & 30 deletions

File tree

src/scripts/create_teams.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from src.shared.clients.jit import get_existing_teams, create_teams, list_assets, add_teams_to_asset, delete_teams, \
1111
get_jit_jwt_token
1212
from src.shared.diff_tools import get_different_items_in_lists
13-
from src.shared.models import Asset, TeamAttributes, Organization, TeamStructure
13+
from src.shared.models import Asset, TeamAttributes, Organization, TeamStructure, ResourceType
1414

1515
# Load environment variables from .env file.
1616
load_dotenv()
@@ -82,7 +82,8 @@ def update_assets(token, assets: List[Asset], organization):
8282
logger.info(f"Syncing team(s) {teams_to_update} to asset '{asset.asset_name}'")
8383
add_teams_to_asset(token, asset, teams_to_update)
8484
else:
85-
if asset.tags and "team" in [t.name for t in asset.tags]:
85+
asset_has_teams_tag = asset.tags and "team" in [t.name for t in asset.tags]
86+
if asset_has_teams_tag:
8687
logger.info(f"Removing all teams from asset '{asset.asset_name}'")
8788
add_teams_to_asset(token, asset, teams_to_update)
8889

@@ -131,7 +132,7 @@ def get_desired_teams(assets: List[Asset], organization: Organization) -> List[s
131132
for team in organization.teams:
132133
team_resources = []
133134
for resource in team.resources:
134-
if resource.type == "github_repo" and resource.name in [asset.asset_name for asset in assets]:
135+
if resource.type == ResourceType.GithubRepo and resource.name in [asset.asset_name for asset in assets]:
135136
team_resources.append(resource.name)
136137
if team_resources:
137138
desired_teams.append(team.name)
@@ -189,7 +190,7 @@ def get_teams_for_assets(organization: Organization) -> Dict[str, List[str]]:
189190
asset_to_team_map = {}
190191
for team in organization.teams:
191192
for resource in team.resources:
192-
if resource.type == "github_repo":
193+
if resource.type == ResourceType.GithubRepo:
193194
asset_name = resource.name
194195
if asset_name in asset_to_team_map:
195196
asset_to_team_map[asset_name].append(team.name)

src/shared/clients/github.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from github import Github
44
from loguru import logger
55

6-
from src.shared.models import TeamStructure, Resource, Organization
6+
from src.shared.models import TeamStructure, Resource, Organization, ResourceType
77

88

99
def get_teams_from_github_topics() -> Organization:
@@ -33,11 +33,11 @@ def get_teams_from_github_topics() -> Organization:
3333
# Check if the topic already exists in the teams dictionary
3434
if topic in teams:
3535
# Add the repository to the existing team
36-
teams[topic].resources.append(Resource(type="github_repo", name=repo_name))
36+
teams[topic].resources.append(Resource(type=ResourceType.GithubRepo, name=repo_name))
3737
else:
3838
# Create a new team template for the topic
3939
team_template = TeamStructure(name=topic, members=[],
40-
resources=[Resource(type="github_repo", name=repo_name)])
40+
resources=[Resource(type=ResourceType.GithubRepo, name=repo_name)])
4141

4242
# Add the team template to the teams dictionary
4343
teams[topic] = team_template

src/shared/clients/jit.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from loguru import logger
77

88
from src.shared.consts import JIT_DEFAULT_API_ENDPOINT
9+
from src.shared.env_tools import get_jit_endpoint_base_url
910
from src.shared.models import Asset, TeamAttributes
1011

1112

@@ -15,7 +16,7 @@ def get_jit_jwt_token() -> Optional[str]:
1516
"secret": os.getenv('JIT_CLIENT_SECRET')
1617
}
1718

18-
response = requests.post(f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/authentication/login",
19+
response = requests.post(f"{get_jit_endpoint_base_url()}/authentication/login",
1920
json=payload)
2021

2122
if response.status_code == 200:
@@ -28,7 +29,7 @@ def get_jit_jwt_token() -> Optional[str]:
2829
def list_assets(token: str) -> List[Asset]:
2930
try:
3031
# Make a GET request to the asset API
31-
url = f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/asset"
32+
url = f"{get_jit_endpoint_base_url()}/asset"
3233
headers = {
3334
"Authorization": f"Bearer {token}"
3435
}
@@ -59,7 +60,7 @@ def _handle_resoponse(response, existing_teams):
5960

6061
try:
6162
# Make a GET request to the asset API
62-
url = f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/teams?limit=100"
63+
url = f"{get_jit_endpoint_base_url()}/teams?limit=100"
6364

6465
headers = get_request_headers(token)
6566
response = requests.get(url, headers=headers)
@@ -96,7 +97,7 @@ def delete_teams(token, team_names):
9697
break
9798

9899
if team_id:
99-
url = f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/teams/{team_id}"
100+
url = f"{get_jit_endpoint_base_url()}/teams/{team_id}"
100101
headers = {"Authorization": f"Bearer {token}"}
101102

102103
response = requests.delete(url, headers=headers)
@@ -112,7 +113,7 @@ def delete_teams(token, team_names):
112113

113114
def create_teams(token, teams_to_create):
114115
try:
115-
url = f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/teams/"
116+
url = f"{get_jit_endpoint_base_url()}/teams/"
116117
headers = get_request_headers(token)
117118
for team_name in teams_to_create:
118119
payload = {
@@ -137,7 +138,7 @@ def get_request_headers(token):
137138

138139
def add_teams_to_asset(token, asset: Asset, teams: List[str]):
139140
try:
140-
url = f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/asset/asset/{asset.asset_id}"
141+
url = f"{get_jit_endpoint_base_url()}/asset/asset/{asset.asset_id}"
141142
headers = get_request_headers(token)
142143
payload = {
143144
"teams": teams

src/shared/env_tools.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import os
2+
3+
from src.shared.consts import JIT_DEFAULT_API_ENDPOINT
4+
5+
6+
def get_jit_endpoint_base_url() -> str:
7+
return os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)

src/shared/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
from enum import Enum
12
from typing import Optional, List
23

34
from pydantic import BaseModel
4-
55
from src.shared.consts import MANUAL_TEAM_SOURCE
66

77

8+
class ResourceType(str, Enum):
9+
GithubRepo = 'github_repo'
10+
11+
812
class TeamAttributes(BaseModel):
913
tenant_id: str
1014
id: str

tests/factories.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from faker import Faker
44
from polyfactory.factories.pydantic_factory import ModelFactory
55
from src.shared.consts import MANUAL_TEAM_SOURCE
6-
from src.shared.models import TeamAttributes, TeamStructure, Asset, Organization, Resource
6+
from src.shared.models import TeamAttributes, TeamStructure, Asset, Organization, Resource, ResourceType
77

88
locales = OrderedDict([
99
('en-US', 1),
@@ -29,7 +29,7 @@ class TeamAttributesFactory(ModelFactory):
2929

3030
class ResourceFactory(ModelFactory):
3131
__model__ = Resource
32-
type = "github_repo"
32+
type = ResourceType.GithubRepo
3333
name = fake.word
3434

3535

tests/shared/test_clients.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import os
2-
31
import pytest
4-
52
from src.shared.clients.github import get_teams_from_github_topics
63
from src.shared.clients.jit import list_assets, get_existing_teams, create_teams, add_teams_to_asset, delete_teams, \
74
get_jit_jwt_token
8-
from src.shared.consts import JIT_DEFAULT_API_ENDPOINT
9-
from src.shared.models import TeamAttributes, Asset, Organization, TeamStructure, Resource
5+
from src.shared.env_tools import get_jit_endpoint_base_url
6+
from src.shared.models import TeamAttributes, Asset, Organization, TeamStructure, Resource, ResourceType
107

118

129
class MockRepo:
@@ -25,20 +22,22 @@ def get_topics(self):
2522
([MockRepo("repo1", [])], Organization(teams=[])),
2623
([MockRepo("repo1", ["topic1"]), MockRepo("repo2", ["topic2"])],
2724
Organization(teams=[
28-
TeamStructure(name="topic1", members=[], resources=[Resource(type="github_repo", name="repo1")]),
29-
TeamStructure(name="topic2", members=[], resources=[Resource(type="github_repo", name="repo2")])
25+
TeamStructure(name="topic1", members=[],
26+
resources=[Resource(type=ResourceType.GithubRepo, name="repo1")]),
27+
TeamStructure(name="topic2", members=[],
28+
resources=[Resource(type=ResourceType.GithubRepo, name="repo2")])
3029
])),
3130
([MockRepo("repo1", ["topic1"]), MockRepo("repo2", ["topic2"]), MockRepo("repo3", ["topic2"]),
3231
MockRepo("repo4", ["topic1", "topic2"])],
3332
Organization(teams=[
3433
TeamStructure(name="topic1", members=[], resources=[
35-
Resource(type="github_repo", name="repo1"),
36-
Resource(type="github_repo", name="repo4")
34+
Resource(type=ResourceType.GithubRepo, name="repo1"),
35+
Resource(type=ResourceType.GithubRepo, name="repo4")
3736
]),
3837
TeamStructure(name="topic2", members=[], resources=[
39-
Resource(type="github_repo", name="repo2"),
40-
Resource(type="github_repo", name="repo3"),
41-
Resource(type="github_repo", name="repo4")
38+
Resource(type=ResourceType.GithubRepo, name="repo2"),
39+
Resource(type=ResourceType.GithubRepo, name="repo3"),
40+
Resource(type=ResourceType.GithubRepo, name="repo4")
4241
])
4342
])),
4443
]
@@ -93,7 +92,7 @@ def test_get_jwt_token(status_code, expected_result, mocker):
9392
token = get_jit_jwt_token()
9493

9594
requests_post_mock.assert_called_once_with(
96-
f"{os.getenv('JIT_API_ENDPOINT', JIT_DEFAULT_API_ENDPOINT)}/authentication/login",
95+
f"{get_jit_endpoint_base_url()}/authentication/login",
9796
json={"clientId": None, "secret": None}
9897
)
9998
assert token == expected_result
@@ -205,7 +204,7 @@ def test_delete_teams(mocker, status_code, existing_team_names, input_team_names
205204
expected_warning):
206205
mock_existing_teams = [
207206
TeamAttributes(tenant_id=f"tenant{i + 1}", id=str(i + 1), created_at=f"date{i + 1}", modified_at=f"date{i + 2}",
208-
name=team_name)
207+
name=team_name)
209208
for i, team_name in enumerate(existing_team_names)
210209
]
211210

0 commit comments

Comments
 (0)