Unverified Commit 0e4e04bf authored by Denys Mishunov's avatar Denys Mishunov 🔴
Browse files

feat(chat): enable Duo Chat for SM instances

The commit losens the restriction on the account
type by simply checking whether we have any
ccount at all.
parent 0657afd3
Loading
Loading
Loading
Loading
+14 −14
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@
          "type": "webview",
          "id": "gl.chatView",
          "name": "Experiment",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount"
        }
      ]
    },
@@ -103,23 +103,23 @@
      "commandPalette": [
        {
          "command": "gl.openChat",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount"
        },
        {
          "command": "gl.explainSelectedCode",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection"
        },
        {
          "command": "gl.generateTests",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection"
        },
        {
          "command": "gl.refactorCode",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection"
        },
        {
          "command": "gl.newChatConversation",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount"
        }
      ],
      "editor/context": [
@@ -132,44 +132,44 @@
        {
          "command": "gl.explainSelectedCode",
          "group": "navigation",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection"
        },
        {
          "command": "gl.generateTests",
          "group": "navigation",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection"
        },
        {
          "command": "gl.refactorCode",
          "group": "navigation",
          "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection"
          "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection"
        }
      ]
    },
    "keybindings": [
      {
        "command": "gl.openChat",
        "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount",
        "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount",
        "key": "alt+d"
      },
      {
        "command": "gl.explainSelectedCode",
        "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection",
        "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection",
        "key": "alt+e"
      },
      {
        "command": "gl.generateTests",
        "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection",
        "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection",
        "key": "alt+t"
      },
      {
        "command": "gl.refactorCode",
        "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount && editorHasSelection",
        "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount && editorHasSelection",
        "key": "alt+r"
      },
      {
        "command": "gl.newChatConversation",
        "when": "config.gitlab.duoChat.enabled && gitlab:hasSaaSAccount",
        "when": "config.gitlab.duoChat.enabled && !gitlab:noAccount",
        "key": "alt+n"
      }
    ],
+79 −0
Original line number Diff line number Diff line
import { GitLabPlatformForAccount, GitLabPlatformManager } from '../platform/gitlab_platform';
import { GitLabPlatformManagerForChat } from './get_platform_manager_for_chat';
import { account, gitlabPlatformForAccount } from '../test_utils/entities';
import { Account } from '../platform/gitlab_account';
import { createFakePartial } from '../test_utils/create_fake_partial';

jest.mock('../utils/extension_configuration');

describe('GitLabPlatformManagerForChat', () => {
  let platformManagerForChat: GitLabPlatformManagerForChat;
  let gitlabPlatformManager: GitLabPlatformManager;

  const buildGitLabPlatformForAccount = (useAccount: Account): GitLabPlatformForAccount => ({
    ...gitlabPlatformForAccount,
    account: useAccount,
  });

  const firstGitlabPlatformForAccount: GitLabPlatformForAccount = buildGitLabPlatformForAccount({
    ...account,
    username: 'first-account',
  });
  const secondGitLabPlatformForAccount: GitLabPlatformForAccount = buildGitLabPlatformForAccount({
    ...account,
    username: 'second-account',
  });

  beforeEach(() => {
    gitlabPlatformManager = createFakePartial<GitLabPlatformManager>({
      getForActiveProject: jest.fn(),
      getForActiveAccount: jest.fn(),
      getForAllAccounts: jest.fn(),
      getForSaaSAccount: jest.fn(),
    });

    platformManagerForChat = new GitLabPlatformManagerForChat(gitlabPlatformManager);
  });

  afterEach(() => {
    jest.clearAllMocks();
  });

  describe('when no gitlab account is available', () => {
    beforeEach(() => {
      jest.mocked(gitlabPlatformManager.getForAllAccounts).mockResolvedValueOnce([]);
    });

    it('returns undefined', async () => {
      expect(await platformManagerForChat.getGitLabPlatform()).toBe(undefined);
    });
  });

  describe('when a single gitlab account is available', () => {
    let customGitlabPlatformForAccount: GitLabPlatformForAccount;

    beforeEach(() => {
      customGitlabPlatformForAccount = firstGitlabPlatformForAccount;

      jest
        .mocked(gitlabPlatformManager.getForAllAccounts)
        .mockResolvedValueOnce([customGitlabPlatformForAccount]);
    });

    it('returns gitlab platform for that account', async () => {
      expect(await platformManagerForChat.getGitLabPlatform()).toBe(customGitlabPlatformForAccount);
    });
  });

  describe('when multiple gitlab accounts are available', () => {
    beforeEach(() => {
      jest
        .mocked(gitlabPlatformManager.getForAllAccounts)
        .mockResolvedValueOnce([firstGitlabPlatformForAccount, secondGitLabPlatformForAccount]);
    });

    it('returns gitlab platform for the first linked account', async () => {
      expect(await platformManagerForChat.getGitLabPlatform()).toBe(firstGitlabPlatformForAccount);
    });
  });
});
+26 −0
Original line number Diff line number Diff line
import { GitLabPlatformManager, GitLabPlatformForAccount } from '../platform/gitlab_platform';

