Skip to content

Commit 650608f

Browse files
authored
fix(search): scope GitHub activity searches and split issues from pull requests (#10577)
* fix(search): scope GitHub activity to the member's account and organizations, one stream per issue type GitHub refuses some connections' issue and commit searches that name no owner, and every issue search that names neither issues nor pull requests (422), so list_my_activity read no GitHub activity on those connections. Activity searches now carry user:/org: qualifiers and split issues from pull requests, reusing the split native issue search already makes. * fix(search): include collaborator repository owners in the GitHub activity scope and report a capped scope as partial
1 parent 836d31a commit 650608f

2 files changed

Lines changed: 122 additions & 12 deletions

File tree

‎apps/sim/lib/sim-search/live/activity-providers.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { atlassianActivity } from '@/lib/sim-search/live/atlassian'
1111
import { githubActivity } from '@/lib/sim-search/live/github'
1212
import { gitLabActivity, gitLabMemberActivity } from '@/lib/sim-search/live/gitlab-activity'
1313
import { calendarActivity, driveActivity, gmailActivity } from '@/lib/sim-search/live/google'
14+
import { NativeSearchError } from '@/lib/sim-search/live/http'
1415
import { linearActivity } from '@/lib/sim-search/live/linear'
1516
import { slackActivity } from '@/lib/sim-search/live/slack'
1617
import type { NativeActivityInput, NativeClient } from '@/lib/sim-search/live/types'
@@ -481,7 +482,15 @@ describe('GitHub activity', () => {
481482
it('reports what the member opened, committed and is involved in, each at its own moment', async () => {
482483
const api = client((path, options) => {
483484
if (path === '/user') return { login: 'octo' }
485+
if (path === '/user/orgs') return [{ login: 'acme' }]
486+
if (path === '/user/repos')
487+
return [{ owner: { login: 'friend', type: 'User' } }, { owner: { login: 'acme' } }]
484488
const q = String(options?.query?.q)
489+
/** As GitHub answers some connections: an unscoped search, or an issue search naming no type, is a 422. */
490+
if (!/ user:octo org:acme user:friend /.test(` ${q} `))
491+
throw new NativeSearchError('unavailable', 'The provider rejected this query (422).')
492+
if (path === '/search/issues' && !/ is:(issue|pull-request)( |$)/.test(q))
493+
throw new NativeSearchError('unavailable', 'The provider rejected this query (422).')
485494
if (path === '/search/commits')
486495
return {
487496
total_count: 1,
@@ -502,6 +511,7 @@ describe('GitHub activity', () => {
502511
repository_url: 'https://api.github.com/repos/acme/app',
503512
...extra,
504513
})
514+
if (!q.includes('is:pull-request')) return { total_count: 0, items: [] }
505515
return q.startsWith('author:octo')
506516
? { total_count: 1, items: [issue(7, { created_at: '2026-11-05T10:00:00Z' })] }
507517
: { total_count: 3, items: [issue(9, { updated_at: '2026-11-06T10:00:00Z' })] }
@@ -514,7 +524,23 @@ describe('GitHub activity', () => {
514524
['committed', '2026-11-04T10:00:00.000Z'],
515525
['involved', '2026-11-06T10:00:00.000Z'],
516526
])
517-
expect(page.streams.map((stream) => stream.more)).toEqual([false, false, true])
527+
expect(page.streams.map((stream) => stream.more)).toEqual([false, false, false, false, true])
528+
expect(page.partial).toBeUndefined()
529+
})
530+
531+
it('reports a partial listing when the owners it scopes to were capped', async () => {
532+
const api = client((path) => {
533+
if (path === '/user') return { login: 'octo' }
534+
if (path === '/user/orgs')
535+
return Array.from({ length: 100 }, (_, index) => ({ login: `org-${index}` }))
536+
if (path === '/user/repos') return []
537+
return { total_count: 0, items: [] }
538+
})
539+
const page = await githubActivity(api, WINDOW)
540+
expect(page).toMatchObject({
541+
partial: true,
542+
message: expect.stringContaining('Only some of the organizations'),
543+
})
518544
})
519545
})
520546

‎apps/sim/lib/sim-search/live/github.ts‎

Lines changed: 95 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,65 @@ async function githubPersonLogin(client: NativeClient, involving: string): Promi
166166

167167
const GITHUB_LOGIN = /^[A-Za-z\d](?:[A-Za-z\d-]{0,38})$/
168168

169+
/**
170+
* GitHub's issue search refuses a query that names neither issues nor pull requests, so a query
171+
* without an `is:`/`type:` qualifier runs once for each.
172+
*/
173+
function issueTypeQueries(query: string): string[] {
174+
return /(?:^|\s)(?:is|type):(?:issue|pr|pull-request)(?:\s|$)/i.test(query)
175+
? [query]
176+
: ['issue', 'pull-request'].map((type) => `${query} is:${type}`)
177+
}
178+
179+
/** The most owners in one activity scope, so the qualifiers stay well inside GitHub's query limit. */
180+
const GITHUB_ACTIVITY_SCOPE_BYTES = 2000
181+
182+
/**
183+
* The accounts that own the member's repositories as `user:`/`org:` qualifiers: the member,
184+
* their organizations, and the owners of repositories they collaborate on. GitHub refuses some
185+
* connections' unscoped searches, and owners cover the member's work without a repository cap.
186+
* `partial` is set when a listing reached its page size or the scope reached its byte bound.
187+
*/
188+
async function githubOwnerScope(
189+
client: NativeClient,
190+
login: string
191+
): Promise<{ scope: string; partial: boolean }> {
192+
const [organizations, collaborations] = await Promise.all([
193+
client.json('/user/orgs', { query: { per_page: '100' }, memo: true }).then(array),
194+
client
195+
.json('/user/repos', {
196+
query: { affiliation: 'collaborator', per_page: '100', sort: 'pushed' },
197+
memo: true,
198+
})
199+
.then(array),
200+
])
201+
const owners = new Map<string, 'user' | 'org'>([[login.toLowerCase(), 'user']])
202+
const add = (name: string, kind: 'user' | 'org') => {
203+
if (GITHUB_LOGIN.test(name) && !owners.has(name.toLowerCase()))
204+
owners.set(name.toLowerCase(), kind)
205+
}
206+
for (const row of organizations) add(string(row.login), 'org')
207+
for (const row of collaborations) {
208+
const owner = object(row.owner)
209+
add(string(owner.login), string(owner.type) === 'Organization' ? 'org' : 'user')
210+
}
211+
const qualifiers: string[] = []
212+
let bytes = 0
213+
for (const [name, kind] of owners) {
214+
const qualifier = `${kind}:${name}`
215+
if (bytes + qualifier.length + 1 > GITHUB_ACTIVITY_SCOPE_BYTES) break
216+
qualifiers.push(qualifier)
217+
bytes += qualifier.length + 1
218+
}
219+
return {
220+
scope: qualifiers.join(' '),
221+
partial:
222+
qualifiers.length < owners.size ||
223+
organizations.length === 100 ||
224+
collaborations.length === 100,
225+
}
226+
}
227+
169228
/** Whether a query already names a qualifier; GitHub ORs a repeated one rather than ANDing it. */
170229
const hasGitHubQualifier = (query: string, name: string) =>
171230
githubTokens(query).some((token) => token.toLowerCase().startsWith(`${name}:`))
@@ -355,15 +414,13 @@ export async function searchGitHub(
355414
: 'Searched GitHub issues, pull requests, and code.'
356415
)
357416
}
358-
if (
359-
input.native.kind === 'issues' &&
360-
!/(?:^|\s)(?:is|type):(?:issue|pr|pull-request)(?:\s|$)/i.test(query)
361-
)
417+
const typed = input.native.kind === 'issues' ? issueTypeQueries(query) : [query]
418+
if (typed.length > 1)
362419
return collectNativePages(
363-
['issue', 'pull-request'].map((kind) =>
420+
typed.map((typedQuery) =>
364421
searchGitHub(client, {
365422
...input,
366-
native: { ...input.native!, query: `${query} is:${kind}` },
423+
native: { ...input.native!, query: typedQuery },
367424
})
368425
),
369426
'Searched issues and pull requests separately.'
@@ -518,15 +575,42 @@ export async function githubActivity(
518575
input: NativeActivityInput
519576
): Promise<NativeActivityPage> {
520577
const login = await githubPersonLogin(client, 'me')
578+
const { scope, partial: scopeCapped } = await githubOwnerScope(client, login)
579+
/** Issues and pull requests are separate streams, as GitHub's issue search requires. */
580+
const issueStreams = (
581+
person: string,
582+
field: 'created' | 'updated',
583+
action: 'created' | 'involved'
584+
) =>
585+
issueTypeQueries(`${person}:${login} ${scope}`).map((query) =>
586+
githubActivityStream(client, input, 'issues', query, field, action)
587+
)
521588
const streams = await Promise.all([
522-
githubActivityStream(client, input, 'issues', `author:${login}`, 'created', 'created'),
523-
githubActivityStream(client, input, 'commits', `author:${login}`, 'author-date', 'committed'),
524-
githubActivityStream(client, input, 'issues', `involves:${login}`, 'updated', 'involved'),
589+
...issueStreams('author', 'created', 'created'),
590+
githubActivityStream(
591+
client,
592+
input,
593+
'commits',
594+
`author:${login} ${scope}`,
595+
'author-date',
596+
'committed'
597+
),
598+
...issueStreams('involves', 'updated', 'involved'),
525599
])
526600
return {
527601
streams: streams.map(({ incomplete: _, ...stream }) => stream),
528-
...(streams.some(({ incomplete }) => incomplete)
529-
? { partial: true, message: 'GitHub could not complete part of this search in time.' }
602+
...(streams.some(({ incomplete }) => incomplete) || scopeCapped
603+
? {
604+
partial: true,
605+
message: joinMessages([
606+
streams.some(({ incomplete }) => incomplete)
607+
? 'GitHub could not complete part of this search in time.'
608+
: undefined,
609+
scopeCapped
610+
? 'Only some of the organizations and repository owners you work with were searched.'
611+
: undefined,
612+
]),
613+
}
530614
: {}),
531615
}
532616
}

0 commit comments

Comments
 (0)