fix(nx-cloud): use create-workspace-v1 endpoint if v2 returns with 404 (#28015)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> By default we connect all workspaces to Nx Cloud via the V2 connect endpoint. ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Certain users (on-prem or single tenant) may not have access to the V2 endpoint, and so we should fall back to V1 so as not to prevent new workspaces from being created. ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
This commit is contained in:
parent
f743808bd5
commit
acb19a6439
@ -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(
|
async function createNxCloudWorkspaceV2(
|
||||||
workspaceName: string,
|
workspaceName: string,
|
||||||
installationSource: string,
|
installationSource: string,
|
||||||
@ -103,6 +125,29 @@ export interface ConnectToNxCloudOptions {
|
|||||||
generateToken?: boolean;
|
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(
|
function addNxCloudIdToNxJson(
|
||||||
tree: Tree,
|
tree: Tree,
|
||||||
nxCloudId: string,
|
nxCloudId: string,
|
||||||
@ -140,6 +185,12 @@ export async function connectToNxCloud(
|
|||||||
const isGitHubDetected =
|
const isGitHubDetected =
|
||||||
schema.github ?? (await repoUsesGithub(schema.github));
|
schema.github ?? (await repoUsesGithub(schema.github));
|
||||||
|
|
||||||
|
let responseFromCreateNxCloudWorkspaceV1:
|
||||||
|
| {
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
| undefined;
|
||||||
|
|
||||||
let responseFromCreateNxCloudWorkspaceV2:
|
let responseFromCreateNxCloudWorkspaceV2:
|
||||||
| {
|
| {
|
||||||
nxCloudId: string;
|
nxCloudId: string;
|
||||||
@ -157,22 +208,49 @@ export async function connectToNxCloud(
|
|||||||
)
|
)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
|
try {
|
||||||
getRootPackageName(tree),
|
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
|
||||||
schema.installationSource,
|
getRootPackageName(tree),
|
||||||
getNxInitDate()
|
schema.installationSource,
|
||||||
);
|
getNxInitDate()
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
if (e.response?.status === 404) {
|
||||||
|
responseFromCreateNxCloudWorkspaceV1 = await createNxCloudWorkspaceV1(
|
||||||
|
getRootPackageName(tree),
|
||||||
|
schema.installationSource,
|
||||||
|
getNxInitDate()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
addNxCloudIdToNxJson(
|
if (responseFromCreateNxCloudWorkspaceV2) {
|
||||||
tree,
|
addNxCloudIdToNxJson(
|
||||||
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
|
tree,
|
||||||
schema.directory
|
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
|
||||||
);
|
schema.directory
|
||||||
|
);
|
||||||
await formatChangedFilesWithPrettierIfAvailable(tree, {
|
await formatChangedFilesWithPrettierIfAvailable(tree, {
|
||||||
silent: schema.hideFormatLogs,
|
silent: schema.hideFormatLogs,
|
||||||
});
|
});
|
||||||
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
|
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
|
||||||
|
} else if (responseFromCreateNxCloudWorkspaceV1) {
|
||||||
|
addNxCloudAccessTokenToNxJson(
|
||||||
|
tree,
|
||||||
|
responseFromCreateNxCloudWorkspaceV1?.token,
|
||||||
|
schema.directory
|
||||||
|
);
|
||||||
|
await formatChangedFilesWithPrettierIfAvailable(tree, {
|
||||||
|
silent: schema.hideFormatLogs,
|
||||||
|
});
|
||||||
|
return responseFromCreateNxCloudWorkspaceV1.token;
|
||||||
|
} else {
|
||||||
|
throw new Error(
|
||||||
|
'Could not create an Nx Cloud Workspace. Please try again.'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function connectToNxCloudGenerator(
|
async function connectToNxCloudGenerator(
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user