Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nx-cloud): include nxCloudId when generating connect urls #27882

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/create-nx-workspace/src/utils/nx/nx-cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions packages/nx/src/nx-cloud/utilities/get-cloud-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 7 additions & 6 deletions packages/nx/src/nx-cloud/utilities/is-workspace-claimed.ts
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to tell when invoking this function whether or not it is an ID or an access token based on where we read it/created it from.

Let's address this separately.

? { nxCloudId: accessToken }
: { nxCloudAccessToken: accessToken };
const response = await require('axios').post(
`${apiUrl}/nx-cloud/is-workspace-claimed`,
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API for this endpoint had changed since it was written. You need to specify the type of token being passed to the API

nxCloudAccessToken,
}
requestData
);

if (response.data.message) {
Expand Down
9 changes: 7 additions & 2 deletions packages/nx/src/nx-cloud/utilities/onboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
18 changes: 0 additions & 18 deletions packages/nx/src/utils/nx-cloud-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}