Skip to content

Commit 4eead06

Browse files
committed
about to refactor Package
1 parent 446cf4e commit 4eead06

File tree

13 files changed

+217
-42
lines changed

13 files changed

+217
-42
lines changed

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
FROM python:3-alpine
22
LABEL org.opencontainers.image.authors="socket.dev"
33
ARG CLI_VERSION
4+
ARG SDK_VERSION
45
ARG PIP_INDEX_URL=https://pypi.org/simple
56
ARG PIP_EXTRA_INDEX_URL=https://pypi.org/simple
67

78
RUN apk update \
89
&& apk add --no-cache git nodejs npm yarn
910

11+
# Install CLI first
1012
RUN pip install --index-url ${PIP_INDEX_URL} --extra-index-url ${PIP_EXTRA_INDEX_URL} socketsecurity==$CLI_VERSION \
11-
&& socketcli -v \
12-
&& socketcli -v | grep -q $CLI_VERSION
13+
# Then override SDK version
14+
&& pip install --index-url ${PIP_INDEX_URL} --extra-index-url ${PIP_EXTRA_INDEX_URL} socket-sdk-python==${SDK_VERSION:-latest}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dependencies = [
1313
'GitPython',
1414
'packaging',
1515
'python-dotenv',
16-
'socket-sdk-python>=2.0.2'
16+
'socket-sdk-python>=2.0.4'
1717
]
1818
readme = "README.md"
1919
description = "Socket Security CLI for CI/CD"

