Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(server): core IoC 5 - getBranchLatestCommitsFactory #3137

Merged
merged 1 commit into from
Sep 26, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ReplyCreateInput
} from '@/modules/core/graph/generated/graphql'
import {
getBranchLatestCommits,
getBranchLatestCommitsFactory,
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
import {
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function addCommentCreatedActivity(params: {
const getViewerResourceItemsUngrouped = getViewerResourceItemsUngroupedFactory({
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getBranchLatestCommits: getBranchLatestCommitsFactory({ db }),
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
Expand Down
6 changes: 4 additions & 2 deletions packages/server/modules/automate/graph/resolvers/automate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { Automate, Roles, isNullOrUndefined, isNonNullable } from '@speckle/shar
import { getFeatureFlags, getServerOrigin } from '@/modules/shared/helpers/envHelper'
import {
getBranchesByIdsFactory,
getBranchLatestCommits
getBranchLatestCommitsFactory
} from '@/modules/core/repositories/branches'
import {
createTestAutomationRunFactory,
Expand Down Expand Up @@ -134,6 +134,7 @@ const getAutomationRunsItems = getAutomationRunsItemsFactory({ db })

const getProjectAutomationsItems = getProjectAutomationsItemsFactory({ db })
const getProjectAutomationsTotalCount = getProjectAutomationsTotalCountFactory({ db })
const getBranchLatestCommits = getBranchLatestCommitsFactory({ db })

export = (FF_AUTOMATE_MODULE_ENABLED
? {
Expand Down Expand Up @@ -555,7 +556,8 @@ export = (FF_AUTOMATE_MODULE_ENABLED
automateRunsEmitter: AutomateRunsEmitter.emit,
getAutomationToken,
upsertAutomationRun,
getFullAutomationRevisionMetadata
getFullAutomationRevisionMetadata,
getBranchLatestCommits
}),
validateStreamAccess
})
Expand Down
4 changes: 3 additions & 1 deletion packages/server/modules/automate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { AutomationsEmitter } from '@/modules/automate/events/automations'
import { publish } from '@/modules/shared/utils/subscriptions'
import { AutomateRunsEmitter } from '@/modules/automate/events/runs'
import { createAppToken } from '@/modules/core/services/tokens'
import { getBranchLatestCommitsFactory } from '@/modules/core/repositories/branches'

const { FF_AUTOMATE_MODULE_ENABLED } = getFeatureFlags()
let quitListeners: Optional<() => void> = undefined
Expand Down Expand Up @@ -84,7 +85,8 @@ const initializeEventListeners = () => {
automateRunsEmitter: AutomateRunsEmitter.emit,
getAutomationToken: getAutomationTokenFactory({ db }),
upsertAutomationRun: upsertAutomationRunFactory({ db }),
getFullAutomationRevisionMetadata
getFullAutomationRevisionMetadata,
getBranchLatestCommits: getBranchLatestCommitsFactory({ db })
})
const setupStatusUpdateSubscriptionsInvoke = setupStatusUpdateSubscriptionsFactory({
getAutomationRunFullTriggers,
Expand Down
104 changes: 56 additions & 48 deletions packages/server/modules/automate/services/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
LiveAutomation,
RunTriggerSource
} from '@/modules/automate/helpers/types'
import { getBranchLatestCommits } from '@/modules/core/repositories/branches'
import { getCommit } from '@/modules/core/repositories/commits'
import { createAppToken } from '@/modules/core/services/tokens'
import { Roles, Scopes } from '@speckle/shared'
Expand Down Expand Up @@ -48,6 +47,7 @@ import {
TriggerAutomationRevisionRun,
UpsertAutomationRun
} from '@/modules/automate/domain/operations'
import { GetBranchLatestCommits } from '@/modules/core/domain/branches/operations'

export type OnModelVersionCreateDeps = {
getAutomation: GetAutomation
Expand Down Expand Up @@ -216,7 +216,8 @@ export type TriggerAutomationRevisionRunDeps = {
upsertAutomationRun: UpsertAutomationRun
automateRunsEmitter: AutomateRunsEventsEmitter
getFullAutomationRevisionMetadata: GetFullAutomationRevisionMetadata
} & CreateAutomationRunDataDeps
} & CreateAutomationRunDataDeps &
ComposeTriggerDataDeps

/**
* This triggers a run for a specific automation revision
Expand Down Expand Up @@ -254,7 +255,7 @@ export const triggerAutomationRevisionRunFactory =
manifest
})

const triggerManifests = await composeTriggerData({
const triggerManifests = await composeTriggerDataFactory(deps)({
manifest,
projectId: automationWithRevision.projectId,
triggerDefinitions: automationWithRevision.revision.triggers
Expand Down Expand Up @@ -405,58 +406,64 @@ export const ensureRunConditionsFactory =
}
}

async function composeTriggerData(params: {
projectId: string
manifest: BaseTriggerManifest
triggerDefinitions: AutomationTriggerDefinitionRecord[]
}): Promise<BaseTriggerManifest[]> {
const { projectId, manifest, triggerDefinitions } = params

const manifests: BaseTriggerManifest[] = [{ ...manifest }]

/**
* The reason why we collect multiple triggers, even tho there's only one:
* - We want to collect the current context (all active versions of all triggers) at the time when the run is triggered,
* cause once the function already runs, there may be new versions already
*/

if (triggerDefinitions.length > 1) {
const associatedTriggers = triggerDefinitions.filter((t) => {
if (t.triggerType !== manifest.triggerType) return false
type ComposeTriggerDataDeps = {
getBranchLatestCommits: GetBranchLatestCommits
}

if (isVersionCreatedTriggerManifest(manifest)) {
return t.triggeringId === manifest.modelId
}
const composeTriggerDataFactory =
(deps: ComposeTriggerDataDeps) =>
async (params: {
projectId: string
manifest: BaseTriggerManifest
triggerDefinitions: AutomationTriggerDefinitionRecord[]
}): Promise<BaseTriggerManifest[]> => {
const { projectId, manifest, triggerDefinitions } = params

const manifests: BaseTriggerManifest[] = [{ ...manifest }]

/**
* The reason why we collect multiple triggers, even tho there's only one:
* - We want to collect the current context (all active versions of all triggers) at the time when the run is triggered,
* cause once the function already runs, there may be new versions already
*/

if (triggerDefinitions.length > 1) {
const associatedTriggers = triggerDefinitions.filter((t) => {
if (t.triggerType !== manifest.triggerType) return false

if (isVersionCreatedTriggerManifest(manifest)) {
return t.triggeringId === manifest.modelId
}

return false
})
return false
})

// Version creation triggers
if (manifest.triggerType === VersionCreationTriggerType) {
const latestVersions = await getBranchLatestCommits(
associatedTriggers.map((t) => t.triggeringId),
projectId
)
manifests.push(
...latestVersions.map(
(version): VersionCreatedTriggerManifest => ({
modelId: version.branchId,
projectId,
versionId: version.id,
triggerType: VersionCreationTriggerType
})
// Version creation triggers
if (manifest.triggerType === VersionCreationTriggerType) {
const latestVersions = await deps.getBranchLatestCommits(
associatedTriggers.map((t) => t.triggeringId),
projectId
)
)
manifests.push(
...latestVersions.map(
(version): VersionCreatedTriggerManifest => ({
modelId: version.branchId,
projectId,
versionId: version.id,
triggerType: VersionCreationTriggerType
})
)
)
}
}
}

return manifests
}
return manifests
}

export type ManuallyTriggerAutomationDeps = {
getAutomationTriggerDefinitions: GetAutomationTriggerDefinitions
getAutomation: GetAutomation
getBranchLatestCommits: typeof getBranchLatestCommits
getBranchLatestCommits: GetBranchLatestCommits
triggerFunction: TriggerAutomationRevisionRun
validateStreamAccess: typeof validateStreamAccess
}
Expand Down Expand Up @@ -534,8 +541,9 @@ export type CreateTestAutomationRunDeps = {
getFullAutomationRevisionMetadata: GetFullAutomationRevisionMetadata
upsertAutomationRun: UpsertAutomationRun
validateStreamAccess: typeof validateStreamAccess
getBranchLatestCommits: typeof getBranchLatestCommits
} & CreateAutomationRunDataDeps
getBranchLatestCommits: GetBranchLatestCommits
} & CreateAutomationRunDataDeps &
ComposeTriggerDataDeps

/**
* TODO: Reduce duplication w/ other fns in this service
Expand Down Expand Up @@ -596,7 +604,7 @@ export const createTestAutomationRunFactory =
{ limit: 1 }
)

const triggerManifests = await composeTriggerData({
const triggerManifests = await composeTriggerDataFactory(deps)({
projectId: automationRevisionRecord.projectId,
triggerDefinitions: automationRevisionRecord.revision.triggers,
manifest: <VersionCreatedTriggerManifest>{
Expand Down
15 changes: 10 additions & 5 deletions packages/server/modules/automate/tests/trigger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { beforeEachContext, truncateTables } from '@/test/hooks'
import { Automate } from '@speckle/shared'
import { getFeatureFlags } from '@/modules/shared/helpers/envHelper'
import {
getBranchLatestCommits,
getBranchLatestCommitsFactory,
getLatestStreamBranch
} from '@/modules/core/repositories/branches'
import {
Expand Down Expand Up @@ -96,6 +96,7 @@ const getFullAutomationRevisionMetadata = getFullAutomationRevisionMetadataFacto
})
const updateAutomationRevision = updateAutomationRevisionFactory({ db })
const updateAutomationRun = updateAutomationRunFactory({ db })
const getBranchLatestCommits = getBranchLatestCommitsFactory({ db })

;(FF_AUTOMATE_MODULE_ENABLED ? describe : describe.skip)(
'Automate triggers @automate',
Expand Down Expand Up @@ -320,7 +321,8 @@ const updateAutomationRun = updateAutomationRunFactory({ db })
automateRunsEmitter: AutomateRunsEmitter.emit,
getAutomationToken,
upsertAutomationRun,
getFullAutomationRevisionMetadata
getFullAutomationRevisionMetadata,
getBranchLatestCommits
})({
revisionId: cryptoRandomString({ length: 10 }),
manifest: <VersionCreatedTriggerManifest>{
Expand Down Expand Up @@ -412,7 +414,8 @@ const updateAutomationRun = updateAutomationRunFactory({ db })
automateRunsEmitter: AutomateRunsEmitter.emit,
getAutomationToken,
upsertAutomationRun,
getFullAutomationRevisionMetadata
getFullAutomationRevisionMetadata,
getBranchLatestCommits
})({
revisionId: automationRevisionId,
manifest: <VersionCreatedTriggerManifest>{
Expand Down Expand Up @@ -510,7 +513,8 @@ const updateAutomationRun = updateAutomationRunFactory({ db })
automateRunsEmitter: AutomateRunsEmitter.emit,
getAutomationToken,
upsertAutomationRun,
getFullAutomationRevisionMetadata
getFullAutomationRevisionMetadata,
getBranchLatestCommits
})({
revisionId: automationRevisionId,
manifest: <VersionCreatedTriggerManifest>{
Expand Down Expand Up @@ -992,7 +996,8 @@ const updateAutomationRun = updateAutomationRunFactory({ db })
automateRunsEmitter: AutomateRunsEmitter.emit,
getAutomationToken,
upsertAutomationRun,
getFullAutomationRevisionMetadata
getFullAutomationRevisionMetadata,
getBranchLatestCommits
}),
validateStreamAccess,
...(overrides || {})
Expand Down
3 changes: 2 additions & 1 deletion packages/server/modules/cli/commands/download/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { downloadCommitFactory } from '@/modules/cross-server-sync/services/comm
import { cliLogger } from '@/logging/logging'
import { getStream, getStreamCollaborators } from '@/modules/core/repositories/streams'
import {
getBranchLatestCommits,
getBranchLatestCommitsFactory,
getStreamBranchByNameFactory,
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
Expand Down Expand Up @@ -82,6 +82,7 @@ const command: CommandModule<
const validateInputAttachments = validateInputAttachmentsFactory({
getBlobs: getBlobsFactory({ db })
})
const getBranchLatestCommits = getBranchLatestCommitsFactory({ db })
const insertComments = insertCommentsFactory({ db })
const insertCommentLinks = insertCommentLinksFactory({ db })
const getViewerResourceItemsUngrouped = getViewerResourceItemsUngroupedFactory({
Expand Down
4 changes: 2 additions & 2 deletions packages/server/modules/cli/commands/download/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { downloadProjectFactory } from '@/modules/cross-server-sync/services/pro
import { downloadCommitFactory } from '@/modules/cross-server-sync/services/commit'
import { getStream, getStreamCollaborators } from '@/modules/core/repositories/streams'
import {
getBranchLatestCommits,
getBranchLatestCommitsFactory,
getStreamBranchByNameFactory,
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'
Expand Down Expand Up @@ -83,7 +83,7 @@ const command: CommandModule<
const getViewerResourceItemsUngrouped = getViewerResourceItemsUngroupedFactory({
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getBranchLatestCommits: getBranchLatestCommitsFactory({ db }),
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
Expand Down
6 changes: 1 addition & 5 deletions packages/server/modules/comments/domain/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import {
CommentRecord,
CommentViewRecord
} from '@/modules/comments/helpers/types'
import { BranchLatestCommit } from '@/modules/core/domain/commits/types'
import {
CreateCommentInput,
CreateCommentReplyInput,
EditCommentInput,
LegacyCommentViewerData,
ViewerUpdateTrackingTarget
} from '@/modules/core/graph/generated/graphql'
import { CommitRecord } from '@/modules/core/helpers/types'
import { SmartTextEditorValueSchema } from '@/modules/core/services/richTextEditorService'
import { MarkNullableOptional, Optional } from '@/modules/shared/helpers/typeHelper'
import { MaybeNullOrUndefined, SpeckleViewer } from '@speckle/shared'
Expand Down Expand Up @@ -93,10 +93,6 @@ export type GetPaginatedBranchCommentsTotalCount = (
params: Omit<PaginatedBranchCommentsParams, 'limit' | 'cursor'>
) => Promise<number>

type BranchLatestCommit = CommitRecord & {
branchId: string
}

export type GetPaginatedProjectCommentsPage = (
params: PaginatedProjectCommentsParams,
options?: {
Expand Down
6 changes: 3 additions & 3 deletions packages/server/modules/comments/graph/resolvers/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ import {
import { getStreamObjects } from '@/modules/core/repositories/objects'
import { adminOverrideEnabled } from '@/modules/shared/helpers/envHelper'
import {
getBranchLatestCommits,
getBranchLatestCommitsFactory,
getStreamBranchesByNameFactory
} from '@/modules/core/repositories/branches'

Expand Down Expand Up @@ -176,7 +176,7 @@ const authorizeCommentAccess = authorizeCommentAccessFactory({
const getViewerResourceItemsUngrouped = getViewerResourceItemsUngroupedFactory({
getViewerResourceGroups: getViewerResourceGroupsFactory({
getStreamObjects,
getBranchLatestCommits,
getBranchLatestCommits: getBranchLatestCommitsFactory({ db }),
getStreamBranchesByName: getStreamBranchesByNameFactory({ db }),
getSpecificBranchCommits,
getAllBranchCommits
Expand Down Expand Up @@ -226,7 +226,7 @@ const getPaginatedBranchComments = getPaginatedBranchCommentsFactory({
})
const getPaginatedProjectComments = getPaginatedProjectCommentsFactory({
resolvePaginatedProjectCommentsLatestModelResources:
resolvePaginatedProjectCommentsLatestModelResourcesFactory(),
resolvePaginatedProjectCommentsLatestModelResourcesFactory({ db }),
getPaginatedProjectCommentsPage: getPaginatedProjectCommentsPageFactory({ db }),
getPaginatedProjectCommentsTotalCount: getPaginatedProjectCommentsTotalCountFactory({
db
Expand Down
Loading