Skip to content

Commit f8bb5d9

Browse files
committed
Update generate.yml to work with action restrictions
1 parent 5eca0a2 commit f8bb5d9

File tree

1 file changed

+101
-29
lines changed

1 file changed

+101
-29
lines changed

.github/workflows/generate.yml

Lines changed: 101 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ on:
1010
- 'scripts/prettify-base-json.mjs'
1111
- '.github/workflows/generate.yml'
1212
schedule:
13-
# At 06:37 on every day-of-week from Monday through Friday.
14-
- cron: '37 6 * * 1-5'
13+
# At 07:23 on every day-of-week from Monday through Friday.
14+
- cron: '23 7 * * 1-5'
1515
workflow_dispatch:
1616
inputs:
1717
force:
@@ -24,46 +24,118 @@ permissions:
2424
contents: write
2525
pull-requests: write
2626

27-
permissions:
28-
contents: write
29-
pull-requests: write
30-
3127
jobs:
3228
fetch_and_update:
3329
name: Sync OpenAPI definition
34-
uses: SocketDev/workflows/.github/workflows/reusable-sync.yml@master
35-
secrets: inherit
36-
with:
37-
url: 'https://api.socket.dev/v0/openapi'
38-
path: 'openapi.json'
39-
branch-name: 'automated/open-api'
40-
commit-message: 'fix(openapi): sync with openapi definition'
41-
pr-title: 'Sync with OpenAPI definition'
42-
pr-body: |
43-
## 🔄 OpenAPI Sync
44-
45-
The OpenAPI definition in the API has been updated. This PR automatically:
46-
- Downloads the latest OpenAPI specification
47-
- Regenerates TypeScript types
48-
- Updates SDK method signatures if needed
49-
50-
### What's Changed
51-
See the file changes below for specific updates to the API types and methods.
52-
53-
⚠️ **Please review carefully for any breaking changes in the API.**
54-
npm-post-sync-script: 'generate-sdk'
30+
runs-on: ubuntu-latest
31+
steps:
32+
- name: Random delay
33+
if: github.event_name == 'schedule'
34+
run: |
35+
# Add random delay between 0-10 minutes for scheduled runs
36+
delay=$((RANDOM % 600))
37+
echo "Sleeping for $delay seconds..."
38+
sleep $delay
39+
40+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v4
41+
with:
42+
token: ${{ secrets.GITHUB_TOKEN }}
43+
44+
- uses: SocketDev/socket-sdk-js/.github/actions/setup@62eccf4b30fb53542d85450470b41b014666803c
45+
with:
46+
node-version: '22'
47+
48+
- name: Fetch latest OpenAPI definition
49+
id: fetch
50+
run: |
51+
echo "Fetching latest OpenAPI definition..."
52+
curl -sSL https://api.socket.dev/v0/openapi -o openapi-new.json
53+
54+
# Check if file changed
55+
if [ -f openapi.json ]; then
56+
if diff -q openapi.json openapi-new.json > /dev/null; then
57+
echo "No changes detected"
58+
echo "changed=false" >> $GITHUB_OUTPUT
59+
else
60+
echo "Changes detected"
61+
echo "changed=true" >> $GITHUB_OUTPUT
62+
mv openapi-new.json openapi.json
63+
fi
64+
else
65+
echo "OpenAPI file doesn't exist, creating new one"
66+
echo "changed=true" >> $GITHUB_OUTPUT
67+
mv openapi-new.json openapi.json
68+
fi
69+
70+
- name: Run post-sync script
71+
if: steps.fetch.outputs.changed == 'true' || github.event.inputs.force == 'true'
72+
run: pnpm run generate-sdk
73+
74+
- name: Configure git
75+
if: steps.fetch.outputs.changed == 'true' || github.event.inputs.force == 'true'
76+
run: |
77+
git config --global user.name "github-actions[bot]"
78+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
79+
80+
- name: Check for changes
81+
id: check
82+
if: steps.fetch.outputs.changed == 'true' || github.event.inputs.force == 'true'
83+
run: |
84+
if [ -n "$(git status --porcelain)" ]; then
85+
echo "has_changes=true" >> $GITHUB_OUTPUT
86+
else
87+
echo "has_changes=false" >> $GITHUB_OUTPUT
88+
fi
89+
90+
- name: Commit and push changes
91+
if: steps.check.outputs.has_changes == 'true'
92+
run: |
93+
git checkout -b automated/open-api
94+
git add .
95+
git commit -m "fix(openapi): sync with openapi definition"
96+
git push origin automated/open-api -fu --no-verify
97+
98+
- name: Create Pull Request
99+
if: steps.check.outputs.has_changes == 'true'
100+
env:
101+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
102+
run: |
103+
# Check if PR already exists
104+
existing_pr=$(gh pr list --head automated/open-api --json number --jq '.[0].number' || echo "")
105+
106+
if [ -z "$existing_pr" ]; then
107+
gh pr create \
108+
--head automated/open-api \
109+
--base main \
110+
--title "Sync with OpenAPI definition" \
111+
--body "## 🔄 OpenAPI Sync
112+
113+
The OpenAPI definition in the API has been updated. This PR automatically:
114+
- Downloads the latest OpenAPI specification
115+
- Regenerates TypeScript types
116+
- Updates SDK method signatures if needed
117+
118+
### What's Changed
119+
See the file changes below for specific updates to the API types and methods.
120+
121+
⚠️ **Please review carefully for any breaking changes in the API.**" \
122+
--label "dependencies" \
123+
--label "automated"
124+
else
125+
echo "PR #$existing_pr already exists, skipping creation"
126+
fi
55127
56128
validate:
57129
name: Validate generated SDK
58130
needs: fetch_and_update
59131
if: success()
60132
runs-on: ubuntu-latest
61133
steps:
62-
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
134+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v4
63135
with:
64136
ref: automated/open-api
65137

66-
- uses: SocketDev/socket-sdk-js/.github/actions/setup@e8ae193eb686db288e09cbacce75a828681ccae9
138+
- uses: SocketDev/socket-sdk-js/.github/actions/setup@62eccf4b30fb53542d85450470b41b014666803c
67139
with:
68140
node-version: '22'
69141

0 commit comments

Comments
 (0)