Skip to content

feat(vscode): select org explicitly - #83

Open
John-David Dalton (jdalton) wants to merge 1 commit into
mainfrom
feat/package-intelligence-vscode-org-selection-delivery
Open

John-David Dalton (jdalton) wants to merge 1 commit into
mainfrom
feat/package-intelligence-vscode-org-selection-delivery

Conversation

@jdalton

@jdalton John-David Dalton (jdalton) commented Oct 10, 2026 •

Copy link
Copy Markdown
Collaborator

Users with a Socket API token can explicitly select an organization using socket-security.orgSlug. The extension validates that the token can access the selected organization and clears cached package results when the token or selection changes. Users can still work with a token alone.

Package lookups continue to use the existing API. A follow-up will use the selected organization once the organization-scoped API and SDK support are available.

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Org selection unused by package fetches
    • Package fetches now send the organization resolved by getAuthenticatedContext to the org-scoped purl endpoint when socket-security.orgSlug selects an accessible org.

Create PR

Or push these changes by commenting:

@cursor push 260a33cf1b
Preview (260a33cf1b)
diff --git a/src/api.mts b/src/api.mts
--- a/src/api.mts
+++ b/src/api.mts
@@ -23,6 +23,7 @@
 }
 
 export type PackageRequestOptions = {
+  orgSlug?: string | undefined
   signal?: AbortSignal | undefined
   timeout?: number | undefined
 }
@@ -236,17 +237,25 @@
   options?: PackageRequestOptions | undefined,
 ): AsyncGenerator {
   const sdk = createSocketSdk(apiKey, options)
+  const { orgSlug } = {
+    __proto__: null,
+    ...options,
+  } as PackageRequestOptions
   const batchSize = 1024
+  const query = { alerts: true, compact: false }
   for (let index = 0, { length } = purls; index < length; index += batchSize) {
     const components = purls
       .slice(index, index + batchSize)
       .map(purl => ({ __proto__: null, purl }))
-    const result = await sdk.batchPackageFetch(
-      { components },
-      { alerts: true, compact: false },
-    )
+    const result = orgSlug
+      ? await sdk.batchOrgPackageFetch(orgSlug, { components }, query)
+      : await sdk.batchPackageFetch({ components }, query)
     if (!result.success) {
-      throw createPackageRequestError(result)
+      throw createPackageRequestError({
+        error: '',
+        status: result.status,
+        success: false,
+      })
     }
     yield* result.data
   }

diff --git a/src/ui/purl-alerts-and-scores/manager.mts b/src/ui/purl-alerts-and-scores/manager.mts
--- a/src/ui/purl-alerts-and-scores/manager.mts
+++ b/src/ui/purl-alerts-and-scores/manager.mts
@@ -3,12 +3,15 @@
 import { logger } from '../../infra/log.mts'
 import path from 'node:path'
 import { mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'
-import { getAPIKey } from '../../auth.mts'
+import { getAPIKey, getAuthenticatedContext } from '../../auth.mts'
 import {
   isPackageDisplayDataRenderable,
   streamPackageScores,
 } from '../../api.mts'
-import type { PackageScoreAndAlerts } from '../../api.mts'
+import type {
+  PackageRequestOptions,
+  PackageScoreAndAlerts,
+} from '../../api.mts'
 import { safeDeleteSync } from '@socketsecurity/lib/fs/safe'
 import { isObject } from '@socketsecurity/lib/objects/predicates'
 import { worstArtifactsByPurl } from './select-artifacts.mts'
@@ -168,6 +171,33 @@
   #generation = 0
   #contextReady = true
   private constructor() {}
+  async #resolvePackageFetchRequest(
+    signal: AbortSignal,
+    timeout: number,
+  ): Promise<{
+    apiKey: string | undefined
+    options: PackageRequestOptions
+  }> {
+    const transport = { signal, timeout }
+    const configuredSlug = vscode.workspace
+      .getConfiguration()
+      .get('socket-security.orgSlug')
+    if (typeof configuredSlug !== 'string' || configuredSlug.length === 0) {
+      return { apiKey: await getAPIKey(), options: transport }
+    }
+    const authenticated = await getAuthenticatedContext()
+    if (!authenticated) {
+      return { apiKey: await getAPIKey(), options: transport }
+    }
+    return {
+      apiKey: authenticated.accessToken,
+      options: {
+        orgSlug: authenticated.organization.slug,
+        signal,
+        timeout,
+      },
+    }
+  }
   clear(requeueActive = true) {
     this.#generation += 1
     if (!requeueActive) {
@@ -280,17 +310,21 @@
         )
       })
       try {
-        const apiKey = await getAPIKey()
+        const request = await this.#resolvePackageFetchRequest(
+          controller.signal,
+          this.timeout,
+        )
         if (controller.signal.aborted || generation !== this.#generation) {
           return
         }
         // logger.info(`Requesting Socket API for PURLs: ${[...thesePendingUpdates].join(', ')}`)
         // Bound the SDK request with the same ceiling the AbortController timer
         // uses so a hung connection can't leave entries pending forever.
-        const scores = streamPackageScores(apiKey, [...thesePendingUpdates], {
-          signal: controller.signal,
-          timeout: this.timeout,
-        })
+        const scores = streamPackageScores(
+          request.apiKey,
+          [...thesePendingUpdates],
+          request.options,
+        )
         // The /v0/purl endpoint can stream MULTIPLE artifacts for the same
         // input PURL (e.g. a PyPI sdist and wheel of one version) with
         // different scores and alerts. Buffer them all, then collapse to the

diff --git a/test/repo/unit/api.test.mts b/test/repo/unit/api.test.mts
--- a/test/repo/unit/api.test.mts
+++ b/test/repo/unit/api.test.mts
@@ -404,6 +404,22 @@
     expect(seen.map(s => s.score.overall)).toEqual([42, 99])
   })
 
