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
+
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
0 commit comments