Skip to content

Commit 34128df

Browse files
Fix okit.graphql is not a function (#10442)
Signed-off-by: Artem Savchenko <[email protected]>
1 parent 2446629 commit 34128df

File tree

8 files changed

+85
-24
lines changed

8 files changed

+85
-24
lines changed

services/github/pod-github/src/sync/comments.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
githubExternalSyncVersion,
2525
githubSyncVersion
2626
} from '../types'
27-
import { collectUpdate, deleteObjects, errorToObj, getSince, isGHWriteAllowed } from './utils'
27+
import { collectUpdate, deleteObjects, ensureGraphQLOctokit, errorToObj, getSince, isGHWriteAllowed } from './utils'
2828

2929
import { Analytics } from '@hcengineering/analytics'
3030
import { IssueComment, IssueCommentCreatedEvent, IssueCommentEvent } from '@octokit/webhooks-types'
@@ -144,7 +144,10 @@ export class CommentSyncManager implements DocSyncManager {
144144
account: PersonId,
145145
id: string
146146
): Promise<void> {
147-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
147+
const okit = ensureGraphQLOctokit(
148+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
149+
container
150+
)
148151

149152
const q = `mutation deleteComment($commentID: ID!) {
150153
deleteIssueComment(

services/github/pod-github/src/sync/issueBase.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { Octokit } from 'octokit'
3838
import { ContainerFocus, IntegrationManager, githubExternalSyncVersion, githubSyncVersion } from '../types'
3939
import { IssueExternalData } from './githubTypes'
4040
import { stripGuestLink } from './guest'
41-
import { collectUpdate, compareMarkdown, deleteObjects, errorToObj, guessStatus } from './utils'
41+
import { collectUpdate, compareMarkdown, deleteObjects, ensureGraphQLOctokit, errorToObj, guessStatus } from './utils'
4242

4343
/**
4444
* @public
@@ -338,7 +338,10 @@ export abstract class IssueSyncManagerBase {
338338
const allAttributes = this.client.getHierarchy().getAllAttributes(existingIssue._class)
339339
const platformUpdate = collectUpdate<Issue>(previousData, existingIssue, Array.from(allAttributes.keys()))
340340

341-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
341+
const okit = ensureGraphQLOctokit(
342+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
343+
container
344+
)
342345

343346
// Remove current same values from update
344347
for (const [k, v] of Object.entries(update)) {

services/github/pod-github/src/sync/issues.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import {
4848
} from '../types'
4949
import { IssueExternalData, issueDetails } from './githubTypes'
5050
import { GithubIssueData, IssueSyncManagerBase, IssueUpdate, WithMarkup } from './issueBase'
51-
import { getSince, gqlp, guessStatus, isGHWriteAllowed, syncRunner } from './utils'
51+
import { ensureGraphQLOctokit, getSince, gqlp, guessStatus, isGHWriteAllowed, syncRunner } from './utils'
5252

5353
export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncManager {
5454
createPromise: Promise<IssueExternalData | undefined> | undefined
@@ -608,6 +608,8 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
608608
okit: Octokit,
609609
account: PersonId
610610
): Promise<boolean> {
611+
const graphqlOkit = ensureGraphQLOctokit(okit, container)
612+
611613
const { state, stateReason, body, ...issueUpdate } = await this.collectIssueUpdate(
612614
info,
613615
existing,
@@ -624,7 +626,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
624626
// We should allow modification from user.
625627

626628
const closeIssue = async (): Promise<void> => {
627-
await okit.graphql(
629+
await graphqlOkit.graphql(
628630
`
629631
mutation closeIssue($issue: ID!) {
630632
closeIssue(input: {
@@ -642,7 +644,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
642644
}
643645

644646
const reopenIssue = async (): Promise<void> => {
645-
await okit.graphql(
647+
await graphqlOkit.graphql(
646648
`
647649
mutation reopenIssue($issue: ID!) {
648650
reopenIssue(input: {
@@ -675,7 +677,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
675677
// We need to call re-open issue
676678
await reopenIssue()
677679
}
678-
await okit.graphql(
680+
await graphqlOkit.graphql(
679681
`
680682
mutation updateIssue($issue: ID!, $body: String! ) {
681683
updateIssue(input: {
@@ -713,7 +715,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
713715
await reopenIssue()
714716
}
715717
if (hasOtherChanges) {
716-
await okit.graphql(
718+
await graphqlOkit.graphql(
717719
`
718720
mutation updateIssue($issue: ID!) {
719721
updateIssue(input: {
@@ -751,7 +753,10 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
751753
): Promise<IssueExternalData | undefined> {
752754
const existingIssue = existing
753755

754-
const okit = (await this.provider.getOctokit(ctx, existingIssue.modifiedBy)) ?? container.container.octokit
756+
const okit = ensureGraphQLOctokit(
757+
(await this.provider.getOctokit(ctx, existingIssue.modifiedBy)) ?? container.container.octokit,
758+
container
759+
)
755760

756761
const repoId = repository.nodeId
757762

@@ -797,7 +802,10 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
797802
account: PersonId,
798803
id: string
799804
): Promise<void> {
800-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
805+
const okit = ensureGraphQLOctokit(
806+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
807+
container
808+
)
801809

802810
const q = `mutation deleteIssue($issueID: ID!) {
803811
deleteIssue(

services/github/pod-github/src/sync/pullrequests.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
} from './githubTypes'
6262
import { GithubIssueData, IssueSyncManagerBase, WithMarkup } from './issueBase'
6363
import {
64+
ensureGraphQLOctokit,
6465
errorToObj,
6566
getSinceRaw,
6667
gqlp,
@@ -975,6 +976,8 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
975976
okit: Octokit,
976977
account: PersonId
977978
): Promise<boolean> {
979+
const graphqlOkit = ensureGraphQLOctokit(okit, container)
980+
978981
let { state, stateReason, body, ...issueUpdate } = await this.collectIssueUpdate(
979982
info,
980983
existing,
@@ -1006,7 +1009,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
10061009
workspace: this.provider.getWorkspaceId()
10071010
})
10081011
if (isGHWriteAllowed()) {
1009-
await okit.graphql(
1012+
await graphqlOkit.graphql(
10101013
`
10111014
mutation updatePullRequest($issue: ID!, $body: String!) {
10121015
updatePullRequest(input: {
@@ -1040,7 +1043,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
10401043
workspace: this.provider.getWorkspaceId()
10411044
})
10421045
if (isGHWriteAllowed()) {
1043-
await okit.graphql(
1046+
await graphqlOkit.graphql(
10441047
`
10451048
mutation updatePullRequest($issue: ID!) {
10461049
updatePullRequest(input: {

services/github/pod-github/src/sync/reviewComments.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
githubSyncVersion
3030
} from '../types'
3131
import { ReviewComment as ReviewCommentExternalData, reviewCommentDetails } from './githubTypes'
32-
import { collectUpdate, deleteObjects, errorToObj, isGHWriteAllowed } from './utils'
32+
import { collectUpdate, deleteObjects, ensureGraphQLOctokit, errorToObj, isGHWriteAllowed } from './utils'
3333

3434
import { Analytics } from '@hcengineering/analytics'
3535
import { PullRequestReviewCommentCreatedEvent, PullRequestReviewCommentEvent } from '@octokit/webhooks-types'
@@ -155,7 +155,10 @@ export class ReviewCommentSyncManager implements DocSyncManager {
155155
derivedClient: TxOperations,
156156
parent?: DocSyncInfo
157157
): Promise<void> {
158-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
158+
const okit = ensureGraphQLOctokit(
159+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
160+
container
161+
)
159162
const q = `mutation deleteReviewComment($reviewID: ID!) {
160163
deletePullRequestReviewComment(input: {
161164
id: $reviewID
@@ -435,7 +438,10 @@ export class ReviewCommentSyncManager implements DocSyncManager {
435438
if (Object.keys(platformUpdate).length > 0) {
436439
if (platformUpdate.body !== undefined) {
437440
const body = await this.provider.getMarkupSafe(container.container, platformUpdate.body)
438-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
441+
const okit = ensureGraphQLOctokit(
442+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
443+
container
444+
)
439445
const q = `mutation updateReviewComment($commentID: ID!, $body: String!) {
440446
updatePullRequestReviewComment(input: {
441447
threadId: $threadID
@@ -510,7 +516,10 @@ export class ReviewCommentSyncManager implements DocSyncManager {
510516
return {}
511517
}
512518
const existingReview = existing as GithubReviewComment
513-
const okit = (await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit
519+
const okit = ensureGraphQLOctokit(
520+
(await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit,
521+
container
522+
)
514523

515524
// No external version yet, create it.
516525
try {

services/github/pod-github/src/sync/reviewThreads.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ import {
3535
getUpdatedAtReviewThread,
3636
reviewThreadDetails
3737
} from './githubTypes'
38-
import { collectUpdate, deleteObjects, errorToObj, isGHWriteAllowed, syncChilds, syncDerivedDocuments } from './utils'
38+
import {
39+
collectUpdate,
40+
deleteObjects,
41+
ensureGraphQLOctokit,
42+
errorToObj,
43+
isGHWriteAllowed,
44+
syncChilds,
45+
syncDerivedDocuments
46+
} from './utils'
3947

4048
import { Analytics } from '@hcengineering/analytics'
4149
import { PullRequestReviewThreadEvent } from '@octokit/webhooks-types'
@@ -371,7 +379,10 @@ export class ReviewThreadSyncManager implements DocSyncManager {
371379
if (Object.keys(platformUpdate).length > 0) {
372380
// Check and update external
373381
if (platformUpdate.isResolved !== undefined && githubConfiguration.ResolveThreadSupported) {
374-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
382+
const okit = ensureGraphQLOctokit(
383+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
384+
container
385+
)
375386
const q = `mutation updateReviewThread($threadID: ID!) {
376387
${platformUpdate.isResolved ? 'resolveReviewThread' : 'unresolveReviewThread'} (
377388
input: {
@@ -446,7 +457,10 @@ export class ReviewThreadSyncManager implements DocSyncManager {
446457
return {}
447458
}
448459
const existingReview = existing as GithubReviewThread
449-
const okit = (await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit
460+
const okit = ensureGraphQLOctokit(
461+
(await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit,
462+
container
463+
)
450464

451465
// No external version yet, create it.
452466
// Will be added into pending state.

services/github/pod-github/src/sync/reviews.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
githubSyncVersion
3030
} from '../types'
3131
import { PullRequestExternalData, Review as ReviewExternalData, reviewDetails, toReviewState } from './githubTypes'
32-
import { collectUpdate, deleteObjects, errorToObj, isGHWriteAllowed, syncChilds } from './utils'
32+
import { collectUpdate, deleteObjects, ensureGraphQLOctokit, errorToObj, isGHWriteAllowed, syncChilds } from './utils'
3333

3434
import { Analytics } from '@hcengineering/analytics'
3535
import { PullRequestReviewEvent, PullRequestReviewSubmittedEvent } from '@octokit/webhooks-types'
@@ -150,7 +150,10 @@ export class ReviewSyncManager implements DocSyncManager {
150150
account: PersonId,
151151
id: string
152152
): Promise<void> {
153-
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
153+
const okit = ensureGraphQLOctokit(
154+
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
155+
container
156+
)
154157
const q = `mutation deleteReview($reviewID: ID!) {
155158
deletePullRequestReview(input: {
156159
pullRequestReviewId: $reviewID
@@ -432,7 +435,10 @@ export class ReviewSyncManager implements DocSyncManager {
432435
return {}
433436
}
434437
const existingReview = existing as GithubReview
435-
const okit = (await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit
438+
const okit = ensureGraphQLOctokit(
439+
(await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit,
440+
container
441+
)
436442

437443
// No external version yet, create it.
438444
try {

services/github/pod-github/src/sync/utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { PlatformError, unknownStatus } from '@hcengineering/platform'
2323
import task from '@hcengineering/task'
2424
import { IssueStatus } from '@hcengineering/tracker'
2525
import { deepEqual } from 'fast-equals'
26-
import { githubExternalSyncVersion } from '../types'
26+
import { Octokit } from 'octokit'
27+
import { ContainerFocus, githubExternalSyncVersion } from '../types'
2728

2829
/**
2930
* Return if github write operations are allowed.
@@ -35,6 +36,20 @@ export function isGHWriteAllowed (): boolean {
3536
return true
3637
}
3738

39+
/**
40+
* Ensures an Octokit instance has the graphql method available.
41+
* If the provided okit doesn't have graphql, falls back to container.octokit which is guaranteed to have it.
42+
* @param okit - The Octokit instance to check (may be undefined)
43+
* @param container - The ContainerFocus containing the fallback octokit instance
44+
* @returns An Octokit instance with graphql method available
45+
*/
46+
export function ensureGraphQLOctokit (okit: Octokit | undefined, container: ContainerFocus): Octokit {
47+
if (okit !== undefined && typeof (okit as any).graphql === 'function') {
48+
return okit
49+
}
50+
return container.container.octokit
51+
}
52+
3853
/**
3954
* @public
4055
*/

0 commit comments

Comments
 (0)