Skip to content

Commit

Permalink
feat(nx-cloud): remove feature flag for nx login (#27657)
Browse files Browse the repository at this point in the history
<!-- 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 -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

Enables `nx connect` to use the V2 endpoint from Nx API such that it
creates an `nxCloudId`

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
lourw authored Aug 28, 2024
1 parent e28de09 commit 0ef6892
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('connect-to-nx-cloud', () => {
expect(
withEnvironmentVariables(
{
NX_ENABLE_LOGIN: 'true',
NX_CLOUD_ACCESS_TOKEN: null,
},
() =>
onlyDefaultRunnerIsUsed({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,6 @@ 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 @@ -124,29 +102,6 @@ export interface ConnectToNxCloudOptions {
directory?: string;
}

function addNxCloudOptionsToNxJson(
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 @@ -183,12 +138,6 @@ export async function connectToNxCloud(
} else {
const usesGithub = schema.github ?? (await repoUsesGithub(schema.github));

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

let responseFromCreateNxCloudWorkspaceV2:
| {
nxCloudId: string;
Expand All @@ -198,41 +147,22 @@ export async function connectToNxCloud(
// do NOT create Nx Cloud token (createNxCloudWorkspace)
// if user is using github and is running nx-connect
if (!(usesGithub && schema.installationSource === 'nx-connect')) {
if (process.env.NX_ENABLE_LOGIN === 'true') {
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudIdToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
schema.directory
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
} else {
responseFromCreateNxCloudWorkspaceV1 = await createNxCloudWorkspaceV1(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudOptionsToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV1?.token,
schema.directory
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV1.token;
}
responseFromCreateNxCloudWorkspaceV2 = await createNxCloudWorkspaceV2(
getRootPackageName(tree),
schema.installationSource,
getNxInitDate()
);

addNxCloudIdToNxJson(
tree,
responseFromCreateNxCloudWorkspaceV2?.nxCloudId,
schema.directory
);

await formatChangedFilesWithPrettierIfAvailable(tree, {
silent: schema.hideFormatLogs,
});
return responseFromCreateNxCloudWorkspaceV2.nxCloudId;
}
}
}
Expand Down

0 comments on commit 0ef6892

Please sign in to comment.