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: take nx-cloud.env into account for cloud view #1475

Merged
merged 2 commits into from
Jan 16, 2023
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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNxWorkspace } from '@nx-console/vscode/nx-workspace';
import { getNxCloudRunnerOptions } from '@nx-console/vscode/nx-workspace';
import request, { gql } from 'graphql-request';
import { authentication } from 'vscode';
import {
Expand All @@ -17,11 +17,9 @@ export class NxCloudApiService {
async claimCloudWorkspace(
orgId: string
): Promise<ConnectWorkspaceUsingTokenResponse> {
const workspaceConfig = (await getNxWorkspace()).workspace;
const nxAccessToken =
workspaceConfig.tasksRunnerOptions?.['default']?.options['accessToken'];
const nxAccessToken = (await getNxCloudRunnerOptions())?.accessToken;

const authAccessToken = await this.getAccessToken();
const authAccessToken = await this.getAuthAccessToken();

if (!authAccessToken) {
throw new Error('No auth access token found');
Expand Down Expand Up @@ -58,11 +56,9 @@ export class NxCloudApiService {
}

async getWorkspaceDetailsByToken(): Promise<GetWorkspaceDetailsByTokenResponse> {
const workspaceConfig = (await getNxWorkspace()).workspace;
const nxAcessToken =
workspaceConfig.tasksRunnerOptions?.['default']?.options['accessToken'];
const nxAccessToken = (await getNxCloudRunnerOptions())?.accessToken;

if (!nxAcessToken) {
if (!nxAccessToken) {
throw new Error('No nx access token found.');
}

Expand All @@ -84,7 +80,7 @@ export class NxCloudApiService {
`;

const variables = {
accessToken: nxAcessToken,
accessToken: nxAccessToken,
};

const session = await authentication.getSession('nxCloud', [], {
Expand Down Expand Up @@ -148,7 +144,7 @@ export class NxCloudApiService {
workspaceId: workspaceId,
};

const authAccessToken = await this.getAccessToken();
const authAccessToken = await this.getAuthAccessToken();
const headers = authAccessToken
? {
Authorization: `Bearer ${authAccessToken}`,
Expand Down Expand Up @@ -234,7 +230,7 @@ export class NxCloudApiService {
return 'disconnected';
}

private async getAccessToken() {
private async getAuthAccessToken() {
return await authentication
.getSession('nxCloud', [], { silent: true })
.then((session) => session?.accessToken);
Expand Down
38 changes: 32 additions & 6 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { ProjectConfiguration } from '@nrwl/devkit';
import { NxWorkspaceRequest } from '@nx-console/language-server/types';
import { NxWorkspace } from '@nx-console/shared/types';
import { sendRequest } from '@nx-console/vscode/lsp-client';
import { parse } from 'dotenv';
import { join } from 'path';
import { readFileSync } from 'fs';

export function getNxWorkspace(reset?: boolean): Promise<NxWorkspace> {
return sendRequest(NxWorkspaceRequest, { reset });
Expand All @@ -20,13 +23,36 @@ export async function getNxWorkspaceProjects(reset?: boolean): Promise<{
export async function getNxCloudRunnerOptions(): Promise<
{ accessToken: string; url?: string } | undefined
> {
const nxConfig = (await getNxWorkspace()).workspace;
const nxWorkspace = await getNxWorkspace();
const workspaceConfig = nxWorkspace.workspace;

if (!nxConfig.tasksRunnerOptions) {
if (!workspaceConfig.tasksRunnerOptions) {
return;
}
const nxCloudTaskRunner = Object.values(nxConfig.tasksRunnerOptions).find(
(r) => r.runner == '@nrwl/nx-cloud'
);
return nxCloudTaskRunner?.options;
const nxCloudTaskRunner = Object.values(
workspaceConfig.tasksRunnerOptions
).find((r) => r.runner == '@nrwl/nx-cloud');

if (!nxCloudTaskRunner) {
return undefined;
}

// check if nx-cloud.env exists and use that access token if it does
const env = getNxCloudEnv(nxWorkspace.workspacePath);
const accessToken = env.NX_CLOUD_AUTH_TOKEN || env.NX_CLOUD_ACCESS_TOKEN;

if (!accessToken) {
return nxCloudTaskRunner.options;
}

return { ...nxCloudTaskRunner.options, accessToken };
}

function getNxCloudEnv(workspacePath: string): any {
try {
const envContents = readFileSync(join(workspacePath, 'nx-cloud.env'));
return parse(envContents);
} catch (e) {
return {};
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@vscode/webview-ui-toolkit": "^1.2.0",
"@yarnpkg/fslib": "2.6.1-rc.5",
"@yarnpkg/libzip": "2.2.3-rc.5",
"dotenv": "^16.0.3",
"fast-glob": "^3.2.11",
"find-cache-dir": "^3.3.2",
"graphql": "^16.6.0",
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12119,6 +12119,13 @@ __metadata:
languageName: node
linkType: hard

"dotenv@npm:^16.0.3":
version: 16.0.3
resolution: "dotenv@npm:16.0.3"
checksum: afcf03f373d7a6d62c7e9afea6328e62851d627a4e73f2e12d0a8deae1cd375892004f3021883f8aec85932cd2834b091f568ced92b4774625b321db83b827f8
languageName: node
linkType: hard

"dotenv@npm:^8.0.0":
version: 8.6.0
resolution: "dotenv@npm:8.6.0"
Expand Down Expand Up @@ -20682,6 +20689,7 @@ __metadata:
"@yarnpkg/libzip": 2.2.3-rc.5
"@yarnpkg/pnp": ^3.1.1-rc.12
cypress: ^10.2.0
dotenv: ^16.0.3
esbuild: ^0.14.54
esbuild-copy-files-plugin: ^1.1.0
eslint: 8.15.0
Expand Down