Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions services/github/pod-github/src/sync/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
githubExternalSyncVersion,
githubSyncVersion
} from '../types'
import { collectUpdate, deleteObjects, errorToObj, getSince, isGHWriteAllowed } from './utils'
import { collectUpdate, deleteObjects, ensureGraphQLOctokit, errorToObj, getSince, isGHWriteAllowed } from './utils'

import { Analytics } from '@hcengineering/analytics'
import { IssueComment, IssueCommentCreatedEvent, IssueCommentEvent } from '@octokit/webhooks-types'
Expand Down Expand Up @@ -144,7 +144,10 @@ export class CommentSyncManager implements DocSyncManager {
account: PersonId,
id: string
): Promise<void> {
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)

const q = `mutation deleteComment($commentID: ID!) {
deleteIssueComment(
Expand Down
7 changes: 5 additions & 2 deletions services/github/pod-github/src/sync/issueBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { Octokit } from 'octokit'
import { ContainerFocus, IntegrationManager, githubExternalSyncVersion, githubSyncVersion } from '../types'
import { IssueExternalData } from './githubTypes'
import { stripGuestLink } from './guest'
import { collectUpdate, compareMarkdown, deleteObjects, errorToObj, guessStatus } from './utils'
import { collectUpdate, compareMarkdown, deleteObjects, ensureGraphQLOctokit, errorToObj, guessStatus } from './utils'

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

const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)

// Remove current same values from update
for (const [k, v] of Object.entries(update)) {
Expand Down
22 changes: 15 additions & 7 deletions services/github/pod-github/src/sync/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
} from '../types'
import { IssueExternalData, issueDetails } from './githubTypes'
import { GithubIssueData, IssueSyncManagerBase, IssueUpdate, WithMarkup } from './issueBase'
import { getSince, gqlp, guessStatus, isGHWriteAllowed, syncRunner } from './utils'
import { ensureGraphQLOctokit, getSince, gqlp, guessStatus, isGHWriteAllowed, syncRunner } from './utils'

export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncManager {
createPromise: Promise<IssueExternalData | undefined> | undefined
Expand Down Expand Up @@ -608,6 +608,8 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
okit: Octokit,
account: PersonId
): Promise<boolean> {
const graphqlOkit = ensureGraphQLOctokit(okit, container)

const { state, stateReason, body, ...issueUpdate } = await this.collectIssueUpdate(
info,
existing,
Expand All @@ -624,7 +626,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
// We should allow modification from user.

const closeIssue = async (): Promise<void> => {
await okit.graphql(
await graphqlOkit.graphql(
`
mutation closeIssue($issue: ID!) {
closeIssue(input: {
Expand All @@ -642,7 +644,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
}

const reopenIssue = async (): Promise<void> => {
await okit.graphql(
await graphqlOkit.graphql(
`
mutation reopenIssue($issue: ID!) {
reopenIssue(input: {
Expand Down Expand Up @@ -675,7 +677,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
// We need to call re-open issue
await reopenIssue()
}
await okit.graphql(
await graphqlOkit.graphql(
`
mutation updateIssue($issue: ID!, $body: String! ) {
updateIssue(input: {
Expand Down Expand Up @@ -713,7 +715,7 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
await reopenIssue()
}
if (hasOtherChanges) {
await okit.graphql(
await graphqlOkit.graphql(
`
mutation updateIssue($issue: ID!) {
updateIssue(input: {
Expand Down Expand Up @@ -751,7 +753,10 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
): Promise<IssueExternalData | undefined> {
const existingIssue = existing

const okit = (await this.provider.getOctokit(ctx, existingIssue.modifiedBy)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, existingIssue.modifiedBy)) ?? container.container.octokit,
container
)

const repoId = repository.nodeId

Expand Down Expand Up @@ -797,7 +802,10 @@ export class IssueSyncManager extends IssueSyncManagerBase implements DocSyncMan
account: PersonId,
id: string
): Promise<void> {
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)

const q = `mutation deleteIssue($issueID: ID!) {
deleteIssue(
Expand Down
7 changes: 5 additions & 2 deletions services/github/pod-github/src/sync/pullrequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
} from './githubTypes'
import { GithubIssueData, IssueSyncManagerBase, WithMarkup } from './issueBase'
import {
ensureGraphQLOctokit,
errorToObj,
getSinceRaw,
gqlp,
Expand Down Expand Up @@ -975,6 +976,8 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
okit: Octokit,
account: PersonId
): Promise<boolean> {
const graphqlOkit = ensureGraphQLOctokit(okit, container)

let { state, stateReason, body, ...issueUpdate } = await this.collectIssueUpdate(
info,
existing,
Expand Down Expand Up @@ -1006,7 +1009,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
workspace: this.provider.getWorkspaceId()
})
if (isGHWriteAllowed()) {
await okit.graphql(
await graphqlOkit.graphql(
`
mutation updatePullRequest($issue: ID!, $body: String!) {
updatePullRequest(input: {
Expand Down Expand Up @@ -1040,7 +1043,7 @@ export class PullRequestSyncManager extends IssueSyncManagerBase implements DocS
workspace: this.provider.getWorkspaceId()
})
if (isGHWriteAllowed()) {
await okit.graphql(
await graphqlOkit.graphql(
`
mutation updatePullRequest($issue: ID!) {
updatePullRequest(input: {
Expand Down
17 changes: 13 additions & 4 deletions services/github/pod-github/src/sync/reviewComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
githubSyncVersion
} from '../types'
import { ReviewComment as ReviewCommentExternalData, reviewCommentDetails } from './githubTypes'
import { collectUpdate, deleteObjects, errorToObj, isGHWriteAllowed } from './utils'
import { collectUpdate, deleteObjects, ensureGraphQLOctokit, errorToObj, isGHWriteAllowed } from './utils'

import { Analytics } from '@hcengineering/analytics'
import { PullRequestReviewCommentCreatedEvent, PullRequestReviewCommentEvent } from '@octokit/webhooks-types'
Expand Down Expand Up @@ -155,7 +155,10 @@ export class ReviewCommentSyncManager implements DocSyncManager {
derivedClient: TxOperations,
parent?: DocSyncInfo
): Promise<void> {
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)
const q = `mutation deleteReviewComment($reviewID: ID!) {
deletePullRequestReviewComment(input: {
id: $reviewID
Expand Down Expand Up @@ -435,7 +438,10 @@ export class ReviewCommentSyncManager implements DocSyncManager {
if (Object.keys(platformUpdate).length > 0) {
if (platformUpdate.body !== undefined) {
const body = await this.provider.getMarkupSafe(container.container, platformUpdate.body)
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)
const q = `mutation updateReviewComment($commentID: ID!, $body: String!) {
updatePullRequestReviewComment(input: {
threadId: $threadID
Expand Down Expand Up @@ -510,7 +516,10 @@ export class ReviewCommentSyncManager implements DocSyncManager {
return {}
}
const existingReview = existing as GithubReviewComment
const okit = (await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit,
container
)

// No external version yet, create it.
try {
Expand Down
20 changes: 17 additions & 3 deletions services/github/pod-github/src/sync/reviewThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ import {
getUpdatedAtReviewThread,
reviewThreadDetails
} from './githubTypes'
import { collectUpdate, deleteObjects, errorToObj, isGHWriteAllowed, syncChilds, syncDerivedDocuments } from './utils'
import {
collectUpdate,
deleteObjects,
ensureGraphQLOctokit,
errorToObj,
isGHWriteAllowed,
syncChilds,
syncDerivedDocuments
} from './utils'

import { Analytics } from '@hcengineering/analytics'
import { PullRequestReviewThreadEvent } from '@octokit/webhooks-types'
Expand Down Expand Up @@ -371,7 +379,10 @@ export class ReviewThreadSyncManager implements DocSyncManager {
if (Object.keys(platformUpdate).length > 0) {
// Check and update external
if (platformUpdate.isResolved !== undefined && githubConfiguration.ResolveThreadSupported) {
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)
const q = `mutation updateReviewThread($threadID: ID!) {
${platformUpdate.isResolved ? 'resolveReviewThread' : 'unresolveReviewThread'} (
input: {
Expand Down Expand Up @@ -446,7 +457,10 @@ export class ReviewThreadSyncManager implements DocSyncManager {
return {}
}
const existingReview = existing as GithubReviewThread
const okit = (await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit,
container
)

// No external version yet, create it.
// Will be added into pending state.
Expand Down
12 changes: 9 additions & 3 deletions services/github/pod-github/src/sync/reviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
githubSyncVersion
} from '../types'
import { PullRequestExternalData, Review as ReviewExternalData, reviewDetails, toReviewState } from './githubTypes'
import { collectUpdate, deleteObjects, errorToObj, isGHWriteAllowed, syncChilds } from './utils'
import { collectUpdate, deleteObjects, ensureGraphQLOctokit, errorToObj, isGHWriteAllowed, syncChilds } from './utils'

import { Analytics } from '@hcengineering/analytics'
import { PullRequestReviewEvent, PullRequestReviewSubmittedEvent } from '@octokit/webhooks-types'
Expand Down Expand Up @@ -150,7 +150,10 @@ export class ReviewSyncManager implements DocSyncManager {
account: PersonId,
id: string
): Promise<void> {
const okit = (await this.provider.getOctokit(ctx, account)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, account)) ?? container.container.octokit,
container
)
const q = `mutation deleteReview($reviewID: ID!) {
deletePullRequestReview(input: {
pullRequestReviewId: $reviewID
Expand Down Expand Up @@ -432,7 +435,10 @@ export class ReviewSyncManager implements DocSyncManager {
return {}
}
const existingReview = existing as GithubReview
const okit = (await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit
const okit = ensureGraphQLOctokit(
(await this.provider.getOctokit(ctx, existingReview.modifiedBy)) ?? container.container.octokit,
container
)

// No external version yet, create it.
try {
Expand Down
17 changes: 16 additions & 1 deletion services/github/pod-github/src/sync/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { PlatformError, unknownStatus } from '@hcengineering/platform'
import task from '@hcengineering/task'
import { IssueStatus } from '@hcengineering/tracker'
import { deepEqual } from 'fast-equals'
import { githubExternalSyncVersion } from '../types'
import { Octokit } from 'octokit'
import { ContainerFocus, githubExternalSyncVersion } from '../types'

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

/**
* Ensures an Octokit instance has the graphql method available.
* If the provided okit doesn't have graphql, falls back to container.octokit which is guaranteed to have it.
* @param okit - The Octokit instance to check (may be undefined)
* @param container - The ContainerFocus containing the fallback octokit instance
* @returns An Octokit instance with graphql method available
*/
export function ensureGraphQLOctokit (okit: Octokit | undefined, container: ContainerFocus): Octokit {
if (okit !== undefined && typeof (okit as any).graphql === 'function') {
return okit
}
return container.container.octokit
}

/**
* @public
*/
Expand Down
Loading