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

Reference implementation for proposed changes to create custom tasks mutation #3401

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const {

const {
addAdvancedTaskDefinition,
customTaskDefinitionResolver,
updateAdvancedTaskDefinition,
advancedTaskDefinitionById,
resolveTasksForEnvironment,
Expand Down Expand Up @@ -491,6 +492,10 @@ const resolvers = {
deployTargetConfigsByDeployTarget: getDeployTargetConfigsByDeployTarget,
getEnvVariablesByProjectEnvironmentName,
},
CustomTaskMutations: {
createImage: customTaskDefinitionResolver.image,
createCommand: customTaskDefinitionResolver.command,
},
Mutation: {
addProblem,
addProblemHarborScanMatch,
Expand Down Expand Up @@ -573,6 +578,7 @@ const resolvers = {
deleteEnvVariableByName,
addTask,
addAdvancedTaskDefinition,
customTasks: () => ({}),
updateAdvancedTaskDefinition,
deleteAdvancedTaskDefinition,
invokeRegisteredTask,
Expand Down
43 changes: 43 additions & 0 deletions services/api/src/resources/task/task_definition_resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand Down
32 changes: 32 additions & 0 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1916,6 +1947,7 @@ const typeDefs = gql`
}

type Mutation {
customTasks: CustomTaskMutations!
"""
Add Environment or update if it is already existing
"""
Expand Down