Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/github/project-view-field-values.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, expect, it } from 'vitest'
import { normalizeLinkedPullRequests } from './project-view/project-view-field-normalization'
describe('normalizeLinkedPullRequests linked PR values', () => {
it('normalizes linked pull requests with a visible overflow count', () => {
const value = normalizeLinkedPullRequests({
__typename: 'ProjectV2ItemFieldPullRequestValue',
field: { id: 'field-pr', name: 'Linked pull requests', dataType: 'LINKED_PULL_REQUESTS' },
pullRequests: {
totalCount: 12,
nodes: [{ number: 12, title: 'Fix', url: 'https://github.com/o/r/pull/12' }, null]
}
})
expect(value).toEqual({
pullRequests: [{ number: 12, title: 'Fix', url: 'https://github.com/o/r/pull/12' }],
totalCount: 12
})
})
})
53 changes: 36 additions & 17 deletions src/main/github/project-view/project-error-classification.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { OptionalProjectItemField } from './project-view-cache'
import type { GitHubProjectViewError } from '../../../shared/github/project-result-types'
import { githubProjectHost } from '../../../shared/github/project-identity'

export type GhGraphqlError = {
type?: string
message?: string
path?: (string | number)[]
extensions?: { code?: string }
extensions?: { code?: string; fieldName?: string; typeName?: string }
}

export function extractGraphqlErrors(stderr: string, stdout: string): GhGraphqlError[] {
Expand All @@ -27,25 +28,43 @@ export function extractGraphqlErrors(stderr: string, stdout: string): GhGraphqlE
return []
}

