Commit 7a6e048f authored by Paul Slaughter's avatar Paul Slaughter 2️⃣
Browse files

feat: Add "codeSuggestionsClientDirectToGateway" feature flag

- This is sent over to the LSP which will consume it
  for connecting directly to the AI Gateway
- gitlab#433433
parent 73669812
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ export enum FeatureFlag {
  SecurityScans = 'securityScansFlag',
  ForceCodeSuggestionsViaMonolith = 'forceCodeSuggestionsViaMonolith',
  LanguageServer = 'languageServer',
  CodeSuggestionsClientDirectToGateway = 'codeSuggestionsClientDirectToGateway',
}

// Set the feature flag default value here
@@ -13,4 +14,5 @@ export const FEATURE_FLAGS_DEFAULT_VALUES = {
  [FeatureFlag.ForceCodeSuggestionsViaMonolith]: false,
  [FeatureFlag.TestFlag]: false,
  [FeatureFlag.LanguageServer]: true,
  [FeatureFlag.CodeSuggestionsClientDirectToGateway]: false,
};
+40 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import { GitLabPlatformManagerForCodeSuggestions } from '../code_suggestions/git
import { GitLabPlatformForAccount, GitLabPlatformManager } from '../platform/gitlab_platform';
import { gitlabPlatformForAccount } from '../test_utils/entities';
import { CodeSuggestionsStateManager } from '../code_suggestions/code_suggestions_state_manager';
import { setFakeWorkspaceConfiguration } from '../test_utils/vscode_fakes';

jest.mock('../code_suggestions/gitlab_platform_manager_for_code_suggestions');
jest.mock('../log'); // disable logging in tests
@@ -64,6 +65,9 @@ describe('LanguageClientWrapper', () => {
            telemetry: {
              actions: [{ action: TRACKING_EVENTS.ACCEPTED }],
            },
            featureFlags: {
              codeSuggestionsClientDirectToGateway: false,
            },
          },
        },
      );
@@ -124,4 +128,40 @@ describe('LanguageClientWrapper', () => {
      expect(stateManager.setError).toHaveBeenLastCalledWith(false);
    });
  });

  describe('syncConfig', () => {
    let subject: LanguageClientWrapper;

    beforeEach(async () => {
      subject = new LanguageClientWrapper(client, fakeManager, stateManager);
      await subject.initAndStart();

      // initAndStart() will trigger some mocks, so let's start from a clean slate
      jest.clearAllMocks();
    });

    it('reads featureFlags configuration', async () => {
      setFakeWorkspaceConfiguration({
        featureFlags: {
          codeSuggestionsClientDirectToGateway: true,
          // Include somethingElse to show that it is not included
          somethingElse: false,
        },
      });

      await subject.syncConfig();

      expect(client.sendNotification).toHaveBeenCalledTimes(1);
      expect(client.sendNotification).toHaveBeenCalledWith(
        DidChangeConfigurationNotification.type,
        {
          settings: expect.objectContaining({
            featureFlags: {
              codeSuggestionsClientDirectToGateway: true,
            },
          }),
        },
      );
    });
  });
});
+10 −1
Original line number Diff line number Diff line
@@ -12,6 +12,10 @@ import { GitLabPlatformManager } from '../platform/gitlab_platform';
import { GitLabPlatformManagerForCodeSuggestions } from '../code_suggestions/gitlab_platform_manager_for_code_suggestions';
import { CodeSuggestionsStateManager } from '../code_suggestions/code_suggestions_state_manager';
import { log } from '../log';
import { FeatureFlag, isEnabled } from '../feature_flags';

// note: Temporary until new gitlab-lsp version bumps with IConfig updated
type LspConfig = IConfig | { featureFlags: Record<string, boolean> };

export class LanguageClientWrapper {
  #client: BaseLanguageClient;
@@ -64,12 +68,17 @@ export class LanguageClientWrapper {
      log.warn('There is no GitLab account available with access to suggestions');
      return;
    }
    const settings: IConfig = {
    const settings: LspConfig = {
      baseUrl: platform.account.instanceUrl,
      token: platform.account.token,
      telemetry: {
        actions: [{ action: TRACKING_EVENTS.ACCEPTED }],
      },
      featureFlags: {
        [FeatureFlag.CodeSuggestionsClientDirectToGateway]: isEnabled(
          FeatureFlag.CodeSuggestionsClientDirectToGateway,
        ),
      },
    };

    log.info(`Configuring Language Server - baseUrl: ${platform.account.instanceUrl}`);
+13 −0
Original line number Diff line number Diff line
import * as vscode from 'vscode';
import { CONFIG_NAMESPACE } from '../constants';

export const createFakeWorkspaceConfiguration = (
  config: Record<string, unknown>,
): vscode.WorkspaceConfiguration => config as unknown as vscode.WorkspaceConfiguration;

export const setFakeWorkspaceConfiguration = (config: Record<string, unknown>) => {
  const configuration = createFakeWorkspaceConfiguration(config);

  jest.mocked(vscode.workspace.getConfiguration).mockImplementation(section => {
    if (section === CONFIG_NAMESPACE) {
      return configuration;
    }

    return createFakeWorkspaceConfiguration({});
  });
};

export const createConfigurationChangeTrigger = () => {
  let triggerSettingsRefresh: (() => void) | undefined;