scripts/build_container.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ if [ $ENABLE_PYPI_BUILD = "pypi-build=test" ]; then
2626
python -m build --wheel --sdist
2727
twine upload --repository testpypi dist/*$VERSION*
2828
sleep 120
29-
docker build --no-cache --build-arg CLI_VERSION=$VERSION --platform linux/amd64,linux/arm64 -t socketdev/cli:$VERSION-test . \
30-
&& docker build --no-cache --build-arg CLI_VERSION=$VERSION --platform linux/amd64,linux/arm64 -t socketdev/cli:test . \
29+
docker build --no-cache \
30+
--build-arg CLI_VERSION=$VERSION \
31+
--build-arg PIP_INDEX_URL=https://test.pypi.org/simple \
32+
--build-arg PIP_EXTRA_INDEX_URL=https://pypi.org/simple \
33+
--platform linux/amd64,linux/arm64 \
34+
-t socketdev/cli:$VERSION-test . \
35+
&& docker build --no-cache \
36+
--build-arg CLI_VERSION=$VERSION \
37+
--build-arg PIP_INDEX_URL=https://test.pypi.org/simple \
38+
--build-arg PIP_EXTRA_INDEX_URL=https://pypi.org/simple \
39+
--platform linux/amd64,linux/arm64 \
40+
-t socketdev/cli:test . \
3141
&& docker push socketdev/cli:$VERSION-test \
3242
&& docker push socketdev/cli:test
3343
fi

scripts/deploy-test-docker.sh

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/sh
2+
3+
CLI_VERSION=$1
4+
SDK_VERSION=$2
5+
6+
get_latest_version() {
7+
package=$1
8+
curl -s https://test.pypi.org/pypi/$package/json | python -c "
9+
import sys, json
10+
data = json.load(sys.stdin)
11+
versions = list(data.get('releases', {}).keys())
12+
versions.sort(key=lambda x: (
13+
x.split('.dev')[0],
14+
int(x.split('.dev')[1]) if '.dev' in x else 0
15+
))
16+
print(versions[-1] if versions else '')
17+
"
18+
}
19+
20+
if [ -z "$CLI_VERSION" ]; then
21+
echo "No CLI version specified, checking TestPyPI for latest version..."
22+
CLI_VERSION=$(get_latest_version "socketsecurity")
23+
echo "Latest CLI version on TestPyPI is: $CLI_VERSION"
24+
fi
25+
26+
if [ -z "$SDK_VERSION" ]; then
27+
echo "No SDK version specified, checking TestPyPI for latest version..."
28+
SDK_VERSION=$(get_latest_version "socket-sdk-python")
29+
echo "Latest SDK version on TestPyPI is: $SDK_VERSION"
30+
fi
31+
32+
echo -n "Deploy with CLI=$CLI_VERSION and SDK=$SDK_VERSION? (y/n): "
33+
read answer
34+
35+
case $answer in
36+
[Yy]* ) ;;
37+
* ) echo "Aborted."; exit;;
38+
esac
39+
40+
echo "Building and pushing Docker image..."
41+
docker build --no-cache \
42+
--build-arg CLI_VERSION=$CLI_VERSION \
43+
--build-arg SDK_VERSION=$SDK_VERSION \
44+
--build-arg PIP_INDEX_URL=https://test.pypi.org/simple \
45+
--build-arg PIP_EXTRA_INDEX_URL=https://pypi.org/simple \
46+
--platform linux/amd64,linux/arm64 \
47+
-t socketdev/cli:$CLI_VERSION-test . \
48+
&& docker build --no-cache \
49+
--build-arg CLI_VERSION=$CLI_VERSION \
50+
--build-arg SDK_VERSION=$SDK_VERSION \
51+
--build-arg PIP_INDEX_URL=https://test.pypi.org/simple \
52+
--build-arg PIP_EXTRA_INDEX_URL=https://pypi.org/simple \
53+
--platform linux/amd64,linux/arm64 \
54+
-t socketdev/cli:test . \
55+
&& docker push socketdev/cli:$CLI_VERSION-test \
56+
&& docker push socketdev/cli:test
57+
58+
if [ $? -eq 0 ]; then
59+
echo "Successfully deployed version $CLI_VERSION"
60+
else
61+
echo "Failed to deploy version $CLI_VERSION"
62+
exit 1
63+
fi

scripts/deploy-test-pypi.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
# Get version from __init__.py
4+
INIT_FILE="socketsecurity/__init__.py"
5+
ORIGINAL_VERSION=$(grep -o "__version__.*" $INIT_FILE | awk '{print $3}' | tr -d "'")
6+
BACKUP_FILE="${INIT_FILE}.bak"
7+
8+
# Get existing versions from TestPyPI
9+
echo "Checking existing versions on TestPyPI..."
10+
EXISTING_VERSIONS=$(curl -s https://test.pypi.org/pypi/socketsecurity/json | python -c "
11+
import sys, json
12+
data = json.load(sys.stdin)
13+
versions = [v for v in data.get('releases', {}).keys() if v.startswith('$ORIGINAL_VERSION.dev')]
14+
if versions:
15+
versions.sort(key=lambda x: int(x.split('dev')[1]))
16+
print(versions[-1])
17+
")
18+
19+
# Determine new version
20+
if [ -z "$EXISTING_VERSIONS" ]; then
21+
VERSION="${ORIGINAL_VERSION}.dev1"
22+
echo "No existing dev versions found. Using ${VERSION}"
23+
else
24+
LAST_DEV_NUM=$(echo $EXISTING_VERSIONS | grep -o 'dev[0-9]*' | grep -o '[0-9]*')
25+
NEXT_DEV_NUM=$((LAST_DEV_NUM + 1))
26+
VERSION="${ORIGINAL_VERSION}.dev${NEXT_DEV_NUM}"
27+
echo "Found existing version ${EXISTING_VERSIONS}. Using ${VERSION}"
28+
fi
29+
30+
echo "Deploying version ${VERSION} to Test PyPI"
31+
32+
# Backup original __init__.py
33+
cp $INIT_FILE $BACKUP_FILE
34+
35+
# Update version in __init__.py
36+
sed -i.tmp "s/__version__ = '${ORIGINAL_VERSION}'/__version__ = '${VERSION}'/" $INIT_FILE
37+
rm "${INIT_FILE}.tmp"
38+
39+
# Build and upload to test PyPI
40+
python -m build --wheel --sdist > /dev/null 2>&1
41+
42+
# Restore original __init__.py
43+
mv $BACKUP_FILE $INIT_FILE
44+
45+
# Upload to TestPyPI using python -m
46+
python -m twine upload --repository testpypi dist/*${VERSION}*
47+
48+
echo "Deployed to Test PyPI. Wait a few minutes before installing the new version."
49+
echo
50+
51+
echo "New version:"
52+
echo "${VERSION}"

scripts/run.sh

100644100755
File mode changed.

socketsecurity/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.0.0'
2+
__version__ = '2.0.2'

socketsecurity/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,18 @@ def from_args(cls, args_list: Optional[List[str]] = None) -> 'CliConfig':
4040
# Get API token from env or args
4141
api_token = os.getenv("SOCKET_SECURITY_API_KEY") or args.api_token
4242

43+
# Strip quotes from commit message if present
44+
commit_message = args.commit_message
45+
if commit_message and commit_message.startswith('"') and commit_message.endswith('"'):
46+
commit_message = commit_message[1:-1]
47+
4348
config_args = {
4449
'api_token': api_token,
4550
'repo': args.repo,
4651
'branch': args.branch,
4752
'committers': args.committers,
4853
'pr_number': args.pr_number,
49-
'commit_message': args.commit_message,
54+
'commit_message': commit_message,
5055
'default_branch': args.default_branch,
5156
'target_path': args.target_path,
5257
'scm': args.scm,

socketsecurity/core/__init__.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from socketdev.fullscans import (
1212
FullScanParams,
1313
SocketArtifact,
14+
DiffArtifact,
1415
)
1516
from socketdev.org import Organization
1617
from socketdev.repos import RepositoryInfo
@@ -312,25 +313,64 @@ def get_added_and_removed_packages(self, head_full_scan: Optional[FullScan], new
312313
"""
313314
if head_full_scan is None:
314315
# First scan - all packages are new, none removed
316+
log.info(f"No head scan found. New scan ID: {new_full_scan.id}")
315317
return new_full_scan.packages, {}
316318

317319
# Normal case - compare scans
320+
log.info(f"Comparing scans - Head scan ID: {head_full_scan.id}, New scan ID: {new_full_scan.id}")
318321
diff_report = self.sdk.fullscans.stream_diff(self.config.org_slug, head_full_scan.id, new_full_scan.id).data
319-
added_artifacts = diff_report.artifacts.added
320-
removed_artifacts = diff_report.artifacts.removed
322+
323+
# Debug output for artifact counts
324+
log.info(f"Diff report artifact counts:")
325+
log.info(f"Added: {len(diff_report.artifacts.added)}")
326+
log.info(f"Removed: {len(diff_report.artifacts.removed)}")
327+
log.info(f"Unchanged: {len(diff_report.artifacts.unchanged)}")
328+
log.info(f"Replaced: {len(diff_report.artifacts.replaced)}")
329+
log.info(f"Updated: {len(diff_report.artifacts.updated)}")
330+
331+
added_artifacts = diff_report.artifacts.added + diff_report.artifacts.updated
332+
removed_artifacts = diff_report.artifacts.removed + diff_report.artifacts.replaced
321333

322334
added_packages: Dict[str, Package] = {}
323335
removed_packages: Dict[str, Package] = {}
324336

325337
for artifact in added_artifacts:
326-
# Get the full package data from new_full_scan
327-
pkg = new_full_scan.packages[artifact.id]
328-
added_packages[artifact.id] = Package(**asdict(pkg))
338+
try:
339+
# Get the full package data from new_full_scan
340+
pkg = new_full_scan.packages[artifact.id]
341+
added_packages[artifact.id] = Package(**asdict(pkg))
342+
except KeyError:
343+
# Debug output to find matching packages
344+
log.error(f"KeyError: Could not find added artifact {artifact.id} in new_full_scan")
345+
log.error(f"Artifact details - name: {artifact.name}, version: {artifact.version}")
346+
# Look for packages with matching name/version
347+
matches = [p for p in new_full_scan.packages.values()
348+
if p.name == artifact.name and p.version == artifact.version]
349+
if matches:
350+
log.error(f"Found {len(matches)} packages with matching name/version:")
351+
for m in matches:
352+
log.error(f" ID: {m.id}, name: {m.name}, version: {m.version}")
353+
else:
354+
log.error("No matching packages found in new_full_scan")
329355

330356
for artifact in removed_artifacts:
331-
# Get the full package data from head_full_scan
332-
pkg = head_full_scan.packages[artifact.id]
333-
removed_packages[artifact.id] = Package(**asdict(pkg))
357+
try:
358+
# Get the full package data from head_full_scan
359+
pkg = head_full_scan.packages[artifact.id]
360+
removed_packages[artifact.id] = Package(**asdict(pkg))
361+
except KeyError:
362+
# Debug output to find matching packages
363+
log.error(f"KeyError: Could not find removed artifact {artifact.id} in head_full_scan")
364+
log.error(f"Artifact details - name: {artifact.name}, version: {artifact.version}")
365+
# Look for packages with matching name/version
366+
matches = [p for p in head_full_scan.packages.values()
367+
if p.name == artifact.name and p.version == artifact.version]
368+
if matches:
369+
log.error(f"Found {len(matches)} packages with matching name/version:")
370+
for m in matches:
371+
log.error(f" ID: {m.id}, name: {m.name}, version: {m.version}")
372+
else:
373+
log.error("No matching packages found in head_full_scan")
334374

335375
return added_packages, removed_packages
336376

socketsecurity/core/classes.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
from dataclasses import dataclass, field
3-
from typing import Dict, List, TypedDict
3+
from typing import Dict, List, TypedDict, Any
44

55
from socketdev.fullscans import FullScanMetadata, SocketArtifact
66

@@ -393,8 +393,6 @@ def to_dict(self) -> dict:
393393
}
394394

395395

396-
397-
398396
class GithubComment:
399397
url: str
400398
html_url: str

0 commit comments

Comments
 (0)