Skip to content

Commit

Permalink
fix: Avoid refetching project template for ADO (#10077)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dschoordsch authored Aug 8, 2024
1 parent fe6310c commit 510c56e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
75 changes: 61 additions & 14 deletions packages/server/dataloader/azureDevOpsLoaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ export const azureDevOpsAllWorkItems = (
workItems.map(async (returnedWorkItem): Promise<AzureDevOpsWorkItem> => {
const instanceId = getInstanceId(new URL(returnedWorkItem.url))
const mappedWorkItem = await getMappedAzureDevOpsWorkItem(
manager,
userId,
teamId,
instanceId,
returnedWorkItem
returnedWorkItem,
parent
)
return mappedWorkItem
})
Expand Down Expand Up @@ -483,11 +483,11 @@ export const azureDevOpsUserStory = (
} else {
const returnedWorkItem: WorkItem = workItems[0]
const azureDevOpsWorkItem = await getMappedAzureDevOpsWorkItem(
manager,
userId,
teamId,
instanceId,
returnedWorkItem
returnedWorkItem,
parent
)
return azureDevOpsWorkItem
}
Expand Down Expand Up @@ -530,11 +530,11 @@ export const azureDevOpsWorkItem = (
if (returnedWorkItems.length !== 1 || !returnedWorkItems[0]) return null
const returnedWorkItem = returnedWorkItems[0]
const azureDevOpsWorkItem = await getMappedAzureDevOpsWorkItem(
manager,
userId,
teamId,
instanceId,
returnedWorkItem
returnedWorkItem,
parent
)

// update our records
Expand Down Expand Up @@ -580,12 +580,57 @@ export const azureDevOpsWorkItem = (
)
}

export const getMappedAzureDevOpsWorkItem = async (
manager: AzureDevOpsServerManager,
export type AzureDevOpsProjectProcessTemplateKey = {
userId: string
teamId: string
instanceId: string
projectId: string
}
export type AzureDevOpsProjectProcessTemplate = {
error?: Error
projectTemplate?: string
}

export const azureDevOpsProjectProcessTemplate = (parent: RootDataLoader) => {
return new DataLoader<
AzureDevOpsProjectProcessTemplateKey,
AzureDevOpsProjectProcessTemplate,
string
>(
async (keys) => {
const results = await Promise.allSettled(
keys.map(async ({userId, teamId, instanceId, projectId}) => {
const auth = await parent.get('freshAzureDevOpsAuth').load({teamId, userId})
if (!auth) return null
const provider = await parent.get('integrationProviders').loadNonNull(auth.providerId)
const manager = new AzureDevOpsServerManager(
auth,
provider as IntegrationProviderAzureDevOps
)

return manager.getProjectProcessTemplate(instanceId, projectId)
})
)
return results.map((result) =>
result.status === 'fulfilled' && result.value
? result.value
: {error: new Error('Failed to get project process template')}
)
},
{
...parent.dataLoaderOptions,
cacheKeyFn: ({userId, teamId, instanceId, projectId}) =>
`${userId}:${teamId}:${instanceId}:${projectId}`
}
)
}

const getMappedAzureDevOpsWorkItem = async (
userId: string,
teamId: string,
instanceId: string,
returnedWorkItem: WorkItem
returnedWorkItem: WorkItem,
dataLoader: RootDataLoader
) => {
const mappedUrl = returnedWorkItem._links['html']?.href ?? returnedWorkItem.url
const azureDevOpsWorkItem = {
Expand All @@ -603,10 +648,12 @@ export const getMappedAzureDevOpsWorkItem = async (
userId
} as AzureDevOpsWorkItem

const projectResult = await manager.getProjectProcessTemplate(
const projectResult = await dataLoader.get('azureDevOpsProjectProcessTemplate').load({
userId,
teamId,
instanceId,
azureDevOpsWorkItem.teamProject
)
projectId: azureDevOpsWorkItem.teamProject
})
const {error: projectResultError, projectTemplate} = projectResult
if (!!projectResultError) {
const workItemId = returnedWorkItem.id.toString()
Expand Down Expand Up @@ -647,11 +694,11 @@ export const azureDevOpsWorkItems = (
const mappedWorkItems: AzureDevOpsWorkItem[] = await Promise.all(
returnedWorkItems.map(async (returnedWorkItem): Promise<AzureDevOpsWorkItem> => {
const mappedWorkItem = await getMappedAzureDevOpsWorkItem(
manager,
userId,
teamId,
instanceId,
returnedWorkItem
returnedWorkItem,
parent
)
return mappedWorkItem
})
Expand Down
2 changes: 1 addition & 1 deletion packages/server/utils/AzureDevOpsServerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ class AzureDevOpsServerManager implements TaskIntegrationManager {
return {error: undefined, projects: teamProjectReferences}
}

async getProjectProperties(instanceId: string, projectId: string) {
private async getProjectProperties(instanceId: string, projectId: string) {
let firstError: Error | undefined
const uri = `https://${instanceId}/_apis/projects/${projectId}/properties?keys=System.CurrentProcessTemplateId`
const result = await this.get<ProjectProperties>(uri)
Expand Down

0 comments on commit 510c56e

Please sign in to comment.