export class GitLabPlatformManagerForChat {
  readonly #platformManager: GitLabPlatformManager;

  constructor(platformManager: GitLabPlatformManager) {
    this.#platformManager = platformManager;
  }

  /**
   * Obtains a GitLab Platform to send API requests to the GitLab API
   * for the Duo Chat feature.
   *
   * - It returns a GitLabPlatformForAccount for the first linked account.
   * - It returns undefined if there are no accounts linked
   */
  async getGitLabPlatform(): Promise<GitLabPlatformForAccount | undefined> {
    const platforms = await this.#platformManager.getForAllAccounts();

    if (platforms.length === 0) {
      return undefined;
    }

    return platforms[0];
  }
}
+3 −1
Original line number Diff line number Diff line
@@ -11,11 +11,13 @@ import {
  newChatConversation,
} from './commands/new_chat_conversation';
import { GitLabPlatformManager } from '../platform/gitlab_platform';
import { GitLabPlatformManagerForChat } from './get_platform_manager_for_chat';
import { CHAT_SIDEBAR_VIEW_ID } from './gitlab_chat_view';
import { COMMAND_REFACTOR_CODE, refactorCode } from './commands/refactorCode';

export const activateChat = (context: vscode.ExtensionContext, manager: GitLabPlatformManager) => {
  const controller = new GitLabChatController(manager, context);
  const platformManagerForChat = new GitLabPlatformManagerForChat(manager);
  const controller = new GitLabChatController(platformManagerForChat, context);

  // sidebar view
  context.subscriptions.push(
+4 −7
Original line number Diff line number Diff line
import assert from 'assert';
import { GitLabPlatformManager } from '../platform/gitlab_platform';
import { GitLabPlatformManagerForChat } from './get_platform_manager_for_chat';
import { gitlabPlatformForAccount } from '../test_utils/entities';
import { GitLabChatApi, AI_ACTIONS, AI_MESSAGES_QUERY } from './gitlab_chat_api';
import { API_PULLING } from './api/pulling';
@@ -40,7 +40,7 @@ describe('GitLabChatApi', () => {
  const createManager = (
    queryContent = mockedQueryResponse,
    mutationContent = mockedMutationResponse,
  ): GitLabPlatformManager => {
  ): GitLabPlatformManagerForChat => {
    makeApiRequest = jest.fn(async <T>(params: any): Promise<T> => {
      let response;
      if (params?.query === AI_ACTIONS.chat) {
@@ -51,14 +51,11 @@ describe('GitLabChatApi', () => {
      return response;
    });

    return createFakePartial<GitLabPlatformManager>({
      getForActiveProject: jest.fn(),
      getForActiveAccount: jest.fn(),
      getForSaaSAccount: jest.fn(async () => ({
    return createFakePartial<GitLabPlatformManagerForChat>({
      getGitLabPlatform: jest.fn(async () => ({
        ...gitlabPlatformForAccount,
        fetchFromApi: makeApiRequest,
      })),
      getForAllAccounts: jest.fn(),
    });
  };

Loading