diff --git a/apps/sim/lib/sim-search/live/activity-providers.test.ts b/apps/sim/lib/sim-search/live/activity-providers.test.ts index 6c5eab5895b..76d6e70d024 100644 --- a/apps/sim/lib/sim-search/live/activity-providers.test.ts +++ b/apps/sim/lib/sim-search/live/activity-providers.test.ts @@ -6,6 +6,7 @@ import { atlassianActivity } from '@/lib/sim-search/live/atlassian' import { githubActivity } from '@/lib/sim-search/live/github' import { gitLabActivity, gitLabMemberActivity } from '@/lib/sim-search/live/gitlab-activity' import { calendarActivity, driveActivity, gmailActivity } from '@/lib/sim-search/live/google' +import { NativeSearchError } from '@/lib/sim-search/live/http' import { linearActivity } from '@/lib/sim-search/live/linear' import { slackActivity } from '@/lib/sim-search/live/slack' import type { NativeActivityInput, NativeClient } from '@/lib/sim-search/live/types' @@ -476,7 +477,15 @@ describe('GitHub activity', () => { it('reports what the member opened, committed and is involved in, each at its own moment', async () => { const api = client((path, options) => { if (path === '/user') return { login: 'octo' } + if (path === '/user/orgs') return [{ login: 'acme' }] + if (path === '/user/repos') + return [{ owner: { login: 'friend', type: 'User' } }, { owner: { login: 'acme' } }] const q = String(options?.query?.q) + /** As GitHub answers some connections: an unscoped search, or an issue search naming no type, is a 422. */ + if (!/ user:octo org:acme user:friend /.test(` ${q} `)) + throw new NativeSearchError('unavailable', 'The provider rejected this query (422).') + if (path === '/search/issues' && !/ is:(issue|pull-request)( |$)/.test(q)) + throw new NativeSearchError('unavailable', 'The provider rejected this query (422).') if (path === '/search/commits') return { total_count: 1, @@ -497,6 +506,7 @@ describe('GitHub activity', () => { repository_url: 'https://api.github.com/repos/acme/app', ...extra, }) + if (!q.includes('is:pull-request')) return { total_count: 0, items: [] } return q.startsWith('author:octo') ? { total_count: 1, items: [issue(7, { created_at: '2026-11-05T10:00:00Z' })] } : { total_count: 3, items: [issue(9, { updated_at: '2026-11-06T10:00:00Z' })] } @@ -509,7 +519,23 @@ describe('GitHub activity', () => { ['committed', '2026-11-04T10:00:00.000Z'], ['involved', '2026-11-06T10:00:00.000Z'], ]) - expect(page.streams.map((stream) => stream.more)).toEqual([false, false, true]) + expect(page.streams.map((stream) => stream.more)).toEqual([false, false, false, false, true]) + expect(page.partial).toBeUndefined() + }) + + it('reports a partial listing when the owners it scopes to were capped', async () => { + const api = client((path) => { + if (path === '/user') return { login: 'octo' } + if (path === '/user/orgs') + return Array.from({ length: 100 }, (_, index) => ({ login: `org-${index}` })) + if (path === '/user/repos') return [] + return { total_count: 0, items: [] } + }) + const page = await githubActivity(api, WINDOW) + expect(page).toMatchObject({ + partial: true, + message: expect.stringContaining('Only some of the organizations'), + }) }) }) diff --git a/apps/sim/lib/sim-search/live/github.ts b/apps/sim/lib/sim-search/live/github.ts index c4f8ba2c861..6b6801a0bee 100644 --- a/apps/sim/lib/sim-search/live/github.ts +++ b/apps/sim/lib/sim-search/live/github.ts @@ -166,6 +166,65 @@ async function githubPersonLogin(client: NativeClient, involving: string): Promi const GITHUB_LOGIN = /^[A-Za-z\d](?:[A-Za-z\d-]{0,38})$/ +/** + * GitHub's issue search refuses a query that names neither issues nor pull requests, so a query + * without an `is:`/`type:` qualifier runs once for each. + */ +function issueTypeQueries(query: string): string[] { + return /(?:^|\s)(?:is|type):(?:issue|pr|pull-request)(?:\s|$)/i.test(query) + ? [query] + : ['issue', 'pull-request'].map((type) => `${query} is:${type}`) +} + +/** The most owners in one activity scope, so the qualifiers stay well inside GitHub's query limit. */ +const GITHUB_ACTIVITY_SCOPE_BYTES = 2000 + +/** + * The accounts that own the member's repositories as `user:`/`org:` qualifiers: the member, + * their organizations, and the owners of repositories they collaborate on. GitHub refuses some + * connections' unscoped searches, and owners cover the member's work without a repository cap. + * `partial` is set when a listing reached its page size or the scope reached its byte bound. + */ +async function githubOwnerScope( + client: NativeClient, + login: string +): Promise<{ scope: string; partial: boolean }> { + const [organizations, collaborations] = await Promise.all([ + client.json('/user/orgs', { query: { per_page: '100' }, memo: true }).then(array), + client + .json('/user/repos', { + query: { affiliation: 'collaborator', per_page: '100', sort: 'pushed' }, + memo: true, + }) + .then(array), + ]) + const owners = new Map([[login.toLowerCase(), 'user']]) + const add = (name: string, kind: 'user' | 'org') => { + if (GITHUB_LOGIN.test(name) && !owners.has(name.toLowerCase())) + owners.set(name.toLowerCase(), kind) + } + for (const row of organizations) add(string(row.login), 'org') + for (const row of collaborations) { + const owner = object(row.owner) + add(string(owner.login), string(owner.type) === 'Organization' ? 'org' : 'user') + } + const qualifiers: string[] = [] + let bytes = 0 + for (const [name, kind] of owners) { + const qualifier = `${kind}:${name}` + if (bytes + qualifier.length + 1 > GITHUB_ACTIVITY_SCOPE_BYTES) break + qualifiers.push(qualifier) + bytes += qualifier.length + 1 + } + return { + scope: qualifiers.join(' '), + partial: + qualifiers.length < owners.size || + organizations.length === 100 || + collaborations.length === 100, + } +} + /** Whether a query already names a qualifier; GitHub ORs a repeated one rather than ANDing it. */ const hasGitHubQualifier = (query: string, name: string) => githubTokens(query).some((token) => token.toLowerCase().startsWith(`${name}:`)) @@ -355,15 +414,13 @@ export async function searchGitHub( : 'Searched GitHub issues, pull requests, and code.' ) } - if ( - input.native.kind === 'issues' && - !/(?:^|\s)(?:is|type):(?:issue|pr|pull-request)(?:\s|$)/i.test(query) - ) + const typed = input.native.kind === 'issues' ? issueTypeQueries(query) : [query] + if (typed.length > 1) return collectNativePages( - ['issue', 'pull-request'].map((kind) => + typed.map((typedQuery) => searchGitHub(client, { ...input, - native: { ...input.native!, query: `${query} is:${kind}` }, + native: { ...input.native!, query: typedQuery }, }) ), 'Searched issues and pull requests separately.' @@ -518,15 +575,42 @@ export async function githubActivity( input: NativeActivityInput ): Promise { const login = await githubPersonLogin(client, 'me') + const { scope, partial: scopeCapped } = await githubOwnerScope(client, login) + /** Issues and pull requests are separate streams, as GitHub's issue search requires. */ + const issueStreams = ( + person: string, + field: 'created' | 'updated', + action: 'created' | 'involved' + ) => + issueTypeQueries(`${person}:${login} ${scope}`).map((query) => + githubActivityStream(client, input, 'issues', query, field, action) + ) const streams = await Promise.all([ - githubActivityStream(client, input, 'issues', `author:${login}`, 'created', 'created'), - githubActivityStream(client, input, 'commits', `author:${login}`, 'author-date', 'committed'), - githubActivityStream(client, input, 'issues', `involves:${login}`, 'updated', 'involved'), + ...issueStreams('author', 'created', 'created'), + githubActivityStream( + client, + input, + 'commits', + `author:${login} ${scope}`, + 'author-date', + 'committed' + ), + ...issueStreams('involves', 'updated', 'involved'), ]) return { streams: streams.map(({ incomplete: _, ...stream }) => stream), - ...(streams.some(({ incomplete }) => incomplete) - ? { partial: true, message: 'GitHub could not complete part of this search in time.' } + ...(streams.some(({ incomplete }) => incomplete) || scopeCapped + ? { + partial: true, + message: joinMessages([ + streams.some(({ incomplete }) => incomplete) + ? 'GitHub could not complete part of this search in time.' + : undefined, + scopeCapped + ? 'Only some of the organizations and repository owners you work with were searched.' + : undefined, + ]), + } : {}), } }