From b8aeca64612867a8a58ae9525191bf825437fda6 Mon Sep 17 00:00:00 2001 From: lourw <56288712+lourw@users.noreply.github.com> Date: Wed, 11 Sep 2024 09:04:04 -0700 Subject: [PATCH 1/2] fix(nx-cloud): include nxCloudId when generating connect urls --- .../src/utils/nx/nx-cloud.ts | 4 ++-- .../nx/src/nx-cloud/utilities/onboarding.ts | 9 +++++++-- packages/nx/src/utils/nx-cloud-utils.ts | 18 ------------------ 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts b/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts index 9018a3539952d..70ac32fa64e10 100644 --- a/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts +++ b/packages/create-nx-workspace/src/utils/nx/nx-cloud.ts @@ -15,9 +15,9 @@ export function readNxCloudToken(directory: string) { // nx-ignore-next-line )) as typeof import('nx/src/nx-cloud/utilities/get-cloud-options'); - const { accessToken } = getCloudOptions(directory); + const { accessToken, nxCloudId } = getCloudOptions(directory); nxCloudSpinner.succeed('Nx Cloud has been set up successfully'); - return accessToken; + return accessToken || nxCloudId; } export async function getOnboardingInfo( diff --git a/packages/nx/src/nx-cloud/utilities/onboarding.ts b/packages/nx/src/nx-cloud/utilities/onboarding.ts index 19b064fe924c3..114202a3b9146 100644 --- a/packages/nx/src/nx-cloud/utilities/onboarding.ts +++ b/packages/nx/src/nx-cloud/utilities/onboarding.ts @@ -29,6 +29,11 @@ export async function getNxCloudAppOnBoardingUrl(token: string) { export function readNxCloudToken(tree: Tree) { const nxJson = readNxJson(tree); - const { accessToken } = getRunnerOptions('default', nxJson, {}, true); - return accessToken; + const { accessToken, nxCloudId } = getRunnerOptions( + 'default', + nxJson, + {}, + true + ); + return accessToken || nxCloudId; } diff --git a/packages/nx/src/utils/nx-cloud-utils.ts b/packages/nx/src/utils/nx-cloud-utils.ts index f37c932d766fa..2c4392baf846a 100644 --- a/packages/nx/src/utils/nx-cloud-utils.ts +++ b/packages/nx/src/utils/nx-cloud-utils.ts @@ -23,21 +23,3 @@ export function getNxCloudUrl(nxJson: NxJsonConfiguration): string { throw new Error('nx-cloud runner not found in nx.json'); return cloudRunner?.options?.url ?? nxJson.nxCloudUrl ?? 'https://nx.app'; } - -export function getNxCloudToken(nxJson: NxJsonConfiguration): string { - const cloudRunner = Object.values(nxJson.tasksRunnerOptions ?? {}).find( - (r) => r.runner == '@nrwl/nx-cloud' || r.runner == 'nx-cloud' - ); - - if ( - !cloudRunner && - !(nxJson.nxCloudAccessToken || process.env.NX_CLOUD_ACCESS_TOKEN) - ) - throw new Error('nx-cloud runner not found in nx.json'); - - return ( - process.env.NX_CLOUD_ACCESS_TOKEN ?? - cloudRunner?.options.accessToken ?? - nxJson.nxCloudAccessToken - ); -} From 305f02dc3c82fb7d0493df48aeda49ef1b8973e7 Mon Sep 17 00:00:00 2001 From: lourw <56288712+lourw@users.noreply.github.com> Date: Wed, 11 Sep 2024 12:20:12 -0700 Subject: [PATCH 2/2] fix(nx-cloud): update check for access token --- .../connect-to-nx-cloud/connect-to-nx-cloud.ts | 6 ------ .../nx/src/nx-cloud/utilities/get-cloud-options.ts | 4 ++++ .../src/nx-cloud/utilities/is-workspace-claimed.ts | 13 +++++++------ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts b/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts index 9e003b28ca28e..acf9da15f635a 100644 --- a/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts +++ b/packages/nx/src/nx-cloud/generators/connect-to-nx-cloud/connect-to-nx-cloud.ts @@ -140,12 +140,6 @@ export async function connectToNxCloud( const isGitHubDetected = schema.github ?? (await repoUsesGithub(schema.github)); - let responseFromCreateNxCloudWorkspaceV1: - | { - token: string; - } - | undefined; - let responseFromCreateNxCloudWorkspaceV2: | { nxCloudId: string; diff --git a/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts b/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts index 3847c2a324e67..2163f2849c19f 100644 --- a/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts +++ b/packages/nx/src/nx-cloud/utilities/get-cloud-options.ts @@ -21,3 +21,7 @@ export function getCloudUrl() { export function removeTrailingSlash(apiUrl: string) { return apiUrl[apiUrl.length - 1] === '/' ? apiUrl.slice(0, -1) : apiUrl; } + +export function isNxCloudId(token: string): boolean { + return token.length === 24; +} diff --git a/packages/nx/src/nx-cloud/utilities/is-workspace-claimed.ts b/packages/nx/src/nx-cloud/utilities/is-workspace-claimed.ts index aa213fc73c3fc..9638c1d80fb88 100644 --- a/packages/nx/src/nx-cloud/utilities/is-workspace-claimed.ts +++ b/packages/nx/src/nx-cloud/utilities/is-workspace-claimed.ts @@ -1,15 +1,16 @@ -import { getCloudUrl } from './get-cloud-options'; +import { getCloudUrl, isNxCloudId } from './get-cloud-options'; -export async function isWorkspaceClaimed(nxCloudAccessToken) { - if (!nxCloudAccessToken) return false; +export async function isWorkspaceClaimed(accessToken: string) { + if (!accessToken) return false; const apiUrl = getCloudUrl(); try { + const requestData = isNxCloudId(accessToken) + ? { nxCloudId: accessToken } + : { nxCloudAccessToken: accessToken }; const response = await require('axios').post( `${apiUrl}/nx-cloud/is-workspace-claimed`, - { - nxCloudAccessToken, - } + requestData ); if (response.data.message) {