Commit d1ae632d authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak 1️⃣
Browse files

feat: Remove project requirement from Code Suggestions

This commit removes active project being present from
Code Suggestions feature. It also replace a check if
project is hosted on GitLab.com to enable new API with
version based check
parent dda8793b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ Based on these environments, the extension source code lives in three different
Because the WebIDE and Desktop environments are very different, we built abstraction over the way how get GitLab project data and how we make API calls:

```ts
export interface GitLabPlatform {
export interface GitLabPlatformForActiveProject {
  project: GitLabProject;

  fetchFromApi: fetchFromApi;
@@ -41,7 +41,7 @@ export interface GitLabPlatform {

_(this abstraction will grow as we port more features)_

All code in `common` folder has to call either [VS Code API](https://code.visualstudio.com/api/references/vscode-api) that is available in Browser or the `GitLabPlatform`. The `common` code **must not** invoke any `browser` or `desktop` code or any Browser or Node.js APIs.
All code in `common` folder has to call either [VS Code API](https://code.visualstudio.com/api/references/vscode-api) that is available in Browser or the `GitLabPlatformForActiveProject`. The `common` code **must not** invoke any `browser` or `desktop` code or any Browser or Node.js APIs.

## `desktop`

+2 −2
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

As the [Architecture](./architecture.md) document explains, this extension can run in the browser in WebIDE.

This document explains how we package the extension WebIDE and how the browser `GitLabPlatform` implements calling GitLab API and getting the project context.
This document explains how we package the extension WebIDE and how the browser `GitLabPlatformForActiveProject` implements calling GitLab API and getting the project context.

## Diagrams

@@ -54,4 +54,4 @@ The Workflow extension needs access to two objects out of the VS Code runtime:

The extension gets access to these two objects through mediator commands.

The `gitlab_platform_browser.ts` then uses these mediator commands to implement the `GitLabPlatform` interface.
The `gitlab_platform_browser.ts` then uses these mediator commands to implement the `GitLabPlatformForActiveProject` interface.
+41 −3
Original line number Diff line number Diff line
@@ -6,7 +6,11 @@ import {
} from '../common/platform/web_ide';
import { gqlProject, project } from '../common/test_utils/entities';
import { createGitLabPlatformManagerBrowser } from './gitlab_platform_browser';
import { GitLabPlatform, GitLabPlatformManager } from '../common/platform/gitlab_platform';
import {
  GitLabPlatformForActiveProject,
  GitLabPlatformForActiveAccount,
  GitLabPlatformManager,
} from '../common/platform/gitlab_platform';

const FAKE_MEDIATOR_TOKEN = 'fake-mediator-token';

@@ -31,20 +35,53 @@ describe('createGitLabPlatformManagerBrowser', () => {
  };

  describe('functionality', () => {
    let platform: GitLabPlatform | undefined;
    let manager: GitLabPlatformManager;

    beforeEach(async () => {
      mockCommandsForInitialSetup();

      manager = await createGitLabPlatformManagerBrowser();
      platform = await manager.getForActiveProject(false);
    });

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

    describe('without GitLab hosted project', () => {
      let platform: GitLabPlatformForActiveAccount | undefined;

      beforeEach(async () => {
        platform = await manager.getForActiveAccount();
      });

      it('forwards all calls to fetchFromApi to the mediator command', async () => {
        expect(platform).toBeDefined();

        jest.resetAllMocks();

        const testRequest = { type: 'rest', method: 'GET', path: '/test' } as const;
        const testResponse = { value: 'test' };

        jest.mocked(vscode.commands.executeCommand).mockResolvedValue(testResponse);

        const result = await platform?.fetchFromApi(testRequest);

        expect(result).toEqual(testResponse);
        expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
          COMMAND_FETCH_FROM_API,
          FAKE_MEDIATOR_TOKEN,
          testRequest,
        );
      });
    });

    describe('with GitLab hosted project', () => {
      let platform: GitLabPlatformForActiveProject | undefined;

      beforeEach(async () => {
        platform = await manager.getForActiveProject(false);
      });

      it('calls mediator commands to get config and project from WebIDE', async () => {
        expect(platform).toBeDefined();
        expect(platform?.project).toEqual(project);
@@ -75,3 +112,4 @@ describe('createGitLabPlatformManagerBrowser', () => {
      });
    });
  });
});
+10 −4
Original line number Diff line number Diff line
@@ -40,14 +40,20 @@ export const createGitLabPlatformManagerBrowser: () => Promise<GitLabPlatformMan
    }

    const gitLabProject = convertToGitLabProject(project);
    return {
      getForActiveProject: () =>
        Promise.resolve({
          project: gitLabProject,
    const platformBase = {
      fetchFromApi,
      // browser won't let us change User-Agent header
      // so we don't have to construct it
      getUserAgentHeader: () => ({}),
    };

    return {
      getForActiveProject: () =>
        Promise.resolve({
          type: 'project',
          project: gitLabProject,
          ...platformBase,
        }),
      getForActiveAccount: async () => ({ type: 'account', ...platformBase }),
    };
  };
+6 −0
Original line number Diff line number Diff line
@@ -44,10 +44,16 @@ describe('GitLabChatApi', () => {

    return {
      getForActiveProject: jest.fn(async () => ({
        type: 'project' as const,
        project,
        fetchFromApi: makeApiRequest,
        getUserAgentHeader: () => ({}),
      })),
      getForActiveAccount: jest.fn(async () => ({
        type: 'account' as const,
        fetchFromApi: makeApiRequest,
        getUserAgentHeader: () => ({}),
      })),
    };
  };

Loading