Skip to content

Commit 7934cac

Browse files
Automatically re-generate the TS SDK when we merge PRs to this repo (#262)
* Automatically trigger TS SDK generation when PRs get merged * Update scripts --------- Co-authored-by: Matt Blank <[email protected]>
1 parent 0181727 commit 7934cac

File tree

2 files changed

+190
-2
lines changed

2 files changed

+190
-2
lines changed

.github/workflows/ts-sdk.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,45 @@ on:
44
workflow_dispatch:
55
inputs:
66
version:
7-
description: "The version of the SDKs that you would like to release"
7+
description: 'The version of the SDKs that you would like to release'
88
required: true
99
type: string
10+
push:
11+
branches:
12+
- main
13+
paths:
14+
- 'fern/**'
1015

1116
jobs:
1217
release:
1318
runs-on: ubuntu-latest
1419
steps:
1520
- name: Checkout repo
1621
uses: actions/checkout@v3
22+
with:
23+
fetch-depth: 0
1724

1825
- name: Setup node
1926
uses: actions/setup-node@v4
2027

2128
- name: Download Fern
2229
run: npm install -g fern-api
2330

31+
- name: Determine version
32+
id: bump
33+
run: |
34+
if [ -n "${{ inputs.version }}" ]; then
35+
echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
36+
else
37+
chmod +x ./scripts/fern/bump.sh
38+
VERSION=$(./scripts/fern/bump.sh --from HEAD~1 --group ts-sdk)
39+
echo "version=$VERSION" >> $GITHUB_OUTPUT
40+
fi
41+
2442
- name: Release SDKs
2543
env:
2644
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
2745
FERN_NPM_TOKEN: ${{ secrets.FERN_NPM_TOKEN }}
2846
run: |
29-
fern generate --group ts-sdk --version ${{ inputs.version }} --log-level debug
47+
echo "Releasing SDK version: ${{ steps.bump.outputs.version }}"
48+
fern generate --group ts-sdk --version ${{ steps.bump.outputs.version }} --log-level debug

scripts/fern/bump.sh

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/bin/bash
2+
#
3+
# Bumps the version of the SDK by comparing the current API
4+
# against a snapshot of the API from a previous commit.
5+
#
6+
# Usage:
7+
#
8+
# $ ./scripts/bump.sh --from HEAD~1 --group ts-sdk
9+
10+
set -uo pipefail
11+
12+
FROM_COMMIT=""
13+
GROUP=""
14+
15+
log() {
16+
echo "$@" >&2
17+
}
18+
19+
ensure_fern_cli() {
20+
if ! command -v fern &> /dev/null; then
21+
log "Installing Fern CLI..."
22+
if ! npm install -g fern-api@latest; then
23+
log "Failed to install Fern CLI"
24+
exit 1
25+
fi
26+
else
27+
log "Fern CLI is already installed"
28+
fi
29+
}
30+
31+
usage() {
32+
echo "Usage: $0 --from <commit> --group <group>"
33+
echo " --from Previous commit reference (e.g., HEAD~1, 8475a09)"
34+
echo " --group Group to use for version detection (e.g., ts-sdk, java-sdk)"
35+
exit 1
36+
}
37+
38+
fern() {
39+
FERN_NO_VERSION_REDIRECTION=true command fern "$@"
40+
}
41+
42+
get_latest_version() {
43+
local group=$1
44+
local version=""
45+
46+
case $group in
47+
ts-sdk)
48+
version=$(npm view intercom-client version 2>/dev/null)
49+
;;
50+
java-sdk)
51+
version=$(curl -s "https://repo1.maven.org/maven2/io/intercom/intercom-java/maven-metadata.xml" | grep -oP '<version>\K[^<]+' | sort -V | tail -n1)
52+
;;
53+
*)
54+
log "Unknown group: $group"
55+
exit 1
56+
;;
57+
esac
58+
59+
if [[ -z "$version" ]]; then
60+
log "Could not determine latest version for group $group"
61+
exit 1
62+
fi
63+
64+
echo "$version"
65+
}
66+
67+
while [[ $# -gt 0 ]]; do
68+
case $1 in
69+
--from)
70+
FROM_COMMIT="$2"
71+
shift 2
72+
;;
73+
--group)
74+
GROUP="$2"
75+
shift 2
76+
;;
77+
-h|--help)
78+
usage
79+
;;
80+
*)
81+
echo "Unknown option: $1"
82+
usage
83+
;;
84+
esac
85+
done
86+
87+
if [[ -z "$FROM_COMMIT" || -z "$GROUP" ]]; then
88+
usage
89+
fi
90+
91+
# Ensure Fern CLI is installed
92+
ensure_fern_cli
93+
94+
# Get working directory and reset temp dir
95+
WORK_DIR="$(git rev-parse --show-toplevel)"
96+
WORKTREE_DIR="$WORK_DIR/.tmp_worktree"
97+
rm -rf "$WORKTREE_DIR"
98+
99+
# Ensure the FROM_COMMIT exists
100+
log "Verifying that commit $FROM_COMMIT exists..."
101+
if ! git rev-parse "$FROM_COMMIT" &>/dev/null; then
102+
log "Commit $FROM_COMMIT not found. Did you fetch full history?"
103+
exit 1
104+
fi
105+
106+
# Get the latest version
107+
FROM_VERSION=$(get_latest_version "$GROUP")
108+
log "Using current version: $FROM_VERSION"
109+
110+
# Ensure cleanup on exit
111+
cleanup() {
112+
if [[ -d "$WORKTREE_DIR" ]]; then
113+
(cd "$WORKTREE_DIR" && git submodule deinit --all --force >/dev/null 2>&1 || true)
114+
git worktree remove --force "$WORKTREE_DIR" >/dev/null 2>&1 || true
115+
fi
116+
rm -f "$WORK_DIR/from.json" "$WORK_DIR/to.json" >/dev/null 2>&1
117+
popd >/dev/null 2>&1 || true
118+
}
119+
trap cleanup EXIT
120+
121+
# Navigate to project root
122+
pushd "$WORK_DIR" >/dev/null
123+
124+
# Generate IR from current commit
125+
log "Generating IR from current commit..."
126+
fern ir to.json
127+
128+
# Generate IR from previous commit in worktree
129+
log "Creating worktree in $WORKTREE_DIR..."
130+
if ! git worktree add "$WORKTREE_DIR" "$FROM_COMMIT"; then
131+
log "Failed to create worktree"
132+
exit 1
133+
fi
134+
135+
(
136+
cd "$WORKTREE_DIR" || {
137+
log "Cannot access worktree directory"
138+
exit 1
139+
}
140+
141+
log "Updating submodules..."
142+
git submodule update --init --recursive
143+
144+
log "Running fern ir for previous commit..."
145+
fern ir from.json
146+
147+
if [ ! -f "from.json" ]; then
148+
log "from.json was not generated in worktree"
149+
exit 1
150+
fi
151+
)
152+
153+
# Copy from.json back to root
154+
log "Copying from.json to working directory..."
155+
cp "$WORKTREE_DIR/from.json" "$WORK_DIR/from.json"
156+
157+
# Diff and get next version
158+
log "Running fern diff..."
159+
DIFF_OUTPUT=$(fern diff --from from.json --to to.json --from-version "$FROM_VERSION")
160+
log "Diff output: $DIFF_OUTPUT"
161+
162+
NEXT_VERSION=$(echo "$DIFF_OUTPUT" | jq -r '.nextVersion')
163+
164+
if [[ -z "$NEXT_VERSION" || "$NEXT_VERSION" == "null" ]]; then
165+
log "Could not determine next version from fern diff output"
166+
exit 1
167+
fi
168+
169+
echo "$NEXT_VERSION"

0 commit comments

Comments
 (0)