Skip to content

Commit 0f39e43

Browse files
authored
added pr-preview and release workflows (#15)
* added pr-preview and release workflows
1 parent 0dabc45 commit 0f39e43

File tree

3 files changed

+277
-0
lines changed

3 files changed

+277
-0
lines changed

.github/workflows/pr-preview.yml

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