Skip to content

Commit 6352e9f

Browse files
committed
Automatically trigger TS SDK generation when PRs get merged
1 parent f2f3947 commit 6352e9f

File tree

2 files changed

+165
-2
lines changed

2 files changed

+165
-2
lines changed

.github/workflows/ts-sdk.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ 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:
@@ -21,9 +26,20 @@ jobs:
2126
- name: Download Fern
2227
run: npm install -g fern-api
2328

29+
- name: Determine version
30+
id: bump
31+
run: |
32+
if [ -n "${{ inputs.version }}" ]; then
33+
echo "version=${{ inputs.version }}" >> $GITHUB_OUTPUT
34+
else
35+
chmod +x ./scripts/fern/bump.sh
36+
VERSION=$(./scripts/fern/bump.sh --from HEAD~1 --group ts-sdk)
37+
echo "version=$VERSION" >> $GITHUB_OUTPUT
38+
fi
39+
2440
- name: Release SDKs
2541
env:
2642
FERN_TOKEN: ${{ secrets.FERN_TOKEN }}
2743
FERN_NPM_TOKEN: ${{ secrets.FERN_NPM_TOKEN }}
2844
run: |
29-
fern generate --group ts-sdk --version ${{ inputs.version }} --log-level debug
45+
fern generate --group ts-sdk --version ${{ steps.bump.outputs.version }} --log-level debug

scripts/fern/bump.sh

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
set -uo pipefail
10+
11+
# The previous commit to compare against.
12+
FROM_COMMIT=""
13+
14+
# The group to use for version detection
15+
GROUP=""
16+
17+
log() {
18+
# Logs are written to stderr.
19+
echo "$@" >&2
20+
}
21+
22+
usage() {
23+
echo "Usage: $0 --from <commit> --group <group>"
24+
echo " --from Previous commit reference (e.g., HEAD~1, 8475a09)"
25+
echo " --group Group to use for version detection (e.g., ts-sdk, java-sdk)"
26+
exit 1
27+
}
28+
29+
fern() {
30+
# Use the version of the fern CLI installed in the user's environment.
31+
FERN_NO_VERSION_REDIRECTION=true command fern "$@"
32+
}
33+
34+
get_latest_version() {
35+
local group=$1
36+
local version=""
37+
38+
case $group in
39+
ts-sdk)
40+
# Get latest version from npm
41+
version=$(npm view intercom-client version 2>/dev/null)
42+
;;
43+
java-sdk)
44+
# Get latest version from maven
45+
version=$(curl -s "https://repo1.maven.org/maven2/io/intercom/intercom-java/maven-metadata.xml" | grep -oP '<version>\K[^<]+' | sort -V | tail -n1)
46+
;;
47+
*)
48+
log "Unknown group: $group"
49+
exit 1
50+
;;
51+
esac
52+
53+
if [[ -z "$version" ]]; then
54+
log "Could not determine latest version for group $group"
55+
exit 1
56+
fi
57+
58+
echo "$version"
59+
}
60+
61+
while [[ $# -gt 0 ]]; do
62+
case $1 in
63+
--from)
64+
FROM_COMMIT="$2"
65+
shift 2
66+
;;
67+
--group)
68+
GROUP="$2"
69+
shift 2
70+
;;
71+
-h|--help)
72+
usage
73+
;;
74+
*)
75+
echo "Unknown option: $1"
76+
usage
77+
;;
78+
esac
79+
done
80+
81+
if [[ -z "$FROM_COMMIT" || -z "$GROUP" ]]; then
82+
usage
83+
fi
84+
85+
# Get the latest version from the registry
86+
FROM_VERSION=$(get_latest_version "$GROUP")
87+
log "Using current version: $FROM_VERSION"
88+
89+
cleanup() {
90+
# Remove the temporary worktree, if any.
91+
if [[ -n "${WORKTREE_DIR:-}" ]] && [[ -d "$WORKTREE_DIR" ]]; then
92+
(cd "$WORKTREE_DIR" && git submodule deinit --all --force >/dev/null 2>&1 || true)
93+
git worktree remove --force "$WORKTREE_DIR" >/dev/null 2>&1 || true
94+
fi
95+
96+
# Remove the from.json and to.json files, if any.
97+
rm -f "$WORK_DIR/from.json" "$WORK_DIR/to.json" >/dev/null 2>&1
98+
99+
# Pop back to the user's original directory.
100+
popd >/dev/null 2>&1
101+
}
102+
103+
trap cleanup EXIT
104+
105+
# Step 0: Navigate to the fern root directory, if not already.
106+
WORK_DIR="$(git rev-parse --show-toplevel)"
107+
pushd "$WORK_DIR" >/dev/null 2>&1
108+
109+
# Step 1: Generate IR from current commit.
110+
log "Generating IR from current commit..."
111+
fern ir to.json
112+
113+
# Step 2: Create worktree and generate IR from previous commit.
114+
WORKTREE_DIR=$(mktemp -d)
115+
log "Generating IR from previous commit..."
116+
git worktree add "$WORKTREE_DIR" "$FROM_COMMIT" >/dev/null 2>&1
117+
(
118+
cd "$WORKTREE_DIR" || {
119+
log "Cannot access worktree directory"
120+
exit 1
121+
}
122+
123+
# Initialize and update git submodules, if any.
124+
git submodule update --init --recursive >/dev/null 2>&1
125+
126+
fern ir from.json
127+
)
128+
129+
# Step 3: Copy the from.json to the current working directory
130+
cp "$WORKTREE_DIR/from.json" "$WORK_DIR/from.json"
131+
132+
# Step 4: Run fern diff.
133+
log "Running fern diff..."
134+
DIFF_OUTPUT=$(fern diff --from from.json --to to.json --from-version "$FROM_VERSION")
135+
136+
# Debug: Print the full diff output
137+
log "Diff output: $DIFF_OUTPUT"
138+
139+
# Step 5: Extract next version using jq.
140+
NEXT_VERSION=$(echo "$DIFF_OUTPUT" | jq -r '.nextVersion')
141+
142+
if [[ -z "$NEXT_VERSION" ]]; then
143+
log "Could not determine next version from 'fern diff' output: $DIFF_OUTPUT"
144+
exit 1
145+
fi
146+
147+
echo "$NEXT_VERSION"

0 commit comments

Comments
 (0)