From b4da2bf2c447fd12436c2537bf75d34daa91aad4 Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Wed, 22 Feb 2023 10:09:11 +1300 Subject: [PATCH] First pass at new custom task creation mutations --- services/api/src/resolvers.js | 6 +++ .../task/task_definition_resolvers.ts | 43 +++++++++++++++++++ services/api/src/typeDefs.js | 32 ++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/services/api/src/resolvers.js b/services/api/src/resolvers.js index d1f6be2a89..91db255f13 100644 --- a/services/api/src/resolvers.js +++ b/services/api/src/resolvers.js @@ -81,6 +81,7 @@ const { const { addAdvancedTaskDefinition, + customTaskDefinitionResolver, updateAdvancedTaskDefinition, advancedTaskDefinitionById, resolveTasksForEnvironment, @@ -491,6 +492,10 @@ const resolvers = { deployTargetConfigsByDeployTarget: getDeployTargetConfigsByDeployTarget, getEnvVariablesByProjectEnvironmentName, }, + CustomTaskMutations: { + createImage: customTaskDefinitionResolver.image, + createCommand: customTaskDefinitionResolver.command, + }, Mutation: { addProblem, addProblemHarborScanMatch, @@ -573,6 +578,7 @@ const resolvers = { deleteEnvVariableByName, addTask, addAdvancedTaskDefinition, + customTasks: () => ({}), updateAdvancedTaskDefinition, deleteAdvancedTaskDefinition, invokeRegisteredTask, diff --git a/services/api/src/resources/task/task_definition_resolvers.ts b/services/api/src/resources/task/task_definition_resolvers.ts index e25dcf5bc6..acc9d413a4 100644 --- a/services/api/src/resources/task/task_definition_resolvers.ts +++ b/services/api/src/resources/task/task_definition_resolvers.ts @@ -213,6 +213,49 @@ export const advancedTaskDefinitionArgumentById = async ( return R.prop(0, rows); }; + +/** + * + */ +export const customTaskDefinitionResolver = { + image: async (parent, args, context, info) => { + // console.log(parent); + const { input } = args; + const { sqlClientPool, hasPermission, models, userActivityLogger } = context; + let target = await customTaskDefinitionResolver.resolveTarget(sqlClientPool, input.target.projectName, input.target.environmentName || null); + let customTask = { ... input, ... target, ...{advancedTaskDefinitionArguments: input.arguments, type: "IMAGE"}}; + let ret = await addAdvancedTaskDefinition(parent, {input:customTask}, context); + return ret; + }, + command: async (parent, args, context, info) => { + const { input } = args; + const { sqlClientPool, hasPermission, models, userActivityLogger } = context; + let target = await customTaskDefinitionResolver.resolveTarget(sqlClientPool, input.target.projectName, input.target.environmentName || null); + let customTask = { ... input, ... target, ...{advancedTaskDefinitionArguments: input.arguments, type: "COMMAND"}}; + let ret = await addAdvancedTaskDefinition(parent, {input:customTask}, context); + return ret; + }, + resolveTarget: async(sqlClientPool, projectName, environmentName = null) => { + let pid = await projectHelpers(sqlClientPool).getProjectIdByName(projectName); + if(pid == null) { + throw Error(`Unable to find project "${projectName}"`); + } + + if(environmentName == null) { + // we're dealing with a project only, so we set up the target to return the projectID in the required form + return {project: pid}; + } + + // find the environment id + let environments = await environmentHelpers(sqlClientPool).getEnvironmentsByEnvironmentInput({name: environmentName, project: { id: pid }}); + let environment = environments[0]; + if(environment == null) { + throw Error(`Unable to find environment "${environmentName}" for project "${projectName}"`); + } + return {environment: environment.id}; + } +}; + export const addAdvancedTaskDefinition = async ( root, { diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index 1b7f3d88bc..abfb28634e 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -1050,6 +1050,37 @@ const typeDefs = gql` project: String! } + input CustomTaskTargetInput { + projectName: String! + environmentName: String + } + + input CreateImageInput { + name: String! + description: String! + image: String! + permission: TaskPermission! + target: CustomTaskTargetInput! + advancedTaskDefinitionArguments: [AdvancedTaskDefinitionArgumentInput] + confirmationText: String + } + + input CreateCommandInput { + name: String! + description: String! + service: String! + command: String! + permission: TaskPermission! + target: CustomTaskTargetInput! + advancedTaskDefinitionArguments: [AdvancedTaskDefinitionArgumentInput] + confirmationText: String + } + + type CustomTaskMutations { + createImage(input: CreateImageInput!): AdvancedTaskDefinitionImage + createCommand(input: CreateCommandInput!): AdvancedTaskDefinitionCommand + } + type Query { """ Returns the current user @@ -1916,6 +1947,7 @@ const typeDefs = gql` } type Mutation { + customTasks: CustomTaskMutations! """ Add Environment or update if it is already existing """