+  test('requests the organization purl endpoint for a selected org', async () => {
+    const purl = 'pkg:npm/example-org-policy@1.0.0' as SimPURL
+    const scope = nock(API_ORIGIN)
+      .post('/v0/orgs/example-org/purl', { components: [{ purl }] })
+      .query({ alerts: 'true', compact: 'false' })
+      .reply(200, `${artifactLine(purl, 0.5)}\n`)
+
+    const seen = await collectPackageScores(
+      streamPackageScores(TOKEN, [purl], { orgSlug: 'example-org' }),
+    )
+
+    expect(seen).toHaveLength(1)
+    expect(seen[0]?.score.overall).toBe(0.5)
+    expect(scope.isDone()).toBe(true)
+  })
+
   test('requests /v0/purl with the pending purls in the body', async () => {
     const purl = 'pkg:npm/express@4.0.0' as SimPURL
     let sentBody: unknown

diff --git a/test/repo/unit/ui/purl-alerts-and-scores/manager-cache-requests.test.mts b/test/repo/unit/ui/purl-alerts-and-scores/manager-cache-requests.test.mts
--- a/test/repo/unit/ui/purl-alerts-and-scores/manager-cache-requests.test.mts
+++ b/test/repo/unit/ui/purl-alerts-and-scores/manager-cache-requests.test.mts
@@ -4,11 +4,15 @@
 import type { logger as realLogger } from '../../../../../src/infra/log.mts'
 import type { SimPURL } from '../../../../../src/ui/externals/parse-externals.mts'
 import { PURLDataCache } from '../../../../../src/ui/purl-alerts-and-scores/manager.mts'
+import { setStubWorkspaceState } from '../../../../stubs/vscode.mts'
 
-const { getAPIKey, streamPackageScores } = vi.hoisted(() => ({
-  getAPIKey: vi.fn(),
-  streamPackageScores: vi.fn(),
-}))
+const { getAPIKey, getAuthenticatedContext, streamPackageScores } = vi.hoisted(
+  () => ({
+    getAPIKey: vi.fn(),
+    getAuthenticatedContext: vi.fn(),
+    streamPackageScores: vi.fn(),
+  }),
+)
 
 vi.mock(import('node:fs'), async importOriginal => ({
   ...(await importOriginal()),
@@ -20,7 +24,10 @@
   writeFileSync: vi.fn(),
 }))
 vi.mock(import('../../../../../src/api.mts'), () => ({ streamPackageScores }))
-vi.mock(import('../../../../../src/auth.mts'), () => ({ getAPIKey }))
+vi.mock(import('../../../../../src/auth.mts'), () => ({
+  getAPIKey,
+  getAuthenticatedContext,
+}))
 vi.mock(import('../../../../../src/infra/log.mts'), () => ({
   logger: {
     debug: vi.fn(),
@@ -60,6 +67,8 @@
     vi.useFakeTimers()
     vi.setSystemTime(new Date('2026-01-01T00:00:00Z'))
     getAPIKey.mockReset().mockResolvedValue('test-placeholder-token')
+    getAuthenticatedContext.mockReset().mockResolvedValue(undefined)
+    setStubWorkspaceState({})
     streamPackageScores.mockReset().mockImplementation(async function* (
       ...request: [string, SimPURL[]]
     ) {
@@ -141,6 +150,37 @@
     expect(entry.pkgData).toEqual(packageScore(purl))
   })
 
+  test('sends the selected organization slug with the package request', async () => {
+    setStubWorkspaceState({
+      configuration: { 'socket-security.orgSlug': 'example-org' },
+    })
+    getAuthenticatedContext.mockResolvedValue({
+      accessToken: 'test-placeholder-token',
+      organization: {
+        id: 'example-org',
+        image: undefined,
+        name: 'Example Organization',
+        plan: 'team',
+        slug: 'example-org',
+      },
+    })
+    const purl = 'pkg:npm/example-org-policy@1.0.0'
+    const entry = cache.watch(purl)
+    await flushRequests()
+
+    expect(getAPIKey).not.toHaveBeenCalled()
+    expect(streamPackageScores).toHaveBeenCalledWith(
+      'test-placeholder-token',
+      [purl],
+      {
+        orgSlug: 'example-org',
+        signal: expect.any(AbortSignal),
+        timeout: cache.timeout,
+      },
+    )
+    expect(entry.pkgData).toEqual(packageScore(purl))
+  })
+
   test('uses anonymous lookup without a token and clears the timer', async () => {
     getAPIKey.mockResolvedValueOnce(undefined)
     const purl = 'pkg:npm/example-no-token@1.0.0'

You can send follow-ups to the cloud agent here.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 1555d38. Configure here.

@jdalton
John-David Dalton (jdalton) force-pushed the feat/package-intelligence-vscode-org-selection-delivery branch 4 times, most recently from 98f85dc to ac26ac7 Compare October 10, 2026 18:02
Comment thread scripts/fleet/npm/scan/staged.mts Resolved
@jdalton
John-David Dalton (jdalton) force-pushed the feat/package-intelligence-vscode-org-selection-delivery branch 3 times, most recently from cb950aa to 9ea2cd6 Compare November 8, 2026 16:43
@jdalton
John-David Dalton (jdalton) force-pushed the feat/package-intelligence-vscode-org-selection-delivery branch from 9ea2cd6 to 3d71523 Compare December 2, 2026 15:17
@jdalton
John-David Dalton (jdalton) force-pushed the feat/package-intelligence-vscode-org-selection-delivery branch from 3d71523 to ec7cc4a Compare December 2, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant