Commit 40ce8431 authored by Olena Horal-Koretska's avatar Olena Horal-Koretska 2️⃣ Committed by Tomas Vik (OOO back on 2026-08-31)
Browse files

feat: Send telemetry event when suggestion stream is accepted

parent e1698503
Loading
Loading
Loading
Loading
+7 −7
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
      "license": "MIT",
      "dependencies": {
        "@anycable/core": "^0.8.0",
        "@gitlab-org/gitlab-lsp": "^4.2.2",
        "@gitlab-org/gitlab-lsp": "^4.3.0",
        "@snowplow/tracker-core": "3.22.1",
        "cross-fetch": "^4.0.0",
        "dayjs": "^1.11.10",
@@ -1104,9 +1104,9 @@
      }
    },
    "node_modules/@gitlab-org/gitlab-lsp": {
      "version": "4.2.2",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.2.2.tgz",
      "integrity": "sha1-6gO/d6GAj7iIDDsIJODUU5PstYI=",
      "version": "4.3.0",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.3.0.tgz",
      "integrity": "sha1-kq4OT0kXvOhZdPObZMEdU1FNuFA=",
      "dependencies": {
        "@snowplow/tracker-core": "^3.15.0",
        "ajv": "^8.12.0",
@@ -14605,9 +14605,9 @@
      "dev": true
    },
    "@gitlab-org/gitlab-lsp": {
      "version": "4.2.2",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.2.2.tgz",
      "integrity": "sha1-6gO/d6GAj7iIDDsIJODUU5PstYI=",
      "version": "4.3.0",
      "resolved": "https://gitlab.com/api/v4/projects/46519181/packages/npm/@gitlab-org/gitlab-lsp/-/@gitlab-org/gitlab-lsp-4.3.0.tgz",
      "integrity": "sha1-kq4OT0kXvOhZdPObZMEdU1FNuFA=",
      "requires": {
        "@snowplow/tracker-core": "^3.15.0",
        "ajv": "^8.12.0",
+1 −1
Original line number Diff line number Diff line
@@ -299,7 +299,7 @@
  },
  "dependencies": {
    "@anycable/core": "^0.8.0",
    "@gitlab-org/gitlab-lsp": "^4.2.2",
    "@gitlab-org/gitlab-lsp": "^4.3.0",
    "@snowplow/tracker-core": "3.22.1",
    "cross-fetch": "^4.0.0",
    "dayjs": "^1.11.10",
+39 −0
Original line number Diff line number Diff line
import {
  TELEMETRY_NOTIFICATION,
  TRACKING_EVENTS,
  CODE_SUGGESTIONS_CATEGORY,
} from '@gitlab-org/gitlab-lsp';
import { BaseLanguageClient } from 'vscode-languageclient';
import { codeSuggestionStreamAccepted } from './code_suggestion_stream_accepted';
import { CompletionStream } from '../../language_server/completion_stream';
import { createFakePartial } from '../../test_utils/create_fake_partial';

describe('codeSuggestionStreamAccepted command', () => {
  const stream = createFakePartial<CompletionStream>({
    trackingId: 'uniqueTrackingId',
    cancel: jest.fn(),
  });

  const languageClient = createFakePartial<BaseLanguageClient>({
    sendNotification: jest.fn(),
  });
  const runCommand = codeSuggestionStreamAccepted(languageClient);

  beforeEach(async () => {
    await runCommand(stream);
  });

  it('should cancel the stream', () => {
    expect(stream.cancel).toHaveBeenCalled();
  });

  it('should send telemetry notification', () => {
    expect(languageClient.sendNotification).toHaveBeenCalledWith(TELEMETRY_NOTIFICATION, {
      action: TRACKING_EVENTS.ACCEPTED,
      category: CODE_SUGGESTIONS_CATEGORY,
      context: {
        trackingId: stream.trackingId,
      },
    });
  });
});
+15 −2
Original line number Diff line number Diff line
import { BaseLanguageClient } from 'vscode-languageclient';
import {
  TELEMETRY_NOTIFICATION,
  TRACKING_EVENTS,
  CODE_SUGGESTIONS_CATEGORY,
} from '@gitlab-org/gitlab-lsp';
import { log } from '../../log';
import { CompletionStream } from '../../language_server/completion_stream';

export const COMMAND_CODE_SUGGESTION_STREAM_ACCEPTED = 'gl.codeSuggestionStreamAccepted';
export const CODE_SUGGESTION_STREAM_ACCEPTED_COMMAND = 'gl.codeSuggestionStreamAccepted';
// Used for telemetry
export const codeSuggestionStreamAccepted = async (stream: CompletionStream) => {
export const codeSuggestionStreamAccepted =
  (client: BaseLanguageClient) => async (stream: CompletionStream) => {
    log.debug(`stream has been accepted ${stream}`);
    stream.cancel();

    await client?.sendNotification(TELEMETRY_NOTIFICATION, {
      category: CODE_SUGGESTIONS_CATEGORY,
      action: TRACKING_EVENTS.ACCEPTED,
      context: { trackingId: stream.trackingId },
    });
  };
+7 −8
Original line number Diff line number Diff line
import { BaseLanguageClient } from 'vscode-languageclient';
import * as vscode from 'vscode';
import { createFakePartial } from '../test_utils/create_fake_partial';
import { CompletionStream } from './completion_stream';
import { createStreamIterator } from './create_stream_iterator';
@@ -9,16 +8,16 @@ jest.mock('./create_stream_iterator', () => ({
}));

const mockCleanupFn = jest.fn();
const uniqueTrackingId = '1';

describe('CompletionStream', () => {
  it('binds the CompletionStream instance to the cleanup function ', () => {
    const stream = new CompletionStream(
      createFakePartial<BaseLanguageClient>({}),
      createFakePartial<vscode.TextDocument>({}),
      createFakePartial<vscode.Position>({}),
      'stream-1',
      mockCleanupFn,
    );
    const stream = new CompletionStream({
      client: createFakePartial<BaseLanguageClient>({}),
      streamId: 'stream-1',
      uniqueTrackingId,
      onCancelDetached: mockCleanupFn,
    });

    const cleanupFn = (createStreamIterator as jest.Mock).mock.calls[0][3];
    cleanupFn();
Loading