diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 005b855..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Build React Native Package and Playground -on: - workflow_dispatch: - push: - branches: - - main - pull_request: - types: [opened, synchronize, reopened] - merge_group: -permissions: - contents: read - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Harden the runner - uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0 - with: - egress-policy: audit - - - name: Checkout code - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - - name: Setup Node.js 24.x - uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 - with: - node-version: 24.x - - - name: Install pnpm - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Build - run: pnpm build diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..48912cf --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,411 @@ +name: CI + +on: + pull_request: + merge_group: + push: + branches: + - main + +permissions: {} + +# PR runs cancel superseded runs; runs on main and in the merge queue are never cancelled. +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + lint: + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Check exact dependency versions and lint (warnings fail) + run: pnpm lint + + format: + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Check formatting + run: pnpm format:check + + typecheck: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Type-check the package and the playground + run: pnpm check-types + + build: + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Build the package and the Expo playground + run: pnpm build + + unit-tests: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Run package unit tests with coverage + run: pnpm test:coverage + + - name: Run the playground tests (Expo config plugin, E2E support) + run: pnpm --filter playground test + + - name: Run the repo script tests (release, PR Check Summary) + run: pnpm test:scripts + + - name: Run the E2E harness script tests + run: | + for test in e2e/*/test/*.sh; do + "$test" + done + + - name: Upload coverage for Sonar + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: coverage + path: packages/react-native/coverage/lcov.info + if-no-files-found: error + retention-days: 1 + + api-check: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + # Fails when the public API differs from packages/react-native/etc/react-native.api.md. + # Regenerate it with `pnpm api:update` and commit the result. + - name: Compare the public API with the committed snapshot + run: pnpm api:check + + package-check: + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Build the package + run: pnpm --filter @formbricks/react-native build + + - name: publint, attw, and npm pack installed into a scratch consumer + run: pnpm package-check + + sonarqube: + # Fork PRs have no SONAR_TOKEN; PR Check Summary allows this skip only there. + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + needs: unit-tests + runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + # Sonar's JS/TS analysis runs on the Node on PATH; keep it on the same version as every job. + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + package-manager-cache: false + + # unit-tests produced it; Sonar never re-runs the tests. + - name: Download coverage + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: coverage + path: packages/react-native/coverage + + - name: SonarQube scan (waits for the quality gate) + uses: SonarSource/sonarqube-scan-action@59202af334b4ba7b5db01b701b8e79f8b3bef0f4 # v8.5.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + + dependency-review: + # It compares a base and a head, which only PRs and merge groups have. + if: github.event_name == 'pull_request' || github.event_name == 'merge_group' + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Review dependency changes + uses: actions/dependency-review-action@a1d282b36b6f3519aa1f3fc636f609c47dddb294 # v5.0.0 + with: + base-ref: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha }} + head-ref: ${{ github.event.pull_request.head.sha || github.event.merge_group.head_sha }} + # stream-json 1.9.1 comes in only through Detox (E2E test tooling, a playground + # devDependency); the SDK never ships it and it parses only Detox's own files. + # The fix (3.6.0) is ESM-only and Detox doesn't support it. GitHub can't tell + # dev scope from pnpm-lock, so allow these three until Detox upgrades. + allow-ghsas: GHSA-hqr4-qq8f-hg3x, GHSA-mjw6-4jj6-33hc, GHSA-528h-pc64-c93x + + workflow-lint: + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: actionlint + uses: raven-actions/actionlint@3d39aea434753780c3b3d4a1a31c854b4dbf49d7 # v2.2.0 + with: + version: 1.7.12 + + # actionlint only checks the scripts inside workflows. The runner image ships shellcheck. + - name: shellcheck the E2E harness and repo scripts + run: git ls-files -z 'e2e/*.sh' 'scripts/*.sh' | xargs -0 shellcheck + + - name: zizmor + uses: zizmorcore/zizmor-action@45d7276c9ca790aabd705e31ca99c6185c3e76dd # v0.8.1 + with: + version: 1.32.0 + advanced-security: false + + e2e-tests: + # Fork PRs have no secrets; PR Check Summary allows this skip only there. + if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository + # A called workflow cannot exceed these; e2e.yml polls the run's artifacts for the iOS hand-off. + permissions: + contents: read + actions: read + # actionlint 1.7.12 rejects the `$/` form zizmor suggests. + uses: ./.github/workflows/e2e.yml # zizmor: ignore[self-repository] + secrets: + ENTERPRISE_LICENSE_KEY: ${{ secrets.ENTERPRISE_LICENSE_KEY }} + + pr-check-summary: + name: PR Check Summary + needs: + - lint + - format + - typecheck + - build + - unit-tests + - api-check + - package-check + - sonarqube + - dependency-review + - workflow-lint + - e2e-tests + # `always()`, not `!cancelled()`: this is the only required check, and a skipped required job + # counts as passing. scripts/pr-check-summary.mjs decides which skips are allowed. + if: always() + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + contents: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout the summary script + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + sparse-checkout: scripts/pr-check-summary.mjs + sparse-checkout-cone-mode: false + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + + - name: Fail unless every job passed or was an allowed skip + env: + NEEDS_JSON: ${{ toJSON(needs) }} + EVENT_NAME: ${{ github.event_name }} + IS_FORK: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository }} + run: node scripts/pr-check-summary.mjs diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..a326124 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,49 @@ +name: CodeQL + +on: + schedule: + - cron: "17 3 * * 1" # weekly, Monday 03:17 UTC + workflow_dispatch: + +permissions: {} + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + runs-on: ubuntu-latest + timeout-minutes: 30 + permissions: + actions: read + contents: read + security-events: write + strategy: + fail-fast: false + matrix: + language: + - actions + - javascript-typescript + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Initialize CodeQL + uses: github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 + with: + languages: ${{ matrix.language }} + build-mode: none + + - name: Run CodeQL analysis + uses: github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 + with: + category: /language:${{ matrix.language }} diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml new file mode 100644 index 0000000..23f0f9d --- /dev/null +++ b/.github/workflows/e2e.yml @@ -0,0 +1,362 @@ +name: E2E + +# The SDK golden journey against a published Formbricks image, with Detox driving the Expo +# playground (apps/playground) on both platforms. ci.yml calls it on every PR (fork PRs skip it: no +# secrets) with the latest release; run it by hand with `server_version` (for example 6.3.0-rc.1) +# as the pre-release QA. The server harness lives in e2e/server (shared with the other SDKs). +# +# - `android`: one Linux runner holds the server and the emulator, which reaches it at +# http://10.0.2.2:3000 (the RN SDK accepts http://). +# - `server` + `ios`: GitHub's macOS runners cannot run Docker, so the server runs on Linux behind a +# Cloudflare quick tunnel (e2e/tunnel) and the two jobs, side by side (neither needs the other), swap run +# artifacts: the tunnel URL one way, the journey result the other. A job's outputs only publish +# when it ends, which is why the hand-off uses artifacts. +on: + workflow_call: + inputs: + server_version: + description: Formbricks image tag (ghcr.io/formbricks/formbricks) to test against + type: string + default: latest + secrets: + ENTERPRISE_LICENSE_KEY: + description: Enterprise license with the Contacts feature + required: false + workflow_dispatch: + inputs: + server_version: + description: Formbricks image tag (ghcr.io/formbricks/formbricks) to test against, for example 6.3.0-rc.1 + type: string + default: latest + +permissions: + contents: read + actions: read # the iOS jobs poll the run's artifacts for each other's hand-off + +env: + # Unique per attempt; the Detox retry inside the journey step reuses it, which the survey allows. + E2E_USER_ID: qa-user-${{ github.run_id }}-${{ github.run_attempt }} + E2E_FEEDBACK: Golden journey feedback ${{ github.run_id }} + SERVER_ARTIFACT: e2e-server-attempt-${{ github.run_attempt }} + DONE_ARTIFACT: e2e-journey-done-attempt-${{ github.run_attempt }} + +jobs: + # Checked once here; every other job needs it, so a bad tag stops them all before they start. + server-version: + name: Check the server version + runs-on: ubuntu-latest + timeout-minutes: 5 + outputs: + version: ${{ steps.check.outputs.version }} + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Check the server version + id: check + env: + FORMBRICKS_VERSION: ${{ inputs.server_version || 'latest' }} + run: | + if [[ ! "${FORMBRICKS_VERSION}" =~ ^[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}$ ]]; then + echo "::error::server_version '${FORMBRICKS_VERSION}' is not a valid image tag" + exit 1 + fi + echo "Testing against ghcr.io/formbricks/formbricks:${FORMBRICKS_VERSION}" + echo "version=${FORMBRICKS_VERSION}" >> "$GITHUB_OUTPUT" + + android: + name: Golden journey (Detox, Android) + needs: server-version + runs-on: ubuntu-latest + timeout-minutes: 60 + env: + FORMBRICKS_VERSION: ${{ needs.server-version.outputs.version }} + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Check out the code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + # React Native's Gradle plugin asks for a Java 17 toolchain. + - name: Set up JDK 17 + uses: actions/setup-java@d1374f8ac74474200d16ef8ecee22c7ae7a51880 # v6.3.0 + with: + java-version: "17" + distribution: "temurin" + + # Before the server: it is up about a minute after the image pull, so build first. + - name: Build the playground and the Detox runner + run: e2e/react-native/run-journey.sh android build + + - name: Start the Formbricks server and bootstrap the tenant + env: + ENTERPRISE_LICENSE_KEY: ${{ secrets.ENTERPRISE_LICENSE_KEY }} + run: e2e/server/up.sh + + # Stops with a clear error at the license gate when ENTERPRISE_LICENSE_KEY is missing. + - name: Seed the survey through the V1 API + env: + ENTERPRISE_LICENSE_KEY: ${{ secrets.ENTERPRISE_LICENSE_KEY }} + run: e2e/server/seed.sh + + # Hosted runners already expose /dev/kvm; the emulator only needs permission to use it. + - name: Enable KVM + run: | + echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules + sudo udevadm control --reload-rules + sudo udevadm trigger --name-match=kvm + + # run-journey.sh retries a failed journey once inside this step. + - name: Run the golden journey + uses: reactivecircus/android-emulator-runner@a421e43855164a8197daf9d8d40fe71c6996bb0d # v2.38.0 + with: + api-level: 33 + target: google_apis + arch: x86_64 + profile: pixel_6 + force-avd-creation: true + emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none -accel on -no-snapshot + disable-animations: true + script: e2e/react-native/run-journey.sh android test + + - name: Assert the response through the V1 API + run: e2e/server/assert-response.sh + + - name: Delete the seeded data through the V1 API + run: e2e/server/cleanup.sh + + - name: Stop the server and collect its logs + if: always() + run: e2e/server/down.sh e2e/server/logs + + - name: Upload the server logs and Detox artifacts + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: e2e-golden-journey-android-attempt-${{ github.run_attempt }} + path: | + e2e/server/logs + e2e/react-native/artifacts + if-no-files-found: ignore + retention-days: 7 + + server: + name: Server and tunnel for iOS (Linux) + needs: server-version + runs-on: ubuntu-latest + timeout-minutes: 60 + env: + FORMBRICKS_VERSION: ${{ needs.server-version.outputs.version }} + CLOUDFLARED_VERSION: 2026.10.7 + CLOUDFLARED_SHA256: 1f99fb518b83dec2ba8d870a7cc991d82a77e6196295a3d2754ba17ab9f91872 + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Check out the code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # The pair swaps per-attempt artifacts, so both must run in every attempt. + - name: Stop a partial re-run + env: + GH_TOKEN: ${{ github.token }} + run: e2e/tunnel/require-full-rerun.sh "Golden journey (Detox, iOS)" + + - name: Install cloudflared + run: | + curl -fsSL -o "$RUNNER_TEMP/cloudflared" \ + "https://github.com/cloudflare/cloudflared/releases/download/${CLOUDFLARED_VERSION}/cloudflared-linux-amd64" + echo "${CLOUDFLARED_SHA256} $RUNNER_TEMP/cloudflared" | sha256sum -c - + chmod +x "$RUNNER_TEMP/cloudflared" + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + + - name: Start the Formbricks server and bootstrap the tenant + env: + ENTERPRISE_LICENSE_KEY: ${{ secrets.ENTERPRISE_LICENSE_KEY }} + run: e2e/server/up.sh + + # Stops with a clear error at the license gate when ENTERPRISE_LICENSE_KEY is missing. + - name: Seed the survey through the V1 API + env: + ENTERPRISE_LICENSE_KEY: ${{ secrets.ENTERPRISE_LICENSE_KEY }} + run: e2e/server/seed.sh + + - name: Open a tunnel to the server + run: e2e/tunnel/start.sh + + - name: Hand the tunnel URL to the macOS job + id: publish + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ env.SERVER_ARTIFACT }} + path: e2e/tunnel/.state/server.env + if-no-files-found: error + retention-days: 1 + + # Without this the macOS job would wait for a URL that never comes. + - name: Tell the macOS job the server failed + if: failure() && steps.publish.outcome != 'success' + run: | + mkdir -p e2e/tunnel/.state + echo "E2E_SERVER_FAILED=1" > e2e/tunnel/.state/server.env + + - name: Hand the failure to the macOS job + if: failure() && steps.publish.outcome != 'success' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ env.SERVER_ARTIFACT }} + path: e2e/tunnel/.state/server.env + retention-days: 1 + + - name: Keep the server up until the macOS journey is done + env: + GH_TOKEN: ${{ github.token }} + run: | + e2e/tunnel/await-artifact.sh "${DONE_ARTIFACT}" e2e/tunnel/.state/done 3000 + if ! grep -qx 'E2E_JOURNEY_RESULT=success' e2e/tunnel/.state/done/journey.env; then + echo "::error::the macOS journey did not pass ($(cat e2e/tunnel/.state/done/journey.env)); see the Golden journey (Detox, iOS) job" + exit 1 + fi + + - name: Assert the response through the V1 API + run: e2e/server/assert-response.sh + + - name: Delete the seeded data through the V1 API + run: e2e/server/cleanup.sh + + - name: Close the tunnel + if: always() + run: e2e/tunnel/stop.sh e2e/server/logs + + - name: Stop the server and collect its logs + if: always() + run: e2e/server/down.sh e2e/server/logs + + - name: Upload the server logs + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: e2e-golden-journey-ios-server-attempt-${{ github.run_attempt }} + path: e2e/server/logs + if-no-files-found: ignore + retention-days: 7 + + ios: + name: Golden journey (Detox, iOS) + # Only for the order: without a valid version the server job never starts, and this job + # would wait for its tunnel URL in vain. + needs: server-version + runs-on: macos-26 + timeout-minutes: 60 + env: + XCODE_VERSION: "26.3" + HOMEBREW_NO_AUTO_UPDATE: "1" + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Check out the code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # The pair swaps per-attempt artifacts, so both must run in every attempt. + - name: Stop a partial re-run + env: + GH_TOKEN: ${{ github.token }} + run: e2e/tunnel/require-full-rerun.sh "Server and tunnel for iOS (Linux)" + + - name: Select Xcode + # xcode-select, not DEVELOPER_DIR, so the simulator service follows it too. + run: sudo xcode-select --switch "/Applications/Xcode_${XCODE_VERSION}.app" + + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Set up Node.js 24 + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + # Detox lists and picks simulators with applesimutils. + - name: Install applesimutils + run: | + brew tap wix/brew + brew trust --formula wix/brew/applesimutils + brew install applesimutils + + # Before waiting for the server: the build overlaps the server's boot. + - name: Build the playground and the Detox framework + run: e2e/react-native/run-journey.sh ios build + + - name: Wait for the server's tunnel URL + id: server + env: + GH_TOKEN: ${{ github.token }} + run: | + e2e/tunnel/await-artifact.sh "${SERVER_ARTIFACT}" "$RUNNER_TEMP/server" 2400 + # Assigned first: a failing command substitution inside `echo` would not fail the step. + url="$(e2e/tunnel/server-url.sh "$RUNNER_TEMP/server/server.env")" + echo "url=${url}" >> "$GITHUB_OUTPUT" + + # run-journey.sh retries a failed journey once inside this step. + - name: Run the golden journey + id: journey + env: + E2E_APP_URL: ${{ steps.server.outputs.url }} + run: e2e/react-native/run-journey.sh ios test + + # Always, so the server job stops waiting even when this job failed earlier. + - name: Tell the server job the journey is done + if: always() + env: + RESULT: ${{ steps.journey.outcome }} + run: | + mkdir -p "$RUNNER_TEMP/done" + printf 'E2E_JOURNEY_RESULT=%s\n' "${RESULT:-skipped}" > "$RUNNER_TEMP/done/journey.env" + + - name: Hand the result to the server job + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ env.DONE_ARTIFACT }} + path: ${{ runner.temp }}/done/journey.env + retention-days: 1 + + - name: Upload the Detox artifacts + if: always() + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: e2e-golden-journey-ios-attempt-${{ github.run_attempt }} + path: e2e/react-native/artifacts + if-no-files-found: ignore + retention-days: 7 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index 35cb28a..0000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Lint and Type Check -on: - workflow_dispatch: - push: - branches: - - main - pull_request: - types: [opened, synchronize, reopened] - merge_group: -permissions: - contents: read - -jobs: - lint: - runs-on: ubuntu-latest - - steps: - - name: Harden the runner - uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0 - with: - egress-policy: audit - - - name: Checkout code - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - - name: Setup Node.js 24.x - uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 - with: - node-version: 24.x - - - name: Install pnpm - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Lint - run: pnpm lint - - - name: Check types - run: pnpm check-types diff --git a/.github/workflows/pr-title.yml b/.github/workflows/pr-title.yml new file mode 100644 index 0000000..a6aa3a6 --- /dev/null +++ b/.github/workflows/pr-title.yml @@ -0,0 +1,48 @@ +name: PR title + +on: + pull_request: + types: + - opened + - edited + - reopened + - synchronize + +permissions: {} + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + semantic-pr-title: + name: Semantic PR title + runs-on: ubuntu-latest + timeout-minutes: 5 + permissions: + contents: read + pull-requests: read + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + # The GitHub release notes are built from PR titles, so they must follow Conventional Commits. + - name: Check the PR title follows Conventional Commits + uses: amannn/action-semantic-pull-request@2252a14e810aa9b85c2d560bd7070c802206ecf6 # v6.1.2 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + types: | + fix + feat + chore + docs + style + refactor + perf + test + build + ci + revert diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ba0c29e..dd5c647 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,55 +1,210 @@ name: Release React Native SDK -permissions: - id-token: write # Required for OIDC - contents: read +# Publishing a GitHub release publishes the SDK. The version comes from the tag; +# see RELEASING.md. This workflow only builds, publishes and verifies: CI already +# ran on main, and the manual QA run in RELEASING.md covers the rest. on: release: - types: [released] + types: [published] + +permissions: {} + +# One group for every release: each run reads npm's dist-tags and then publishes, so two runs +# at once could both see the same `latest` and move it backwards. GitHub keeps only one pending +# run per group; a third release cancels the waiting one, which is then re-run (RELEASING.md). +concurrency: + group: release + cancel-in-progress: false + +env: + PACKAGE_NAME: "@formbricks/react-native" + PACKAGE_DIR: packages/react-native jobs: - release: - name: "Release React Native Package" + build: + name: Build runs-on: ubuntu-latest - timeout-minutes: 30 + timeout-minutes: 15 + permissions: + contents: read + outputs: + version: ${{ steps.release.outputs.version }} steps: - name: Harden the runner - uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0 + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 with: egress-policy: audit - - name: Checkout code - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + # The tag commit, so the scripts come from the release, not the default branch. + - name: Checkout the release tag + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/tags/${{ github.event.release.tag_name }} + persist-credentials: false - - name: Setup Node.js - uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 + - name: Install pnpm + uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + - name: Setup Node.js 24.x + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version: 24.x - registry-url: "https://registry.npmjs.org" + package-manager-cache: false # no caches in a publishing workflow - - name: Install pnpm - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 + - name: Read the version on the latest dist-tag + id: npm + run: node scripts/release/npm-registry.mjs latest "$PACKAGE_NAME" >> "$GITHUB_OUTPUT" + + # Checks the tag before building. The publish job picks the dist-tag again from fresh + # registry state, so a re-run of a failed publish never acts on a stale `latest`. + - name: Resolve the version and dist-tag from the tag + id: release + env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + RELEASE_PRERELEASE: ${{ github.event.release.prerelease }} + CURRENT_LATEST: ${{ steps.npm.outputs.latest }} + run: >- + node scripts/release/resolve-release.mjs + "$RELEASE_TAG" "$RELEASE_PRERELEASE" --latest "$CURRENT_LATEST" + >> "$GITHUB_OUTPUT" - name: Install dependencies run: pnpm install --frozen-lockfile - - name: Lint - run: pnpm lint + - name: Write the version into the package manifest + working-directory: ${{ env.PACKAGE_DIR }} + env: + VERSION: ${{ steps.release.outputs.version }} + run: npm pkg set "version=$VERSION" - - name: Check types - run: pnpm check-types + - name: Build + run: pnpm exec turbo run build --filter="$PACKAGE_NAME" - - name: Build package + # A fixed file name keeps the publish step free of globs. + - name: Pack + working-directory: ${{ env.PACKAGE_DIR }} run: | - cd packages/react-native - pnpm build + pnpm pack --pack-destination "$RUNNER_TEMP/pack" + mv "$RUNNER_TEMP"/pack/*.tgz "$RUNNER_TEMP/package.tgz" + + - name: Upload the package + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: package + path: ${{ runner.temp }}/package.tgz + if-no-files-found: error + retention-days: 7 + + publish: + name: Publish to npm + needs: build + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + id-token: write # npm trusted publishing (OIDC) + outputs: + dist-tag: ${{ steps.release.outputs.dist-tag }} + already-published: ${{ steps.registry.outputs.exists }} - - name: Run tests - run: pnpm test:coverage + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + # The tag commit, so the scripts come from the release, not the default branch. + - name: Checkout the release tag + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/tags/${{ github.event.release.tag_name }} + persist-credentials: false + + - name: Setup Node.js 24.x + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24.x + package-manager-cache: false # no caches in a publishing workflow + registry-url: "https://registry.npmjs.org" + + - name: Download the package + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: package + path: ${{ runner.temp }}/package + + - name: Read the version on the latest dist-tag + id: npm + run: node scripts/release/npm-registry.mjs latest "$PACKAGE_NAME" >> "$GITHUB_OUTPUT" + + # A prerelease goes to `next`, a stable version to `latest` unless it is older than the + # current latest (a backport), which goes to `release-.`. + - name: Resolve the dist-tag from the tag + id: release + env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + RELEASE_PRERELEASE: ${{ github.event.release.prerelease }} + CURRENT_LATEST: ${{ steps.npm.outputs.latest }} + run: >- + node scripts/release/resolve-release.mjs + "$RELEASE_TAG" "$RELEASE_PRERELEASE" --latest "$CURRENT_LATEST" + >> "$GITHUB_OUTPUT" + + - name: Check whether the version is already on npm + id: registry + env: + VERSION: ${{ needs.build.outputs.version }} + run: node scripts/release/npm-registry.mjs exists "$PACKAGE_NAME" "$VERSION" >> "$GITHUB_OUTPUT" + + - name: Publish + if: steps.registry.outputs.exists == 'false' + env: + DIST_TAG: ${{ steps.release.outputs.dist-tag }} + run: npm publish "$RUNNER_TEMP/package/package.tgz" --tag "$DIST_TAG" --access public + + - name: Skip a version that is already published + if: steps.registry.outputs.exists == 'true' + env: + VERSION: ${{ needs.build.outputs.version }} + run: echo "::notice::$PACKAGE_NAME@$VERSION is already on npm, skipping publish." + + verify: + name: Verify on npm + needs: [build, publish] + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + # The tag commit, so the scripts come from the release, not the default branch. + - name: Checkout the release tag + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + ref: refs/tags/${{ github.event.release.tag_name }} + persist-credentials: false + + - name: Setup Node.js 24.x + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version: 24.x + package-manager-cache: false # no caches in a publishing workflow - - name: Publish to npm + # Also passes when the dist-tag already moved on to a newer version on the same channel, + # so re-running an old release stays green. + - name: Wait for the version on its dist-tag + env: + VERSION: ${{ needs.build.outputs.version }} + DIST_TAG: ${{ needs.publish.outputs.dist-tag }} + ALREADY_PUBLISHED: ${{ needs.publish.outputs.already-published }} run: | - cd packages/react-native - npm publish --access public + args=(--timeout-seconds 600 --interval-seconds 15) + if [[ "$ALREADY_PUBLISHED" == true ]]; then args+=(--already-published); fi + node scripts/release/npm-registry.mjs verify "$PACKAGE_NAME" "$VERSION" "$DIST_TAG" "${args[@]}" diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml new file mode 100644 index 0000000..b6fce53 --- /dev/null +++ b/.github/workflows/scorecard.yml @@ -0,0 +1,52 @@ +name: OpenSSF Scorecard + +on: + schedule: + - cron: "41 3 * * 1" # weekly, Monday 03:41 UTC + workflow_dispatch: + +permissions: {} + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: false + +jobs: + analysis: + name: Scorecard analysis + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + actions: read + security-events: write # upload the SARIF to code scanning + id-token: write # publish results to the OpenSSF API + steps: + - name: Harden the runner + uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4 + with: + egress-policy: audit + + - name: Checkout code + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Run Scorecard + uses: ossf/scorecard-action@fa7117db83c52d787ae5a89f350999701505d78e # v2.5.0 + with: + results_file: results.sarif + results_format: sarif + publish_results: true + + - name: Upload the results as an artifact + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: scorecard-results + path: results.sarif + retention-days: 5 + + - name: Upload the results to code scanning + uses: github/codeql-action/upload-sarif@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 + with: + sarif_file: results.sarif diff --git a/.github/workflows/sonarqube.yml b/.github/workflows/sonarqube.yml deleted file mode 100644 index 5d332d2..0000000 --- a/.github/workflows/sonarqube.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: SonarQube -on: - workflow_dispatch: - push: - branches: - - main - pull_request: - types: [opened, synchronize, reopened] - merge_group: -permissions: - contents: read - -jobs: - sonarqube: - name: SonarQube - runs-on: ubuntu-latest - steps: - - name: Harden the runner - uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0 - with: - egress-policy: audit - - - name: Checkout code - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.24 - - - name: Setup Node.js 22.x - uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 - with: - node-version: 22.x - - - name: Install pnpm - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Run tests with coverage - run: | - pnpm test:coverage - - - name: SonarQube Scan - uses: SonarSource/sonarqube-scan-action@2500896589ef8f7247069a56136f8dc177c27ccf - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index c0aae26..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,39 +0,0 @@ -name: Run unit tests for react-native package -on: - workflow_dispatch: - push: - branches: - - main - pull_request: - types: [opened, synchronize, reopened] - merge_group: -permissions: - contents: read - -jobs: - test: - name: "Test React Native Package" - runs-on: ubuntu-latest - - steps: - - name: Harden the runner - uses: step-security/harden-runner@ec9f2d5744a09debf3a187a3f4f675c53b671911 # v2.13.0 - with: - egress-policy: audit - - - name: Checkout code - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - - name: Setup Node.js 24.x - uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0 - with: - node-version: 24.x - - - name: Install pnpm - uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.1.0 - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Test - run: pnpm test diff --git a/AGENTS.md b/AGENTS.md index 8550b00..4a91137 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,7 +6,7 @@ This repository is a `pnpm` workspace with two main packages: - `packages/react-native`: the published SDK (`@formbricks/react-native`). Source lives in `src/`, grouped by domain under `src/lib/`, with React components in `src/components/` and shared types in `src/types/`. - `apps/playground`: an Expo app for manual testing and integration checks. App code is in `src/`, with static assets in `assets/`. -GitHub Actions under `.github/workflows/` enforce build, lint, type-check, test, and release steps. +GitHub Actions run every check from `.github/workflows/ci.yml`; its `PR Check Summary` job is the only required check. `release.yml` publishes. ## Build, Test, and Development Commands Run commands from the repository root unless package-scoped work is needed. @@ -14,10 +14,15 @@ Run commands from the repository root unless package-scoped work is needed. - `pnpm install --frozen-lockfile`: install workspace dependencies. - `pnpm dev`: run Turbo watchers across packages. - `pnpm build`: build the SDK and playground. -- `pnpm lint`: enforce exact dependency versions, then run Biome checks. +- `pnpm lint`: enforce exact dependency versions, then run Biome lint (warnings fail). +- `pnpm format` / `pnpm format:check`: format with Biome, or only check formatting. - `pnpm check-types`: run TypeScript checks across the workspace. - `pnpm test`: run unit tests with Vitest. -- `pnpm test:coverage`: generate coverage reports used in release validation. +- `pnpm test:coverage`: run the unit tests with coverage; CI's `unit-tests` job runs this and hands the report to SonarQube. +- `pnpm test:scripts`: test the CI and release helper scripts under `scripts/`. +- `pnpm api:check`: compare the public API with `packages/react-native/etc/react-native.api.md` (api-extractor in check mode); CI's `api-check` runs it. +- `pnpm api:update`: regenerate `packages/react-native/etc/react-native.api.md`. Commit it whenever the public API changes. +- `pnpm package-check`: after a build, run publint, attw, and install the `npm pack` tarball into a scratch consumer. - `pnpm --filter playground ios` / `pnpm --filter playground android`: launch the Expo playground on a device target. ## Coding Style & Naming Conventions @@ -26,7 +31,7 @@ Use TypeScript with strict compiler settings. Biome is the formatter and linter; Follow existing naming patterns: React components in lowercase kebab-style filenames such as `survey-web-view.tsx`, domain modules in concise lowercase names such as `state.ts`, and tests as `*.test.ts`. Keep imports using the `@/` alias inside `packages/react-native` when referencing `src/`. ## Testing Guidelines -Unit tests live beside each domain under `src/lib/**/tests/`. Prefer small, focused Vitest suites and place reusable doubles in local `__mocks__/` folders. Add or update tests with every behavior change; there is no published coverage threshold, but `test:coverage` is part of the release workflow, so changed code should stay covered. +Unit tests live beside each domain under `src/lib/**/tests/`. Prefer small, focused Vitest suites and place reusable doubles in local `__mocks__/` folders. Add or update tests with every behavior change; there is no published coverage threshold, but CI's `unit-tests` job runs `test:coverage` and SonarQube reads the report, so changed code should stay covered. ## Commit & Pull Request Guidelines Recent history mostly follows Conventional Commit-style subjects such as `feat:`, `fix:`, and `chore:` with short imperative summaries. Keep commits narrowly scoped and descriptive. diff --git a/RELEASING.md b/RELEASING.md new file mode 100644 index 0000000..800f0bc --- /dev/null +++ b/RELEASING.md @@ -0,0 +1,104 @@ +# Releasing `@formbricks/react-native` + +Publishing a GitHub release publishes the SDK to npm. The tag is the version: there is no +version-bump PR, and `packages/react-native/package.json` on `main` keeps the placeholder +`0.0.0-dev`. + +The cross-SDK runbook, with the manual QA how-to and the golden journey, is the Notion doc +"SDK QA & Release": https://app.notion.com/p/419e5de84a5881cd8091c30193db8607. + +## What happens when you publish a release + +`.github/workflows/release.yml` runs on `release: published`. Every job checks out the tag +commit, so the release scripts come from the tag, not from `main`. It does three jobs: + +1. **Build** reads the tag, checks that it is plain semver and that it agrees with the GitHub + "pre-release" box, and picks the npm dist-tag (see the table). It writes the version into + `packages/react-native/package.json` on the runner only, then builds and packs the SDK. +2. **Publish** reads npm again and picks the dist-tag from what is there now, so a re-run never + moves `latest` back to an older version. It publishes the tarball with npm trusted publishing + (OIDC, no token). If the version is already on npm, it skips the publish, so re-running is + always safe. +3. **Verify** polls npm until the version is published and its dist-tag points at it, or at a + newer version on the same channel (so re-running an old release stays green). It fails after + 10 minutes. + +Release runs never overlap: a second release waits for the first to finish. GitHub keeps only +one waiting run, so publishing a third release while one runs and one waits cancels the waiting +one. Re-run the cancelled run once the others are done. + +| Tag | GitHub "Set as a pre-release" | npm dist-tag | +| --------------- | ----------------------------- | ------------------------------------------------ | +| `3.4.0` | unticked | `latest`, if 3.4.0 ≥ the current `latest` | +| `3.3.2` | unticked | `release-3.3`, if 3.3.2 < the current `latest` | +| `3.4.0-rc.1` | ticked | `next` | +| `3.4.0-beta.2` | ticked | `next` | + +The tag is plain semver: a `v` prefix (`v3.4.0`) and build metadata (`+build.1`) are rejected. A +tag and box that disagree fail the build job before anything is published. A backport never moves +`latest`. + +The release workflow does not run lint or tests: CI already ran on `main`. Nothing checks that QA +ran, so run it before you release. + +## Cut a release + +1. Make sure CI on `main` is green. +2. Run QA: Actions → **E2E** (`.github/workflows/e2e.yml`) → **Run workflow** on `main`, with + `server_version` set to the Formbricks image you release against (default `latest`). See the + Notion doc for what to check. +3. Releases → **Draft a new release**. +4. Choose a tag: create `X.Y.Z` (no `v`) on `main`. +5. Click **Generate release notes**. They come from the semantic PR titles. Edit if needed. +6. Leave **Set as a pre-release** unticked and tick **Set as the latest release**. +7. **Publish release**, then watch the **Release React Native SDK** run until `Verify on npm` is green. + +## Cut a prerelease + +Same as above, with two differences: + +- Tag `X.Y.Z-rc.N` (or `-beta.N`, `-alpha.N`). +- Tick **Set as a pre-release**. + +It publishes to the `next` dist-tag, so `npm install @formbricks/react-native` keeps resolving to the last +stable version. Testers install it with `npm install @formbricks/react-native@next`, or pin the exact version. + +To ship the final version, cut a new stable release (`X.Y.Z`). A prerelease is never moved to +`latest`. + +## Cut a backport + +A stable version older than the current `latest` (for example `3.3.2` while `latest` is `3.4.0`) +publishes to the `release-3.3` dist-tag and leaves `latest` alone. Users install it with +`npm install @formbricks/react-native@release-3.3`, or pin the exact version. + +Same steps as a release, with two differences: + +- Create the tag `X.Y.Z` on the release branch you backported to, not on `main`, and run QA on + that branch. +- Untick **Set as the latest release**. + +## Recover from a failure + +Never delete a tag, and never unpublish from npm. "Re-run" replays the original release event, +so editing the release afterwards does not change what a re-run does. + +| What failed | What to do | +| -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| Build: tag is not semver | Delete the GitHub release (not the tag). Publish a new release on a correct tag. | +| Build: tag and "pre-release" box disagree | Delete the GitHub release (not the tag). Create a release again on the same tag with the box set correctly. | +| Build: install, build or pack failed | If it was flaky, **Re-run failed jobs**. If the code is broken, fix it on `main` and release the next patch version. | +| Publish failed | **Re-run failed jobs**. If the version reached npm before the failure, the publish step is skipped. | +| Verify timed out | Run `npm view @formbricks/react-native dist-tags`. If the version is there now, re-run the failed job. If the dist-tag points elsewhere, a maintainer runs `npm dist-tag add @formbricks/react-native@X.Y.Z `. | +| A bad version shipped | Release a fixed patch version. If needed, point `latest` back with `npm dist-tag add @formbricks/react-native@ latest` and `npm deprecate @formbricks/react-native@ ""`. | + +## Maintaining the workflow + +- npm trusted publishing is bound to the file name `release.yml`. Renaming it breaks publishing + until the trusted publisher on npmjs.com is updated. +- The release logic lives in `scripts/release/` and has tests. Run them with `pnpm test:scripts`; + CI runs them in `unit-tests`. +- The workflow runs the scripts from the tag, so a fix to them ships with the next tag. +- Tags are plain versions (`3.3.1`). The one exception is `v2.2.0`, the tag of the 2.2.0 release + (there is no `2.2.0` tag). It stays, because tags are never deleted and it backs a published + GitHub release. diff --git a/apps/playground/.detoxrc.js b/apps/playground/.detoxrc.js new file mode 100644 index 0000000..bf6187e --- /dev/null +++ b/apps/playground/.detoxrc.js @@ -0,0 +1,51 @@ +// Detox configuration for the golden journey (e2e/golden-journey.e2e.js). The native projects +// come from `expo prebuild` (plugins/with-detox.js); release builds bundle the JS, so no Metro. +// CI drives it through e2e/react-native/run-journey.sh at the repository root. + +/** @type {Detox.DetoxConfig} */ +module.exports = { + testRunner: { + args: { $0: "jest", config: "e2e/jest.config.js" }, + jest: { setupTimeout: 300_000 }, + }, + artifacts: { + rootDir: process.env.DETOX_ARTIFACTS_DIR ?? "e2e/artifacts", + plugins: { + log: "failing", + screenshot: "failing", + uiHierarchy: "enabled", + }, + }, + apps: { + "ios.release": { + type: "ios.app", + binaryPath: + "ios/build/Build/Products/Release-iphonesimulator/FormbricksPlayground.app", + build: + "xcodebuild -workspace ios/FormbricksPlayground.xcworkspace -scheme FormbricksPlayground -configuration Release -destination 'generic/platform=iOS Simulator' -derivedDataPath ios/build ARCHS=arm64 COMPILER_INDEX_STORE_ENABLE=NO", + }, + "android.release": { + type: "android.apk", + binaryPath: "android/app/build/outputs/apk/release/app-release.apk", + testBinaryPath: + "android/app/build/outputs/apk/androidTest/release/app-release-androidTest.apk", + // x86_64 only: the CI emulator's ABI, and a quarter of the native build time. + build: `cd android && ./gradlew --console=plain assembleRelease assembleAndroidTest -DtestBuildType=release -PreactNativeArchitectures=${process.env.DETOX_ANDROID_ABI ?? "x86_64"}`, + }, + }, + devices: { + simulator: { + type: "ios.simulator", + device: { type: process.env.DETOX_IOS_DEVICE ?? "iPhone 17" }, + }, + // The emulator the CI step already booted (reactivecircus/android-emulator-runner). + emulator: { + type: "android.attached", + device: { adbName: process.env.DETOX_ADB_NAME ?? "emulator-.*" }, + }, + }, + configurations: { + "ios.sim.release": { device: "simulator", app: "ios.release" }, + "android.emu.release": { device: "emulator", app: "android.release" }, + }, +}; diff --git a/apps/playground/.gitignore b/apps/playground/.gitignore index 954fc66..bcadf1c 100644 --- a/apps/playground/.gitignore +++ b/apps/playground/.gitignore @@ -35,3 +35,7 @@ yarn-error.* # typescript *.tsbuildinfo + +# Generated by `expo prebuild` (Detox E2E builds) +/android +/ios diff --git a/apps/playground/app.json b/apps/playground/app.json index cee233a..fc1116f 100644 --- a/apps/playground/app.json +++ b/apps/playground/app.json @@ -4,15 +4,17 @@ "adaptiveIcon": { "backgroundColor": "#ffffff", "foregroundImage": "./assets/adaptive-icon.png" - } + }, + "package": "com.formbricks.playground" }, "assetBundlePatterns": ["**/*"], "icon": "./assets/icon.png", "ios": { - "supportsTablet": true + "supportsTablet": true, + "bundleIdentifier": "com.formbricks.playground" }, "jsEngine": "hermes", - "name": "react-native-demo", + "name": "FormbricksPlayground", "newArchEnabled": true, "orientation": "portrait", "slug": "react-native-demo", @@ -25,6 +27,7 @@ "version": "1.0.0", "web": { "favicon": "./assets/favicon.png" - } + }, + "plugins": ["./plugins/with-detox"] } } diff --git a/apps/playground/e2e/golden-journey.e2e.js b/apps/playground/e2e/golden-journey.e2e.js new file mode 100644 index 0000000..d65303a --- /dev/null +++ b/apps/playground/e2e/golden-journey.e2e.js @@ -0,0 +1,265 @@ +/** + * The golden journey shared by every Formbricks SDK, against the E2E server harness (e2e/server at + * the repository root). + * + * e2e/server/seed.sh created the survey: NPS (qa_nps) then open text (qa_feedback), triggered by + * `qa-action` and targeted to plan = pro. e2e/server/assert-response.sh then checks the stored + * response through the V1 API, so the journey must use the same E2E_USER_ID and E2E_FEEDBACK. + * + * Env: E2E_APP_URL (Android emulator: http://10.0.2.2:3000; iOS: the tunnel URL), E2E_USER_ID, + * E2E_FEEDBACK, optional E2E_WORKSPACE_ID. + */ +const { by, device, element, expect, waitFor, web } = require("detox"); +const { poll } = require("./support/poll"); + +/** The app is gone (crashed or killed): no amount of waiting helps, so fail the attempt now. */ +const appIsGone = (error) => + /can't seem to connect to the test app/.test(String(error?.message)); +const until = (attempt, options) => + poll(attempt, { isFatal: appIsGone, ...options }); + +const required = (name) => { + const value = process.env[name]; + if (!value) throw new Error(`Set ${name} for the golden journey`); + return value; +}; + +const appUrl = required("E2E_APP_URL"); +const userId = required("E2E_USER_ID"); +const feedback = required("E2E_FEEDBACK"); +const workspaceId = + process.env.E2E_WORKSPACE_ID ?? "clsdkqaws00000000000000001"; + +// Selectors: the survey renderer's DOM, the same in every SDK's WebView. +const NPS_9 = "//input[@name='qa_nps' and @value='9']/parent::label"; +const FEEDBACK_INPUT = "#qa_feedback-input"; +const THANKS_HEADLINE = + "//*[@data-fb-part='headline'][contains(normalize-space(.), 'Thanks!')]"; +const CLOSE_BUTTON = "[data-fb-part='button-close']"; +const primaryButton = (card) => + `#questionCard-${card} [data-fb-part='button-primary']`; + +const SURVEY_OPEN_TIMEOUT_MS = 150_000; +const TRACK_RETRY_INTERVAL_MS = 10_000; +const STEP_TIMEOUT_MS = 30_000; +const RESPONSE_TIMEOUT_MS = 60_000; + +// The SDK's WebView is the only one in the app, so `web` needs no matcher. +const webElement = (matcher) => web.element(matcher); + +const expectStatus = (text) => + waitFor(element(by.id("status"))) + .toHaveText(text) + .withTimeout(STEP_TIMEOUT_MS); + +const fill = async (testID, text) => { + await waitFor(element(by.id(testID))) + .toBeVisible() + .whileElement(by.id("playground-scroll")) + .scroll(150, "down"); + await element(by.id(testID)).replaceText(text); + await platform().afterFill(testID); +}; + +const tap = async (testID) => { + await waitFor(element(by.id(testID))) + .toBeVisible() + .whileElement(by.id("playground-scroll")) + .scroll(150, "down"); + await element(by.id(testID)).tap(); +}; + +/** + * Runs `fn` (a function source taking the element and `args`) in the WebView and returns its + * result as a string. Detox for iOS passes a runScript result back only when it is a string (a + * boolean comes back undefined), and hands an empty string over as `{ result: "" }`. + */ +const read = async (matcher, fn, args = []) => { + const result = await webElement(matcher).runScript( + `(el, ...args) => String((${fn})(el, ...args))`, + args, + ); + return typeof result === "string" ? result : (result?.result ?? ""); +}; + +/** + * Everything that differs between the platforms, picked once per run by `platform()`. + * + * - `afterFill`: iOS leaves the keyboard up after replaceText, and the survey would open + * underneath it. + * - `press` clicks a survey element. Android: Espresso-Web's click. iOS: the element's own + * click(), which runs the browser's activation behavior (a