Skip to content

Commit 9328620

Browse files
feat: included project id in project specific files (#736)
1 parent 6e71f63 commit 9328620

File tree

8 files changed

+63
-37
lines changed

8 files changed

+63
-37
lines changed

safety/init/command.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,13 +645,10 @@ def do_init(
645645
console.line()
646646

647647
if ask_codebase_setup(ctx, prompt_user):
648-
configure_local_directory(
649-
project_dir,
650-
org_slug,
651-
)
652-
653648
project_created, project_status = create_project(ctx, console, project_dir)
654649

650+
configure_local_directory(project_dir, org_slug, ctx.obj.project.id)
651+
655652
emit_codebase_setup_completed(
656653
event_bus=ctx.obj.event_bus,
657654
ctx=ctx,

safety/tool/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
PUBLIC_REPOSITORY_URL = "https://pkgs.safetycli.com/repository/public/pypi/simple/"
22
ORGANIZATION_REPOSITORY_URL = "https://pkgs.safetycli.com/repository/{}/pypi/simple/"
3+
PROJECT_REPOSITORY_URL = (
4+
"https://pkgs.safetycli.com/repository/{}/project/{}/pypi/simple/"
5+
)
36
PROJECT_CONFIG = ".safety-project.ini"
47

58
MOST_FREQUENTLY_DOWNLOADED_PYPI_PACKAGES = [

safety/tool/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ def configure_alias() -> Optional[List[Tuple[ToolType, Optional[Path]]]]:
8282
return [(ToolType.PIP, None), (ToolType.POETRY, None), (ToolType.UV, None)]
8383

8484

85-
def configure_local_directory(directory: Path, org_slug: Optional[str]):
85+
def configure_local_directory(
86+
directory: Path, org_slug: Optional[str], project_id: Optional[str]
87+
):
8688
configurators = [
8789
PipRequirementsConfigurator(),
8890
PoetryPyprojectConfigurator(),
@@ -94,4 +96,4 @@ def configure_local_directory(directory: Path, org_slug: Optional[str]):
9496
file = Path(file_name)
9597
for configurator in configurators:
9698
if configurator.is_supported(file):
97-
configurator.configure(file, org_slug)
99+
configurator.configure(file, org_slug, project_id)

safety/tool/pip/main.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import typer
1212
from rich.console import Console
1313

14-
from safety.tool.constants import PUBLIC_REPOSITORY_URL, ORGANIZATION_REPOSITORY_URL
14+
from safety.tool.constants import (
15+
PUBLIC_REPOSITORY_URL,
16+
ORGANIZATION_REPOSITORY_URL,
17+
PROJECT_REPOSITORY_URL,
18+
)
1519
from safety.tool.resolver import get_unwrapped_command
1620

1721
from safety.console import main_console
@@ -36,6 +40,7 @@ def configure_requirements(
3640
cls,
3741
file: Path,
3842
org_slug: Optional[str],
43+
project_id: Optional[str],
3944
console: Console = main_console,
4045
) -> Optional[Path]:
4146
"""
@@ -44,16 +49,21 @@ def configure_requirements(
4449
Args:
4550
file (Path): Path to requirements.txt file.
4651
org_slug (str): Organization slug.
52+
project_id (str): Project identifier.
4753
console (Console): Console instance.
4854
"""
4955

5056
with open(file, "r+") as f:
5157
content = f.read()
5258

5359
repository_url = (
54-
ORGANIZATION_REPOSITORY_URL.format(org_slug)
55-
if org_slug
56-
else PUBLIC_REPOSITORY_URL
60+
PROJECT_REPOSITORY_URL.format(org_slug, project_id)
61+
if project_id and org_slug
62+
else (
63+
ORGANIZATION_REPOSITORY_URL.format(org_slug)
64+
if org_slug
65+
else PUBLIC_REPOSITORY_URL
66+
)
5767
)
5868
index_config = f"-i {repository_url}\n"
5969
if content.find(index_config) == -1:

safety/tool/poetry/command.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,3 @@ def before(self, ctx: typer.Context):
154154

155155
_, modified_args = self.patch_source_option(self._args)
156156
self._args = modified_args
157-
print(self._args)

safety/tool/poetry/main.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
from typing import Optional
77

88
from rich.console import Console
9-
import urllib.parse
109

1110
from safety.console import main_console
12-
from safety.tool.constants import PUBLIC_REPOSITORY_URL, ORGANIZATION_REPOSITORY_URL
11+
from safety.tool.constants import (
12+
PUBLIC_REPOSITORY_URL,
13+
ORGANIZATION_REPOSITORY_URL,
14+
PROJECT_REPOSITORY_URL,
15+
)
1316
from safety.tool.resolver import get_unwrapped_command
1417

1518
if sys.version_info >= (3, 11):
@@ -64,14 +67,14 @@ def configure_pyproject(
6467
return None
6568

6669
repository_url = (
67-
ORGANIZATION_REPOSITORY_URL.format(org_slug)
68-
if org_slug
69-
else PUBLIC_REPOSITORY_URL
70-
)
71-
if project_id:
72-
repository_url = repository_url + urllib.parse.urlencode(
73-
{"project-id": project_id}
70+
PROJECT_REPOSITORY_URL.format(org_slug, project_id)
71+
if project_id and org_slug
72+
else (
73+
ORGANIZATION_REPOSITORY_URL.format(org_slug)
74+
if org_slug
75+
else PUBLIC_REPOSITORY_URL
7476
)
77+
)
7578
result = subprocess.run(
7679
[
7780
get_unwrapped_command(name="poetry"),

safety/tool/utils.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ def is_supported(self, file: Path) -> bool:
3434
pass
3535

3636
@abc.abstractmethod
37-
def configure(self, file: Path, org_slug: Optional[str]) -> Optional[Path]:
37+
def configure(
38+
self, file: Path, org_slug: Optional[str], project_id: Optional[str]
39+
) -> Optional[Path]:
3840
"""
3941
Configures specific file.
4042
Args:
4143
file (str): The file to configure.
4244
org_slug (str): The organization slug.
45+
project_id (str): The project identifier.
4346
"""
4447
pass
4548

@@ -50,8 +53,10 @@ class PipRequirementsConfigurator(BuildFileConfigurator):
5053
def is_supported(self, file: Path) -> bool:
5154
return self.__file_name_pattern.match(os.path.basename(file)) is not None
5255

53-
def configure(self, file: Path, org_slug: Optional[str]) -> None:
54-
Pip.configure_requirements(file, org_slug) # type: ignore
56+
def configure(
57+
self, file: Path, org_slug: Optional[str], project_id: Optional[str]
58+
) -> None:
59+
Pip.configure_requirements(file, org_slug, project_id)
5560

5661

5762
class PoetryPyprojectConfigurator(BuildFileConfigurator):
@@ -62,9 +67,11 @@ def is_supported(self, file: Path) -> bool:
6267
os.path.basename(file)
6368
) is not None and Poetry.is_poetry_project_file(file)
6469

65-
def configure(self, file: Path, org_slug: Optional[str]) -> Optional[Path]:
70+
def configure(
71+
self, file: Path, org_slug: Optional[str], project_id: Optional[str]
72+
) -> Optional[Path]:
6673
if self.is_supported(file):
67-
return Poetry.configure_pyproject(file, org_slug) # type: ignore
74+
return Poetry.configure_pyproject(file, org_slug, project_id) # type: ignore
6875
return None
6976

7077

@@ -124,7 +131,9 @@ def is_supported(self, file: Path) -> bool:
124131
and Path("pyproject.toml").exists()
125132
)
126133

127-
def configure(self, file: Path, org_slug: Optional[str]) -> Optional[Path]:
134+
def configure(
135+
self, file: Path, org_slug: Optional[str], project_id: Optional[str]
136+
) -> Optional[Path]:
128137
if self.is_supported(file):
129-
return Uv.configure_pyproject(Path("pyproject.toml"), org_slug)
138+
return Uv.configure_pyproject(Path("pyproject.toml"), org_slug, project_id)
130139
return None

safety/tool/uv/main.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import shutil
55
import sys
66
from typing import Any, Dict, Optional
7-
import urllib.parse
87
import tomlkit
98

109
from rich.console import Console
1110
from safety.console import main_console
12-
from safety.tool.constants import ORGANIZATION_REPOSITORY_URL, PUBLIC_REPOSITORY_URL
11+
from safety.tool.constants import (
12+
ORGANIZATION_REPOSITORY_URL,
13+
PUBLIC_REPOSITORY_URL,
14+
PROJECT_REPOSITORY_URL,
15+
)
1316

1417
if sys.version_info >= (3, 11):
1518
import tomllib
@@ -72,14 +75,14 @@ def configure_pyproject(
7275
return None
7376

7477
repository_url = (
75-
ORGANIZATION_REPOSITORY_URL.format(org_slug)
76-
if org_slug
77-
else PUBLIC_REPOSITORY_URL
78-
)
79-
if project_id:
80-
repository_url = repository_url + urllib.parse.urlencode(
81-
{"project-id": project_id}
78+
PROJECT_REPOSITORY_URL.format(org_slug, project_id)
79+
if project_id and org_slug
80+
else (
81+
ORGANIZATION_REPOSITORY_URL.format(org_slug)
82+
if org_slug
83+
else PUBLIC_REPOSITORY_URL
8284
)
85+
)
8386
try:
8487
content = file.read_text()
8588
doc: Dict[str, Any] = tomlkit.loads(content)

0 commit comments

Comments
 (0)