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): use create-workspace-v1 endpoint if v2 returns with 404 #28015

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ function getNxInitDate(): string | null {
}
}

async function createNxCloudWorkspaceV1(
workspaceName: string,
installationSource: string,
nxInitDate: string | null
): Promise<{ token: string; url: string }> {
const apiUrl = getCloudUrl();
const response = await require('axios').post(
`${apiUrl}/nx-cloud/create-org-and-workspace`,
{
workspaceName,
installationSource,
nxInitDate,
}
);

if (response.data.message) {
throw new Error(response.data.message);
}

return response.data;
}

async function createNxCloudWorkspaceV2(
workspaceName: string,
installationSource: string,
Expand Down Expand Up @@ -103,6 +125,29 @@ export interface ConnectToNxCloudOptions {
generateToken?: boolean;
}

function addNxCloudAccessTokenToNxJson(
tree: Tree,
token: string,
directory: string = ''
) {
const nxJsonPath = join(directory, 'nx.json');
if (tree.exists(nxJsonPath)) {
updateJson<NxJsonConfiguration>(
tree,
join(directory, 'nx.json'),
(nxJson) => {
const overrideUrl = process.env.NX_CLOUD_API || process.env.NRWL_API;
if (overrideUrl) {
nxJson.nxCloudUrl = overrideUrl;
}
nxJson.nxCloudAccessToken = token;

return nxJson;
}
);
}
}

function addNxCloudIdToNxJson(
tree: Tree,
nxCloudId: string,
Expand Down Expand Up @@ -140,6 +185,12 @@ export async function connectToNxCloud(
const isGitHubDetected =
schema.github ?? (await repoUsesGithub(schema.github));

let responseFromCreateNxCloudWorkspaceV1:
| {
token: string;
}
| undefined;

let responseFromCreateNxCloudWorkspaceV2:
| {
nxCloudId: string;
Expand All @@ -157,22 +208,47 @@ export async function connectToNxCloud(
)
return null;

responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudIdToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
schema.directory
);
try {
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);
} catch (e) {
if (e.response?.status === 404) {
responseFromCreateNxCloudWorkspaceV1 = await createNxCloudWorkspaceV1(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);
} else {
throw e;
}
}

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
if (responseFromCreateNxCloudWorkspaceV2) {
addNxCloudIdToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
schema.directory
);
await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
} else if (responseFromCreateNxCloudWorkspaceV1) {
addNxCloudAccessTokenToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV1?.token,
schema.directory
);
await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV1.token;
} else {
return null;
}
}

async function connectToNxCloudGenerator(
Expand Down