diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 2226c36d7..ef8306be5 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -8,24 +8,41 @@ jobs: runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v5 - name: Use Node.js - uses: actions/setup-node@v3 + uses: actions/setup-node@v5 with: node-version-file: '.nvmrc' + - name: '[Setup] Install dependencies' run: npm ci + - name: '[Build] Staging' run: npm run build:staging + - name: '[Build] Production' run: npm run build:prod + - name: 'Authenticate to Google Cloud' uses: 'google-github-actions/auth@v1' with: credentials_json: '${{ secrets.GOOGLE_CREDENTIALS }}' + - name: '[Deploy] Upload SDK Files to GCS' uses: 'google-github-actions/upload-cloud-storage@v1' with: path: 'build/releases' destination: 'sdk-builds-persistence-onesignal/web-sdk/${{ github.sha }}' parent: false + + - name: '[Deploy] Get SDK Version' + id: get_version + run: echo "SDK_VERSION=$(node -p "require('./package.json').config.sdkVersion")" >> $GITHUB_OUTPUT + + - name: '[Deploy] Create turbine pr' + run: ./build/scripts/turbine.sh + env: + GH_TURBINE_TOKEN: ${{ secrets.GH_TURBINE_TOKEN }} + GITHUB_SHA: ${{ github.sha }} + GITHUB_RUN_ID: ${{ github.run_id }} + SDK_VERSION: ${{ steps.get_version.outputs.SDK_VERSION }} diff --git a/__test__/support/environment/TestEnvironmentHelpers.ts b/__test__/support/environment/TestEnvironmentHelpers.ts index d1bf04c88..f6dc334fd 100644 --- a/__test__/support/environment/TestEnvironmentHelpers.ts +++ b/__test__/support/environment/TestEnvironmentHelpers.ts @@ -16,7 +16,10 @@ import MockNotification from '../mocks/MockNotification'; import TestContext from './TestContext'; import { type TestEnvironmentConfig } from './TestEnvironment'; -declare const global: any; +declare const global: { + OneSignal: typeof OneSignal; + Notification: typeof Notification; +}; export function initOSGlobals(config: TestEnvironmentConfig = {}) { global.OneSignal = OneSignal; @@ -42,7 +45,7 @@ export function initOSGlobals(config: TestEnvironmentConfig = {}) { export function stubNotification(config: TestEnvironmentConfig) { global.Notification = MockNotification; - global.Notification.permission = config.permission + MockNotification.permission = config.permission ? config.permission : global.Notification.permission; } diff --git a/build/scripts/turbine.sh b/build/scripts/turbine.sh new file mode 100755 index 000000000..a889773e9 --- /dev/null +++ b/build/scripts/turbine.sh @@ -0,0 +1,68 @@ +#!/bin/bash +set -euo pipefail + +git clone "https://${GH_TURBINE_TOKEN}@github.com/OneSignal/turbine.git" turbine +cd turbine + +# Configure git identity +git config user.name "github-actions[bot]" +git config user.email "github-actions[bot]@users.noreply.github.com" + +# Create new branch +BRANCH_NAME="web-sdk-${SDK_VERSION}-release" + +# Check if PR already exists +EXISTING_PR=$(curl -s \ + -H "Authorization: Bearer $GH_TURBINE_TOKEN" \ + "https://api.github.com/repos/OneSignal/turbine/pulls?head=OneSignal:$BRANCH_NAME&state=open" | + jq length) + +if [ "$EXISTING_PR" -gt 0 ]; then + echo "PR already exists for branch $BRANCH_NAME" + exit 0 +fi + +git checkout -B "$BRANCH_NAME" + +# Update only USERMODEL_WEB_SDK_VERSION +sed -i "s/USERMODEL_WEB_SDK_VERSION: [a-f0-9]\{40\}/USERMODEL_WEB_SDK_VERSION: ${GITHUB_SHA}/" .circleci/config.yml + +# Check if changes were made +if git diff --exit-code; then + echo "No changes to commit" + exit 0 +fi + +# Commit changes +RELEASE_MESSAGE="Update SDK version to ${SDK_VERSION} for web release" +JOB_MESSAGE="Job that successfully built the artifacts: https://github.com/OneSignal/OneSignal-Website-SDK/actions/runs/${GITHUB_RUN_ID}" + +git add .circleci/config.yml +git commit -m "$RELEASE_MESSAGE" -m "$JOB_MESSAGE" + +# Push to origin (override any existing branch) +git push --force origin "$BRANCH_NAME" + +# Create pull request +PR_NUMBER=$( + curl -sS -X POST \ + -H "Authorization: Bearer $GH_TURBINE_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ + \"title\": \"$RELEASE_MESSAGE\", + \"body\": \"$JOB_MESSAGE\", + \"head\": \"$BRANCH_NAME\", + \"base\": \"main\" + }" \ + https://api.github.com/repos/OneSignal/turbine/pulls | + jq -r '.number' +) + +# Request team reviewers +curl -sS -X POST \ + -H "Authorization: Bearer $GH_TURBINE_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ + \"team_reviewers\": [\"eng-sdk-platform\"] + }" \ + "https://api.github.com/repos/OneSignal/turbine/pulls/${PR_NUMBER}/requested_reviewers" diff --git a/src/onesignal/OneSignal.test.ts b/src/onesignal/OneSignal.test.ts index 7a01c75cc..6bda72368 100644 --- a/src/onesignal/OneSignal.test.ts +++ b/src/onesignal/OneSignal.test.ts @@ -435,6 +435,7 @@ describe('OneSignal - No Consent Required', () => { setTransferSubscriptionResponse(); await OneSignal.login(externalId); // should call set alias + await vi.waitUntil(() => addAliasFn.mock.calls.length === 1); expect(addAliasFn).toHaveBeenCalledWith({ identity: { external_id: externalId, @@ -442,6 +443,7 @@ describe('OneSignal - No Consent Required', () => { }); await OneSignal.login(newExternalId); // should call create user + await vi.waitUntil(() => createUserFn.mock.calls.length === 1); expect(createUserFn).toHaveBeenCalledWith({ identity: { external_id: newExternalId, diff --git a/src/shared/environment/detect.ts b/src/shared/environment/detect.ts index 7cb88601f..93d58f1f7 100644 --- a/src/shared/environment/detect.ts +++ b/src/shared/environment/detect.ts @@ -8,10 +8,10 @@ import { Browser } from '../useragent/constants'; import { getBrowserName, getBrowserVersion } from '../useragent/detect'; import { API_ORIGIN, API_TYPE, IS_SERVICE_WORKER } from '../utils/EnvVariables'; -export const isBrowser = typeof window !== 'undefined'; +export const isBrowser = () => typeof window !== 'undefined'; export const hasSafariWindow = () => - isBrowser && typeof window.safari !== 'undefined'; + isBrowser() && typeof window.safari !== 'undefined'; export const supportsServiceWorkers = () => { if (IS_SERVICE_WORKER) return true; @@ -21,7 +21,7 @@ export const supportsServiceWorkers = () => { export const windowEnvString = IS_SERVICE_WORKER ? 'Service Worker' : 'Browser'; export const useSafariLegacyPush = () => - isBrowser && window.safari?.pushNotification != undefined; + isBrowser() && window.safari?.pushNotification != undefined; export const supportsVapidPush = typeof PushSubscriptionOptions !== 'undefined' &&