export function errorsIndicateParentField(errors: GhGraphqlError[], stderr: string): boolean {
const lower = stderr.toLowerCase()
if (lower.includes('preview') && lower.includes('parent')) {
return true
export function unsupportedItemFieldsFromErrors(
errors: GhGraphqlError[],
stderr: string
): OptionalProjectItemField[] {
const unsupported = new Set<OptionalProjectItemField>()
if (/preview/i.test(stderr) && /\bparent\b/i.test(stderr)) {
unsupported.add('parent')
}
return errors.some((e) => {
const type = (e.type ?? '').toUpperCase()
if (type === 'FIELD_NOT_FOUND' || type === 'UNDEFINED_FIELD' || type === 'FIELD_ERRORS') {
const tail = e.path?.at(-1)
if (tail === 'parent') {
return true
}
// FIELD_ERRORS often omits `path`; match on message for the parent field.
if ((e.message ?? '').toLowerCase().includes('parent')) {
return true
for (const error of errors) {
const code = (error.type ?? error.extensions?.code ?? '').replace(/_/g, '').toUpperCase()
const message = error.message ?? ''
const missing =
/doesn't exist|does not exist|cannot query field|unknown field|undefined field|no such type/i.test(
message
)
if (
!['FIELDNOTFOUND', 'UNDEFINEDFIELD', 'FIELDERRORS', 'UNDEFINEDTYPE'].includes(code) &&
!(code === '' && missing)
) {
continue
}
for (const field of ['parent', 'subIssuesSummary', 'linkedPullRequests'] as const) {
const name = field === 'linkedPullRequests' ? 'ProjectV2ItemFieldPullRequestValue' : field
if (
error.path?.at(-1) === name ||
error.extensions?.fieldName === name ||
error.extensions?.typeName === name ||
message
.toLowerCase()
.split(/[^a-z0-9_]+/)
.includes(name.toLowerCase())
) {
unsupported.add(field)
}
}
return false
})
}
return [...unsupported]
}

export function classifyProjectError(
Expand Down
24 changes: 19 additions & 5 deletions src/main/github/project-view/project-view-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function getProjectViewCacheEntry<K, V>(cache: Map<K, V>, key: K): V | undefined
// Module reloads must discard stale capability results.
const ownerTypeCache = new Map<string, GitHubProjectOwnerType | null>()
// One owner's unsupported parent field must not poison another owner's capability.
const parentFieldRetriedByOwner = new Map<string, true>()
export type OptionalProjectItemField = 'parent' | 'subIssuesSummary' | 'linkedPullRequests'
const unsupportedItemFieldsByOwner = new Map<string, ReadonlySet<OptionalProjectItemField>>()
const parentFieldWarningLoggedByOwner = new Map<string, true>()
// Share first-page capability probes per owner.
export const parentFieldProbeInFlight = new Map<string, Promise<void>>()
Expand Down Expand Up @@ -68,12 +69,25 @@ export function getCachedOwnerType(
return getProjectViewCacheEntry(ownerTypeCache, ownerTypeCacheKey(owner, host))
}

export function markItemFieldsUnsupported(
scopeKey: string,
fields: readonly OptionalProjectItemField[]
): void {
const unsupported = new Set(getUnsupportedItemFields(scopeKey))
fields.forEach((field) => unsupported.add(field))
rememberProjectViewCacheEntry(unsupportedItemFieldsByOwner, scopeKey, unsupported)
}

export function getUnsupportedItemFields(scopeKey: string): ReadonlySet<OptionalProjectItemField> {
return getProjectViewCacheEntry(unsupportedItemFieldsByOwner, scopeKey) ?? new Set()
}

export function markParentFieldRetried(scopeKey: string): void {
rememberProjectViewCacheEntry(parentFieldRetriedByOwner, scopeKey, true)
markItemFieldsUnsupported(scopeKey, ['parent'])
}

export function hasParentFieldRetried(scopeKey: string): boolean {
return getProjectViewCacheEntry(parentFieldRetriedByOwner, scopeKey) === true
return getUnsupportedItemFields(scopeKey).has('parent')
}

export function markParentFieldWarningLogged(scopeKey: string): void {
Expand All @@ -86,7 +100,7 @@ export function hasParentFieldWarningLogged(scopeKey: string): boolean {

export function _resetProjectViewCachesForTests(): void {
ownerTypeCache.clear()
parentFieldRetriedByOwner.clear()
unsupportedItemFieldsByOwner.clear()
parentFieldWarningLoggedByOwner.clear()
parentFieldProbeInFlight.clear()
}
Expand All @@ -99,7 +113,7 @@ export function _getProjectViewCacheSizesForTests(): {
} {
return {
ownerTypes: ownerTypeCache.size,
parentFieldRetries: parentFieldRetriedByOwner.size,
parentFieldRetries: unsupportedItemFieldsByOwner.size,
parentFieldWarnings: parentFieldWarningLoggedByOwner.size,
parentFieldProbes: parentFieldProbeInFlight.size
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/github/project-view/project-view-field-normalization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
GitHubProjectField,
GitHubProjectFieldValue,
GitHubProjectLinkedPullRequests,
GitHubProjectIteration,
GitHubProjectLabel,
GitHubProjectSingleSelectOption,
Expand Down Expand Up @@ -112,6 +113,10 @@ export type RawFieldValue = {
date?: string
labels?: { nodes?: RawLabel[] }
users?: { nodes?: RawUser[] }
pullRequests?: {
totalCount?: number
nodes?: ({ number?: number; title?: string; url?: string } | null)[]
}
}

export function normalizeFieldValue(
Expand Down Expand Up @@ -178,3 +183,29 @@ export function normalizeFieldValue(
return null
}
}

export function normalizeLinkedPullRequests(
raw: RawFieldValue
): GitHubProjectLinkedPullRequests | null {
if (raw.__typename !== 'ProjectV2ItemFieldPullRequestValue') {
return null
}
const pullRequests = (raw.pullRequests?.nodes ?? []).flatMap((node) =>
!node || typeof node.number !== 'number'
? []
: [
{
number: node.number,
title: typeof node.title === 'string' ? node.title : '',
url: typeof node.url === 'string' ? node.url : ''
}
]
)
return {
pullRequests,
totalCount:
typeof raw.pullRequests?.totalCount === 'number'
? raw.pullRequests.totalCount
: pullRequests.length
}
}
21 changes: 21 additions & 0 deletions src/main/github/project-view/project-view-item-normalization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
GitHubProjectFieldValue,
GitHubProjectLabel,
GitHubProjectLinkedPullRequests,
GitHubProjectRow,
GitHubProjectRowItemType,
GitHubProjectUser
Expand All @@ -10,6 +11,7 @@ import { driftError } from './project-error-classification'
import {
normalizeFieldValue,
normalizeLabel,
normalizeLinkedPullRequests,
normalizeUser,
type RawFieldValue,
type RawLabel,
Expand All @@ -30,6 +32,7 @@ type RawContent = {
assignees?: { nodes?: RawUser[] }
labels?: { nodes?: RawLabel[] }
parent?: { number?: number; title?: string; url?: string } | null
subIssuesSummary?: { completed?: number; total?: number; percentCompleted?: number }
issueType?: {
id?: string
name?: string
Expand Down Expand Up @@ -100,6 +103,17 @@ export function normalizeItem(raw: RawItem, position: number): NormalizedItemOut
typeof content.parent.url === 'string'
? { number: content.parent.number, title: content.parent.title, url: content.parent.url }
: null
const subIssuesSummary = content?.subIssuesSummary
const subIssuesProgress =
typeof subIssuesSummary?.completed === 'number' &&
typeof subIssuesSummary.total === 'number' &&
typeof subIssuesSummary.percentCompleted === 'number'
? {
completed: subIssuesSummary.completed,
total: subIssuesSummary.total,
percentCompleted: subIssuesSummary.percentCompleted
}
: null
const issueType =
content?.issueType &&
typeof content.issueType.id === 'string' &&
Expand All @@ -112,8 +126,13 @@ export function normalizeItem(raw: RawItem, position: number): NormalizedItemOut
typeof content.issueType.description === 'string' ? content.issueType.description : null
}
: null
let linkedPullRequests: GitHubProjectLinkedPullRequests | null = null
const fieldValuesByFieldId: Record<string, GitHubProjectFieldValue> = {}
for (const fv of raw.fieldValues?.nodes ?? []) {
if (fv.__typename === 'ProjectV2ItemFieldPullRequestValue') {
linkedPullRequests = normalizeLinkedPullRequests(fv)
continue
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const normalized = normalizeFieldValue(fv)
if (normalized) {
fieldValuesByFieldId[normalized.fieldId] = normalized
Expand Down Expand Up @@ -143,6 +162,8 @@ export function normalizeItem(raw: RawItem, position: number): NormalizedItemOut
assignees,
labels,
parentIssue,
linkedPullRequests,
subIssuesProgress,
issueType
},
fieldValuesByFieldId,
Expand Down
11 changes: 7 additions & 4 deletions src/main/github/project-view/project-view-item-page.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { OptionalProjectItemField } from './project-view-cache'
import type { GitHubProjectOwnerType } from '../../../shared/github/project-types'
import type { GitHubProjectViewError } from '../../../shared/github/project-result-types'
import {
Expand All @@ -20,7 +21,7 @@ import { ownerQueryRoot } from './project-view-config'
import type { RawItem } from './project-view-item-normalization'
import {
FIELD_CONFIG_FRAGMENT,
FIELD_VALUES_SELECTION,
fieldValuesSelection,
itemContentSelection
} from './project-view-query-fragments'

Expand All @@ -38,7 +39,7 @@ export async function fetchItemsPageWithRaw(args: {
query: string
first: number
after: string | null
includeParent: boolean
unsupportedFields: ReadonlySet<OptionalProjectItemField>
host?: string
}): Promise<
| { ok: true; page: RawItemsPage }
Expand Down Expand Up @@ -67,8 +68,10 @@ export async function fetchItemsPageWithRaw(args: {
id
type
updatedAt
content { ${itemContentSelection(args.includeParent)} }
${FIELD_VALUES_SELECTION}
content {
${itemContentSelection(!args.unsupportedFields.has('parent'), !args.unsupportedFields.has('subIssuesSummary'))}
}
${fieldValuesSelection(!args.unsupportedFields.has('linkedPullRequests'))}
}
}
}
Expand Down
Loading
Loading