Skip to content

Commit

Permalink
fix: TeamMemberIntegrationAuth single fetch (#10426)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <matt.krick@gmail.com>
  • Loading branch information
mattkrick authored Oct 28, 2024
1 parent 2ce4068 commit 88fec61
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 76 deletions.
3 changes: 2 additions & 1 deletion packages/embedder/embedder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ tracer.init({
plugins: false,
version: process.env.npm_package_version
})
tracer.use('pg')
// The embedder queue is in PG & gets hits non-stop, which dirties up the logs. Ignore the polling query before enabling pg
// tracer.use('pg')

const run = async () => {
const SERVER_ID = process.env.SERVER_ID
Expand Down
10 changes: 5 additions & 5 deletions packages/server/dataloader/azureDevOpsLoaders.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import DataLoader from 'dataloader'
import {decode} from 'jsonwebtoken'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import getAzureDevOpsDimensionFieldMaps from '../postgres/queries/getAzureDevOpsDimensionFieldMaps'
import {IntegrationProviderAzureDevOps} from '../postgres/queries/getIntegrationProvidersByIds'
import insertTaskEstimate from '../postgres/queries/insertTaskEstimate'
import removeTeamMemberIntegrationAuthQuery from '../postgres/queries/removeTeamMemberIntegrationAuth'
import upsertTeamMemberIntegrationAuth from '../postgres/queries/upsertTeamMemberIntegrationAuth'
import {TeamMemberIntegrationAuth} from '../postgres/types'
import AzureDevOpsServerManager, {
ProjectRes,
Resource,
Expand Down Expand Up @@ -133,16 +133,16 @@ export interface AzureProject extends ProjectRes {

export const freshAzureDevOpsAuth = (
parent: RootDataLoader
): DataLoader<TeamUserKey, IGetTeamMemberIntegrationAuthQueryResult | null, string> => {
return new DataLoader<TeamUserKey, IGetTeamMemberIntegrationAuthQueryResult | null, string>(
): DataLoader<TeamUserKey, TeamMemberIntegrationAuth | null, string> => {
return new DataLoader<TeamUserKey, TeamMemberIntegrationAuth | null, string>(
async (keys) => {
const results = await Promise.allSettled(
keys.map(async ({userId, teamId}) => {
const azureDevOpsAuthToRefresh = (await parent.get('teamMemberIntegrationAuths').load({
const azureDevOpsAuthToRefresh = await parent.get('teamMemberIntegrationAuths').load({
service: 'azureDevOps',
teamId,
userId
})) as IGetTeamMemberIntegrationAuthQueryResult | null
})
if (azureDevOpsAuthToRefresh === null) {
return null
}
Expand Down
10 changes: 3 additions & 7 deletions packages/server/dataloader/gcalLoaders.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import DataLoader from 'dataloader'
import GcalOAuth2Manager from '../integrations/gcal/GcalOAuth2Manager'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import upsertTeamMemberIntegrationAuth from '../postgres/queries/upsertTeamMemberIntegrationAuth'
import {TeamMemberIntegrationAuth} from '../postgres/types'
import sendToSentry from '../utils/sendToSentry'
import RootDataLoader from './RootDataLoader'

export const freshGcalAuth = (parent: RootDataLoader) => {
return new DataLoader<
{teamId: string; userId: string},
IGetTeamMemberIntegrationAuthQueryResult | null,
string
>(
return new DataLoader<{teamId: string; userId: string}, TeamMemberIntegrationAuth | null, string>(
async (keys) => {
const results = await Promise.allSettled(
keys.map(async ({teamId, userId}) => {
Expand Down Expand Up @@ -48,7 +44,7 @@ export const freshGcalAuth = (parent: RootDataLoader) => {
await upsertTeamMemberIntegrationAuth(newGcalAuth)
return newGcalAuth
}
return gcalAuth as IGetTeamMemberIntegrationAuthQueryResult
return gcalAuth
})
)
const vals = results.map((result) => (result.status === 'fulfilled' ? result.value : null))
Expand Down
10 changes: 3 additions & 7 deletions packages/server/dataloader/gitlabLoaders.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import DataLoader from 'dataloader'
import GitLabOAuth2Manager from '../integrations/gitlab/GitLabOAuth2Manager'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import upsertTeamMemberIntegrationAuth from '../postgres/queries/upsertTeamMemberIntegrationAuth'
import {TeamMemberIntegrationAuth} from '../postgres/types'
import sendToSentry from '../utils/sendToSentry'
import RootDataLoader from './RootDataLoader'

export const freshGitlabAuth = (parent: RootDataLoader) => {
return new DataLoader<
{teamId: string; userId: string},
IGetTeamMemberIntegrationAuthQueryResult | null,
string
>(
return new DataLoader<{teamId: string; userId: string}, TeamMemberIntegrationAuth | null, string>(
async (keys) => {
const results = await Promise.allSettled(
keys.map(async ({teamId, userId}) => {
Expand Down Expand Up @@ -45,7 +41,7 @@ export const freshGitlabAuth = (parent: RootDataLoader) => {
await upsertTeamMemberIntegrationAuth(newGitlabAuth)
return newGitlabAuth
}
return gitlabAuth as IGetTeamMemberIntegrationAuthQueryResult
return gitlabAuth
})
)
const vals = results.map((result) => (result.status === 'fulfilled' ? result.value : null))
Expand Down
28 changes: 17 additions & 11 deletions packages/server/dataloader/integrationAuthLoaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import isValid from '../graphql/isValid'
import getKysely from '../postgres/getKysely'
import {IGetBestTeamIntegrationAuthQueryResult} from '../postgres/queries/generated/getBestTeamIntegrationAuthQuery'
import {IntegrationProviderServiceEnum} from '../postgres/queries/generated/getIntegrationProvidersByIdsQuery'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import getBestTeamIntegrationAuth from '../postgres/queries/getBestTeamIntegrationAuth'
import getIntegrationProvidersByIds, {
TIntegrationProvider
} from '../postgres/queries/getIntegrationProvidersByIds'
import getTeamMemberIntegrationAuth from '../postgres/queries/getTeamMemberIntegrationAuth'
import {selectSlackNotifications} from '../postgres/select'
import {SlackAuth, SlackNotification} from '../postgres/types'
import {TeamMemberIntegrationAuth} from '../postgres/types/pg'
import {selectSlackNotifications, selectTeamMemberIntegrationAuth} from '../postgres/select'
import {SlackAuth, SlackNotification, TeamMemberIntegrationAuth} from '../postgres/types'
import NullableDataLoader from './NullableDataLoader'
import RootDataLoader from './RootDataLoader'

Expand Down Expand Up @@ -124,17 +121,26 @@ export const bestTeamIntegrationProviders = (parent: RootDataLoader) => {
export const teamMemberIntegrationAuths = (parent: RootDataLoader) => {
return new DataLoader<
TeamMemberIntegrationAuthPrimaryKey,
IGetTeamMemberIntegrationAuthQueryResult | null,
TeamMemberIntegrationAuth | null,
string
>(
async (keys) => {
const results = await Promise.allSettled(
keys.map(async ({service, teamId, userId}) =>
getTeamMemberIntegrationAuth(service, teamId, userId)
const results = await selectTeamMemberIntegrationAuth()
.where(({eb, refTuple, tuple}) =>
eb(
refTuple('teamId', 'userId', 'service'),
'in',
keys.map((key) => tuple(key.teamId, key.userId, key.service))
)
)
.execute()
return keys.map(
(key) =>
results.find(
({teamId, userId, service}) =>
key.teamId === teamId && key.userId === userId && key.service === service
) || null
)
const vals = results.map((result) => (result.status === 'fulfilled' ? result.value : null))
return vals
},
{
...parent.dataLoaderOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/server/graphql/mutations/helpers/createAzureTask.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {DataLoaderWorker} from '../../../graphql/graphql'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../../../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import {IntegrationProviderAzureDevOps} from '../../../postgres/queries/getIntegrationProvidersByIds'
import {TeamMemberIntegrationAuth} from '../../../postgres/types'
import AzureDevOpsServerManager from '../../../utils/AzureDevOpsServerManager'

const createAzureTask = async (
rawContentStr: string,
serviceProjectHash: string,
azureAuth: IGetTeamMemberIntegrationAuthQueryResult,
azureAuth: TeamMemberIntegrationAuth,
dataLoader: DataLoaderWorker
) => {
const provider = await dataLoader.get('integrationProviders').loadNonNull(azureAuth.providerId)
Expand Down
4 changes: 2 additions & 2 deletions packages/server/graphql/mutations/helpers/createGitLabTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import {stateToMarkdown} from 'draft-js-export-markdown'
import {GraphQLResolveInfo} from 'graphql'
import splitDraftContent from 'parabol-client/utils/draftjs/splitDraftContent'
import GitLabServerManager from '../../../integrations/gitlab/GitLabServerManager'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../../../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import {TeamMemberIntegrationAuth} from '../../../postgres/types'
import {DataLoaderWorker, GQLContext} from '../../graphql'

const createGitLabTask = async (
rawContent: string,
fullPath: string,
gitlabAuth: IGetTeamMemberIntegrationAuthQueryResult,
gitlabAuth: TeamMemberIntegrationAuth,
context: GQLContext,
info: GraphQLResolveInfo,
dataLoader: DataLoaderWorker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import findStageById from 'parabol-client/utils/meetings/findStageById'
import {phaseLabelLookup} from 'parabol-client/utils/meetings/lookups'
import appOrigin from '../../../../appOrigin'
import {IntegrationProviderMattermost} from '../../../../postgres/queries/getIntegrationProvidersByIds'
import {SlackNotification, Team} from '../../../../postgres/types'
import {SlackNotification, Team, TeamMemberIntegrationAuth} from '../../../../postgres/types'
import IUser from '../../../../postgres/types/IUser'
import {AnyMeeting, MeetingTypeEnum} from '../../../../postgres/types/Meeting'
import {TeamMemberIntegrationAuth} from '../../../../postgres/types/pg'
import MattermostServerManager from '../../../../utils/MattermostServerManager'
import {analytics} from '../../../../utils/analytics/analytics'
import {toEpochSeconds} from '../../../../utils/epochTime'
Expand Down
6 changes: 3 additions & 3 deletions packages/server/integrations/gitlab/GitLabServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import getIssue from '../../graphql/nestedSchema/GitLab/queries/getIssue.graphql
import getProfile from '../../graphql/nestedSchema/GitLab/queries/getProfile.graphql'
import getProjectIssues from '../../graphql/nestedSchema/GitLab/queries/getProjectIssues.graphql'
import getProjects from '../../graphql/nestedSchema/GitLab/queries/getProjects.graphql'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import {TeamMemberIntegrationAuth} from '../../postgres/types'
import {RootSchema} from '../../types/custom'
import {
CreateIssueMutation,
Expand All @@ -25,13 +25,13 @@ import {CreateTaskResponse, TaskIntegrationManager} from '../TaskIntegrationMana

class GitLabServerManager implements TaskIntegrationManager {
public title = 'GitLab'
private readonly auth: IGetTeamMemberIntegrationAuthQueryResult
private readonly auth: TeamMemberIntegrationAuth
private readonly context: GQLContext
private readonly info: GraphQLResolveInfo
private readonly serverBaseUrl: string

constructor(
auth: IGetTeamMemberIntegrationAuthQueryResult,
auth: TeamMemberIntegrationAuth,
context: GQLContext,
info: GraphQLResolveInfo,
serverBaseUrl: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import JiraServerIssueId from '~/shared/gqlIds/JiraServerIssueId'
import {ExternalLinks} from '~/types/constEnums'
import composeJQL from '~/utils/composeJQL'
import splitDraftContent from '~/utils/draftjs/splitDraftContent'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import {IntegrationProviderJiraServer} from '../../postgres/queries/getIntegrationProvidersByIds'
import {TeamMemberIntegrationAuth} from '../../postgres/types'
import {CreateTaskResponse, TaskIntegrationManager} from '../TaskIntegrationManagerFactory'

const MAX_PAGINATION_RESULTS = 5000
Expand Down Expand Up @@ -97,16 +97,13 @@ interface JiraServerIssuesResponse {

export default class JiraServerRestManager implements TaskIntegrationManager {
public title = 'Jira Server'
private readonly auth: IGetTeamMemberIntegrationAuthQueryResult
private readonly auth: TeamMemberIntegrationAuth
private readonly provider: IntegrationProviderJiraServer
private readonly serverBaseUrl: string
private readonly oauth: OAuth
private readonly token: OAuth.Token

constructor(
auth: IGetTeamMemberIntegrationAuthQueryResult,
provider: IntegrationProviderJiraServer
) {
constructor(auth: TeamMemberIntegrationAuth, provider: IntegrationProviderJiraServer) {
this.auth = auth
this.provider = provider

Expand Down
18 changes: 0 additions & 18 deletions packages/server/postgres/queries/getTeamMemberIntegrationAuth.ts

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions packages/server/postgres/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export const selectTimelineEvent = () => {
>()
}

export const selectTeamMemberIntegrationAuth = () => {
return getKysely().selectFrom('TeamMemberIntegrationAuth').selectAll()
}
export const selectTemplateScaleRef = () => {
return getKysely()
.selectFrom([
Expand Down
4 changes: 3 additions & 1 deletion packages/server/postgres/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
selectSuggestedAction,
selectTasks,
selectTeamInvitations,
selectTeamMemberIntegrationAuth,
selectTeamPromptResponses,
selectTeams,
selectTemplateScale,
Expand Down Expand Up @@ -58,7 +59,8 @@ export type SuggestedAction = ExtractTypeFromQueryBuilderSelect<typeof selectSug
export interface Team extends ExtractTypeFromQueryBuilderSelect<typeof selectTeams> {}

export type TeamMember = Selectable<TeamMemberPG>

export interface TeamMemberIntegrationAuth
extends ExtractTypeFromQueryBuilderSelect<typeof selectTeamMemberIntegrationAuth> {}
export type TeamPromptResponse = ExtractTypeFromQueryBuilderSelect<typeof selectTeamPromptResponses>
export type TemplateScale = ExtractTypeFromQueryBuilderSelect<typeof selectTemplateScale>

Expand Down
6 changes: 3 additions & 3 deletions packages/server/utils/AzureDevOpsServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
TaskIntegrationManager
} from '../integrations/TaskIntegrationManagerFactory'
import {authorizeOAuth2} from '../integrations/helpers/authorizeOAuth2'
import {IGetTeamMemberIntegrationAuthQueryResult} from '../postgres/queries/generated/getTeamMemberIntegrationAuthQuery'
import {IntegrationProviderAzureDevOps} from '../postgres/queries/getIntegrationProvidersByIds'
import {TeamMemberIntegrationAuth} from '../postgres/types'
import makeCreateAzureTaskComment from './makeCreateAzureTaskComment'

export interface AzureDevOpsUser {
Expand Down Expand Up @@ -254,7 +254,7 @@ class AzureDevOpsServerManager implements TaskIntegrationManager {
Accept: 'application/json' as const,
'Content-Type': 'application/json'
}
private readonly auth: IGetTeamMemberIntegrationAuthQueryResult | null
private readonly auth: TeamMemberIntegrationAuth | null

async init(code: string, codeVerifier: string | null) {
if (!codeVerifier) {
Expand All @@ -273,7 +273,7 @@ class AzureDevOpsServerManager implements TaskIntegrationManager {
private readonly provider: IntegrationProviderAzureDevOps | undefined

constructor(
auth: IGetTeamMemberIntegrationAuthQueryResult | null,
auth: TeamMemberIntegrationAuth | null,
provider: IntegrationProviderAzureDevOps | null
) {
if (!!auth && !!auth.accessToken) {
Expand Down

0 comments on commit 88fec61

Please sign in to comment.