Skip to content

Commit 00af6f7

Browse files
committed
added pr-preview and release workflows
1 parent 0dabc45 commit 00af6f7

File tree

3 files changed

+272
-0
lines changed

3 files changed

+272
-0
lines changed

.github/workflows/pr-preview.yml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
name: PR Preview
2+
on:
3+
pull_request:
4+
types: [opened, synchronize]
5+
6+
jobs:
7+
preview:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- uses: actions/setup-python@v5
12+
with:
13+
python-version: '3.x'
14+
15+
- name: Set preview version
16+
run: |
17+
BASE_VERSION=$(python -c "from socketdev import __version__; print(__version__)")
18+
PREVIEW_VERSION="${BASE_VERSION}.dev${{ github.event.pull_request.number }}${{ github.event.pull_request.commits }}"
19+
echo "VERSION=${PREVIEW_VERSION}" >> $GITHUB_ENV
20+
21+
# Update version in __init__.py
22+
echo "__version__ = \"${PREVIEW_VERSION}\"" > socketdev/__init__.py.tmp
23+
cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp
24+
mv socketdev/__init__.py.tmp socketdev/__init__.py
25+
26+
# Verify the change
27+
echo "Updated version in __init__.py:"
28+
cat socketdev/__init__.py | grep "__version__"
29+
30+
- name: Check if version exists on Test PyPI
31+
id: version_check
32+
env:
33+
VERSION: ${{ env.VERSION }}
34+
run: |
35+
if curl -s -f https://test.pypi.org/pypi/socket-sdk-python/$VERSION/json > /dev/null; then
36+
echo "Version ${VERSION} already exists on Test PyPI"
37+
echo "exists=true" >> $GITHUB_OUTPUT
38+
else
39+
echo "Version ${VERSION} not found on Test PyPI"
40+
echo "exists=false" >> $GITHUB_OUTPUT
41+
fi
42+
43+
- name: Build package
44+
if: steps.version_check.outputs.exists != 'true'
45+
run: |
46+
pip install build
47+
python -m build
48+
49+
- name: Restore original version
50+
if: always()
51+
run: |
52+
BASE_VERSION=$(echo $VERSION | cut -d'.' -f1-3)
53+
echo "__version__ = \"${BASE_VERSION}\"" > socketdev/__init__.py.tmp
54+
cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp
55+
mv socketdev/__init__.py.tmp socketdev/__init__.py
56+
57+
- name: Publish to Test PyPI
58+
if: steps.version_check.outputs.exists != 'true'
59+
uses: pypa/[email protected]
60+
with:
61+
repository-url: https://test.pypi.org/legacy/
62+
password: ${{ secrets.TEST_PYPI_TOKEN }}
63+
verbose: true
64+
65+
- name: Comment on PR
66+
if: steps.version_check.outputs.exists != 'true'
67+
uses: actions/github-script@v7
68+
env:
69+
VERSION: ${{ env.VERSION }}
70+
with:
71+
script: |
72+
const version = process.env.VERSION;
73+
const prNumber = context.payload.pull_request.number;
74+
const owner = context.repo.owner;
75+
const repo = context.repo.repo;
76+
// Find existing bot comments
77+
const comments = await github.rest.issues.listComments({
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
issue_number: prNumber,
81+
});
82+
83+
const botComment = comments.data.find(comment =>
84+
comment.user.type === 'Bot' &&
85+
comment.body.includes('🚀 Preview package published!')
86+
);
87+
88+
const comment = `
89+
🚀 Preview package published!
90+
91+
Install with:
92+
\`\`\`bash
93+
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${version}
94+
\`\`\`;
95+
96+
if (botComment) {
97+
// Update existing comment
98+
await github.rest.issues.updateComment({
99+
owner: owner,
100+
repo: repo,
101+
comment_id: botComment.id,
102+
body: comment
103+
});
104+
} else {
105+
// Create new comment
106+
await github.rest.issues.createComment({
107+
owner: owner,
108+
repo: repo,
109+
issue_number: prNumber,
110+
body: comment
111+
});
112+
}
113+
114+
- name: Verify package is available
115+
if: steps.version_check.outputs.exists != 'true'
116+
id: verify_package
117+
env:
118+
VERSION: ${{ env.VERSION }}
119+
run: |
120+
for i in {1..30}; do
121+
if pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${VERSION}; then
122+
echo "Package ${VERSION} is now available and installable on Test PyPI"
123+
pip uninstall -y socket-sdk-python
124+
echo "success=true" >> $GITHUB_OUTPUT
125+
exit 0
126+
fi
127+
echo "Attempt $i: Package not yet installable, waiting 20s... (${i}/30)"
128+
sleep 20
129+
done
130+
echo "success=false" >> $GITHUB_OUTPUT
131+
exit 1

.github/workflows/release.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- 'v*'
6+
7+
jobs:
8+
release:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-python@v5
13+
with:
14+
python-version: '3.11'
15+
16+
- name: Get Version
17+
id: version
18+
run: |
19+
RAW_VERSION=$(python -c "from socketdev import __version__; print(__version__)")
20+
echo "VERSION=$RAW_VERSION" >> $GITHUB_ENV
21+
if [ "v$RAW_VERSION" != "${{ github.ref_name }}" ]; then
22+
echo "Error: Git tag (${{ github.ref_name }}) does not match package version (v$RAW_VERSION)"
23+
exit 1
24+
fi
25+
26+
- name: Check if version exists on PyPI
27+
id: version_check
28+
env:
29+
VERSION: ${{ env.VERSION }}
30+
run: |
31+
if curl -s -f https://pypi.org/pypi/socket-sdk-python/$VERSION/json > /dev/null; then
32+
echo "Version ${VERSION} already exists on PyPI"
33+
echo "pypi_exists=true" >> $GITHUB_OUTPUT
34+
else
35+
echo "Version ${VERSION} not found on PyPI - proceeding with PyPI deployment"
36+
echo "pypi_exists=false" >> $GITHUB_OUTPUT
37+
fi
38+
39+
- name: Build package
40+
if: steps.version_check.outputs.pypi_exists != 'true'
41+
run: |
42+
pip install build
43+
python -m build
44+
45+
- name: Publish to PyPI
46+
if: steps.version_check.outputs.pypi_exists != 'true'
47+
uses: pypa/[email protected]
48+
with:
49+
password: ${{ secrets.PYPI_TOKEN }}

.github/workflows/version-check.yml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Version Check
2+
on:
3+
pull_request:
4+
types: [opened, synchronize]
5+
paths:
6+
- 'socketdev/**'
7+
- 'setup.py'
8+
- 'pyproject.toml'
9+
10+
jobs:
11+
check_version:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0 # Fetch all history for all branches
17+
18+
- name: Check version increment
19+
id: version_check
20+
run: |
21+
# Get version from current PR
22+
PR_VERSION=$(grep -o '__version__ = "[^"]*"' socketdev/__init__.py | cut -d'"' -f2)
23+
echo "Debug PR version: $PR_VERSION"
24+
echo "PR_VERSION=${PR_VERSION}" >> $GITHUB_ENV
25+
26+
# Get version from main branch
27+
git checkout origin/main
28+
MAIN_VERSION=$(grep -o '__version__ = "[^"]*"' socketdev/__init__.py | cut -d'"' -f2)
29+
echo "Debug main version: $MAIN_VERSION"
30+
echo "MAIN_VERSION=${MAIN_VERSION}" >> $GITHUB_ENV
31+
32+
# Compare versions using Python
33+
python3 -c "
34+
from packaging import version
35+
pr_ver = version.parse('${PR_VERSION}')
36+
main_ver = version.parse('${MAIN_VERSION}')
37+
if pr_ver <= main_ver:
38+
print(f'❌ Version must be incremented! Main: {main_ver}, PR: {pr_ver}')
39+
exit(1)
40+
print(f'✅ Version properly incremented from {main_ver} to {pr_ver}')
41+
"
42+
43+
- name: Manage PR Comment
44+
uses: actions/github-script@v7
45+
if: always()
46+
env:
47+
MAIN_VERSION: ${{ env.MAIN_VERSION }}
48+
PR_VERSION: ${{ env.PR_VERSION }}
49+
CHECK_RESULT: ${{ steps.version_check.outcome }}
50+
with:
51+
script: |
52+
const success = process.env.CHECK_RESULT === 'success';
53+
const prNumber = context.payload.pull_request.number;
54+
const owner = context.repo.owner;
55+
const repo = context.repo.repo;
56+
const comments = await github.rest.issues.listComments({
57+
owner: owner,
58+
repo: repo,
59+
issue_number: prNumber,
60+
});
61+
62+
const versionComment = comments.data.find(comment =>
63+
comment.user.type === 'Bot' &&
64+
comment.body.includes('Version Check')
65+
);
66+
67+
if (versionComment) {
68+
if (success) {
69+
// Delete the warning comment if check passes
70+
await github.rest.issues.deleteComment({
71+
owner: owner,
72+
repo: repo,
73+
comment_id: versionComment.id
74+
});
75+
} else {
76+
// Update existing warning
77+
await github.rest.issues.updateComment({
78+
owner: owner,
79+
repo: repo,
80+
comment_id: versionComment.id,
81+
body: `❌ **Version Check Failed**\n\nPlease increment...`
82+
});
83+
}
84+
} else if (!success) {
85+
// Create new warning comment only if check fails
86+
await github.rest.issues.createComment({
87+
owner: owner,
88+
repo: repo,
89+
issue_number: prNumber,
90+
body: `❌ **Version Check Failed**\n\nPlease increment...`
91+
});
92+
}

0 commit comments

Comments
 (0)