Skip to content

added pr-preview and release workflows #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions .github/workflows/pr-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
name: PR Preview
on:
pull_request:
types: [opened, synchronize]

jobs:
preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Set preview version
run: |
BASE_VERSION=$(python -c "from socketdev import __version__; print(__version__)")
PREVIEW_VERSION="${BASE_VERSION}.dev${{ github.event.pull_request.number }}${{ github.event.pull_request.commits }}"
echo "VERSION=${PREVIEW_VERSION}" >> $GITHUB_ENV

# Update version in __init__.py
echo "__version__ = \"${PREVIEW_VERSION}\"" > socketdev/__init__.py.tmp
cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp
mv socketdev/__init__.py.tmp socketdev/__init__.py

# Verify the change
echo "Updated version in __init__.py:"
cat socketdev/__init__.py | grep "__version__"

- name: Check if version exists on Test PyPI
id: version_check
env:
VERSION: ${{ env.VERSION }}
run: |
if curl -s -f https://test.pypi.org/pypi/socket-sdk-python/$VERSION/json > /dev/null; then
echo "Version ${VERSION} already exists on Test PyPI"
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "Version ${VERSION} not found on Test PyPI"
echo "exists=false" >> $GITHUB_OUTPUT
fi

- name: Build package
if: steps.version_check.outputs.exists != 'true'
run: |
pip install build
python -m build

- name: Restore original version
if: always()
run: |
BASE_VERSION=$(echo $VERSION | cut -d'.' -f1-3)
echo "__version__ = \"${BASE_VERSION}\"" > socketdev/__init__.py.tmp
cat socketdev/__init__.py | grep -v "__version__" >> socketdev/__init__.py.tmp
mv socketdev/__init__.py.tmp socketdev/__init__.py

- name: Publish to Test PyPI
if: steps.version_check.outputs.exists != 'true'
uses: pypa/[email protected]
with:
repository-url: https://test.pypi.org/legacy/
password: ${{ secrets.TEST_PYPI_TOKEN }}
verbose: true

- name: Comment on PR
if: steps.version_check.outputs.exists != 'true'
uses: actions/github-script@v7
env:
VERSION: ${{ env.VERSION }}
with:
script: |
const version = process.env.VERSION;
const prNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
// Find existing bot comments
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});

const botComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('🚀 Preview package published!')
);

const comment = `
🚀 Preview package published!

Install with:
\`\`\`bash
pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${version}
\`\`\`;

if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: owner,
repo: repo,
comment_id: botComment.id,
body: comment
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: prNumber,
body: comment
});
}

- name: Verify package is available
if: steps.version_check.outputs.exists != 'true'
id: verify_package
env:
VERSION: ${{ env.VERSION }}
run: |
for i in {1..30}; do
if pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socket-sdk-python==${VERSION}; then
echo "Package ${VERSION} is now available and installable on Test PyPI"
pip uninstall -y socket-sdk-python
echo "success=true" >> $GITHUB_OUTPUT
exit 0
fi
echo "Attempt $i: Package not yet installable, waiting 20s... (${i}/30)"
sleep 20
done
echo "success=false" >> $GITHUB_OUTPUT
exit 1
49 changes: 49 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Release
on:
push:
tags:
- 'v*'

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'

- name: Get Version
id: version
run: |
RAW_VERSION=$(python -c "from socketdev import __version__; print(__version__)")
echo "VERSION=$RAW_VERSION" >> $GITHUB_ENV
if [ "v$RAW_VERSION" != "${{ github.ref_name }}" ]; then
echo "Error: Git tag (${{ github.ref_name }}) does not match package version (v$RAW_VERSION)"
exit 1
fi

- name: Check if version exists on PyPI
id: version_check
env:
VERSION: ${{ env.VERSION }}
run: |
if curl -s -f https://pypi.org/pypi/socket-sdk-python/$VERSION/json > /dev/null; then
echo "Version ${VERSION} already exists on PyPI"
echo "pypi_exists=true" >> $GITHUB_OUTPUT
else
echo "Version ${VERSION} not found on PyPI - proceeding with PyPI deployment"
echo "pypi_exists=false" >> $GITHUB_OUTPUT
fi

- name: Build package
if: steps.version_check.outputs.pypi_exists != 'true'
run: |
pip install build
python -m build

- name: Publish to PyPI
if: steps.version_check.outputs.pypi_exists != 'true'
uses: pypa/[email protected]
with:
password: ${{ secrets.PYPI_TOKEN }}
92 changes: 92 additions & 0 deletions .github/workflows/version-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
name: Version Check
on:
pull_request:
types: [opened, synchronize]
paths:
- 'socketdev/**'
- 'setup.py'
- 'pyproject.toml'

jobs:
check_version:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all branches

- name: Check version increment
id: version_check
run: |
# Get version from current PR
PR_VERSION=$(grep -o '__version__ = "[^"]*"' socketdev/__init__.py | cut -d'"' -f2)
echo "Debug PR version: $PR_VERSION"
echo "PR_VERSION=${PR_VERSION}" >> $GITHUB_ENV

# Get version from main branch
git checkout origin/main
MAIN_VERSION=$(grep -o '__version__ = "[^"]*"' socketdev/__init__.py | cut -d'"' -f2)
echo "Debug main version: $MAIN_VERSION"
echo "MAIN_VERSION=${MAIN_VERSION}" >> $GITHUB_ENV

# Compare versions using Python
python3 -c "
from packaging import version
pr_ver = version.parse('${PR_VERSION}')
main_ver = version.parse('${MAIN_VERSION}')
if pr_ver <= main_ver:
print(f'❌ Version must be incremented! Main: {main_ver}, PR: {pr_ver}')
exit(1)
print(f'✅ Version properly incremented from {main_ver} to {pr_ver}')
"

- name: Manage PR Comment
uses: actions/github-script@v7
if: always()
env:
MAIN_VERSION: ${{ env.MAIN_VERSION }}
PR_VERSION: ${{ env.PR_VERSION }}
CHECK_RESULT: ${{ steps.version_check.outcome }}
with:
script: |
const success = process.env.CHECK_RESULT === 'success';
const prNumber = context.payload.pull_request.number;
const owner = context.repo.owner;
const repo = context.repo.repo;
const comments = await github.rest.issues.listComments({
owner: owner,
repo: repo,
issue_number: prNumber,
});

const versionComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Version Check')
);

if (versionComment) {
if (success) {
// Delete the warning comment if check passes
await github.rest.issues.deleteComment({
owner: owner,
repo: repo,
comment_id: versionComment.id
});
} else {
// Update existing warning
await github.rest.issues.updateComment({
owner: owner,
repo: repo,
comment_id: versionComment.id,
body: `❌ **Version Check Failed**\n\nPlease increment...`
});
}
} else if (!success) {
// Create new warning comment only if check fails
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: prNumber,
body: `❌ **Version Check Failed**\n\nPlease increment...`
});
}
Loading