diff --git a/services/api/src/resolvers.js b/services/api/src/resolvers.js index 6cf6bbf864..2e1c4c2e8b 100644 --- a/services/api/src/resolvers.js +++ b/services/api/src/resolvers.js @@ -282,12 +282,9 @@ const { updateHistoryRetentionPolicy, deleteHarborRetentionPolicy, deleteHistoryRetentionPolicy, - getHarborRetentionPoliciesByProjectId, - getHarborRetentionPoliciesByOrganizationId, - getHistoryRetentionPoliciesByProjectId, - getHistoryRetentionPoliciesByOrganizationId, - listHarborRetentionPolicies, - listHistoryRetentionPolicies, + getRetentionPoliciesByProjectId, + getRetentionPoliciesByOrganizationId, + listAllRetentionPolicies, addHarborRetentionPolicyLink, addHistoryRetentionPolicyLink, removeHarborRetentionPolicyLink, @@ -405,6 +402,10 @@ const resolvers = { DAYS: 'days', MONTHS: 'months', }, + RetentionPolicyType: { + HARBOR: 'harbor', + HISTORY: 'history', + }, Openshift: { projectUser: getProjectUser, token: getToken, @@ -427,8 +428,7 @@ const resolvers = { groups: getGroupsByProjectId, privateKey: getPrivateKey, publicKey: getProjectDeployKey, - harborRetentionPolicies: getHarborRetentionPoliciesByProjectId, - historyRetentionPolicies: getHistoryRetentionPoliciesByProjectId, + retentionPolicies: getRetentionPoliciesByProjectId, }, GroupInterface: { __resolveType(group) { @@ -480,15 +480,13 @@ const resolvers = { owners: getOwnersByOrganizationId, deployTargets: getDeployTargetsByOrganizationId, notifications: getNotificationsByOrganizationId, - harborRetentionPolicies: getHarborRetentionPoliciesByOrganizationId, - historyRetentionPolicies: getHistoryRetentionPoliciesByOrganizationId + retentionPolicies: getRetentionPoliciesByOrganizationId, }, OrgProject: { groups: getGroupsByOrganizationsProject, groupCount: getGroupCountByOrganizationProject, notifications: getNotificationsForOrganizationProjectId, - harborRetentionPolicies: getHarborRetentionPoliciesByProjectId, - historyRetentionPolicies: getHistoryRetentionPoliciesByProjectId, + retentionPolicies: getRetentionPoliciesByProjectId, }, OrgEnvironment: { project: getProjectById, @@ -536,6 +534,18 @@ const resolvers = { } } }, + RetentionPolicy: { + __resolveType(obj) { + switch (obj.type) { + case 'harbor': + return 'HarborRetentionPolicy'; + case 'history': + return 'HistoryRetentionPolicy'; + default: + return null; + } + } + }, AdvancedTaskDefinition: { __resolveType (obj) { switch(obj.type) { @@ -617,8 +627,7 @@ const resolvers = { getProjectGroupOrganizationAssociation, getEnvVariablesByProjectEnvironmentName, checkBulkImportProjectsAndGroupsToOrganization, - listHarborRetentionPolicies, - listHistoryRetentionPolicies + listAllRetentionPolicies }, Mutation: { addProblem, diff --git a/services/api/src/resources/retentionpolicy/resolvers.ts b/services/api/src/resources/retentionpolicy/resolvers.ts index 398aaab494..c40623959c 100644 --- a/services/api/src/resources/retentionpolicy/resolvers.ts +++ b/services/api/src/resources/retentionpolicy/resolvers.ts @@ -217,11 +217,18 @@ export const deleteHistoryRetentionPolicy: ResolverFn = async ( return await deleteRetentionPolicy(sqlClientPool, hasPermission, userActivityLogger, name, 'history'); }; -const listRetentionPolicies = async (sqlClientPool, hasPermission, name, type) => { +export const listAllRetentionPolicies: ResolverFn = async ( + root, + { name, type }, + { sqlClientPool, hasPermission } +) => { await hasPermission('retention_policy', 'viewAll'); let queryBuilder = knex('retention_policy'); - queryBuilder = queryBuilder.and.where('type', type); + + if (type) { + queryBuilder = queryBuilder.and.where('type', type); + } if (name) { queryBuilder = queryBuilder.where('name', name); @@ -229,22 +236,6 @@ const listRetentionPolicies = async (sqlClientPool, hasPermission, name, type) = const rows = await query(sqlClientPool, queryBuilder.toString()); return rows.map(row => ({ ...row, source: null, configuration: {type: row.type, ...JSON.parse(row.configuration)} })); -} - -export const listHarborRetentionPolicies: ResolverFn = async ( - root, - { name }, - { sqlClientPool, hasPermission } -) => { - return await listRetentionPolicies(sqlClientPool, hasPermission, name, 'harbor') -}; - -export const listHistoryRetentionPolicies: ResolverFn = async ( - root, - { name }, - { sqlClientPool, hasPermission } -) => { - return await listRetentionPolicies(sqlClientPool, hasPermission, name, 'history') }; const addRetentionPolicyLink = async (sqlClientPool, hasPermission, userActivityLogger, input, type) => { @@ -470,24 +461,7 @@ export const removeHistoryRetentionPolicyLink: ResolverFn = async ( return await removeRetentionPolicyLink(sqlClientPool, hasPermission, userActivityLogger, input, 'history') }; -// This is only called by the project resolver, so there is no need to do any permission checks as they're already done by the project -export const getHarborRetentionPoliciesByProjectId: ResolverFn = async ( - project, - args, - { sqlClientPool } -) => { - - let pid = args.project; - if (project) { - pid = project.id; - } - let rows = [] - rows = await Helpers(sqlClientPool).getRetentionPoliciesByScopeWithTypeAndLink('harbor', 'project', project.id); - return rows; -}; - -// This is only called by the project resolver, so there is no need to do any permission checks as they're already done by the project -export const getHistoryRetentionPoliciesByProjectId: ResolverFn = async ( +export const getRetentionPoliciesByProjectId: ResolverFn = async ( project, args, { sqlClientPool } @@ -498,12 +472,12 @@ export const getHistoryRetentionPoliciesByProjectId: ResolverFn = async ( pid = project.id; } let rows = [] - rows = await Helpers(sqlClientPool).getRetentionPoliciesByScopeWithTypeAndLink('history', 'project', project.id); + rows = await Helpers(sqlClientPool).getRetentionPoliciesByScopeWithTypeAndLink(args.type, "project", project.id); return rows; }; -// This is only called by the organization resolver, so there is no need to do any permission checks as they're already done by the organization -export const getHarborRetentionPoliciesByOrganizationId: ResolverFn = async ( +// This is only called by the organization resolver, so there is no need to do any permission checks +export const getRetentionPoliciesByOrganizationId: ResolverFn = async ( organization, args, { sqlClientPool } @@ -514,22 +488,6 @@ export const getHarborRetentionPoliciesByOrganizationId: ResolverFn = async ( oid = organization.id; } let rows = [] - rows = await Helpers(sqlClientPool).getRetentionPoliciesByScopeWithTypeAndLink('harbor', 'organization', oid); + rows = await Helpers(sqlClientPool).getRetentionPoliciesByScopeWithTypeAndLink(args.type, "organization", oid); return rows; }; - -// This is only called by the organization resolver, so there is no need to do any permission checks as they're already done by the organization -export const getHistoryRetentionPoliciesByOrganizationId: ResolverFn = async ( - organization, - args, - { sqlClientPool } -) => { - - let oid = args.organization; - if (organization) { - oid = organization.id; - } - let rows = [] - rows = await Helpers(sqlClientPool).getRetentionPoliciesByScopeWithTypeAndLink('history', 'organization', oid); - return rows; -}; \ No newline at end of file diff --git a/services/api/src/typeDefs.js b/services/api/src/typeDefs.js index 8fcab2d395..91744d64f1 100644 --- a/services/api/src/typeDefs.js +++ b/services/api/src/typeDefs.js @@ -810,15 +810,10 @@ const typeDefs = gql` buildImage: String sharedBaasBucket: Boolean """ - harborRetentionPolicies are the available harbor retention policies to a project, this will also include inherited policies from an organization + retentionPolicies are the available retention policies to a project, this will also include inherited policies from an organization if the project is associated to an organization, and the organization has any retention policies """ - harborRetentionPolicies: [HarborRetentionPolicy] - """ - historyRetentionPolicies are the available history retention policies to a project, this will also include inherited policies from an organization - if the project is associated to an organization, and the organization has any retention policies - """ - historyRetentionPolicies: [HistoryRetentionPolicy] + retentionPolicies(type: RetentionPolicyType): [RetentionPolicy] } """ @@ -1110,13 +1105,9 @@ const typeDefs = gql` notifications(type: NotificationType): [Notification] created: String """ - harborRetentionPolicies are the available harbor retention policies to an organization + retentionPolicies are the available retention policies to an organization """ - harborRetentionPolicies: [HarborRetentionPolicy] - """ - historyRetentionPolicies are the available history retention policies to an organization - """ - historyRetentionPolicies: [HistoryRetentionPolicy] + retentionPolicies(type: RetentionPolicyType): [RetentionPolicy] } input AddOrganizationInput { @@ -1162,15 +1153,10 @@ const typeDefs = gql` groupCount: Int notifications: [OrganizationNotification] """ - harborRetentionPolicies are the available harbor retention policies to a project, this will also include inherited policies from an organization + retentionPolicies are the available retention policies to a project, this will also include inherited policies from an organization if the project is associated to an organization, and the organization has any retention policies """ - harborRetentionPolicies: [HarborRetentionPolicy] - """ - historyRetentionPolicies are the available history retention policies to a project, this will also include inherited policies from an organization - if the project is associated to an organization, and the organization has any retention policies - """ - historyRetentionPolicies: [HistoryRetentionPolicy] + retentionPolicies(type: RetentionPolicyType): [RetentionPolicy] } """ @@ -1468,8 +1454,7 @@ const typeDefs = gql` getProjectGroupOrganizationAssociation(input: ProjectOrgGroupsInput!): String @deprecated(reason: "Use checkBulkImportProjectsAndGroupsToOrganization instead") getEnvVariablesByProjectEnvironmentName(input: EnvVariableByProjectEnvironmentNameInput!): [EnvKeyValue] checkBulkImportProjectsAndGroupsToOrganization(input: AddProjectToOrganizationInput!): ProjectGroupsToOrganization - listHarborRetentionPolicies(name: String): [HarborRetentionPolicy] - listHistoryRetentionPolicies(name: String): [HistoryRetentionPolicy] + listAllRetentionPolicies(name: String, type: RetentionPolicyType): [RetentionPolicy] } type ProjectGroupsToOrganization { @@ -2362,7 +2347,7 @@ const typeDefs = gql` } """ - HarborRetentionPolicy is the type for harbor retention policies + HarborRetentionPolicyConfiguration is the type for harbor retention policies configuration """ type HarborRetentionPolicyConfiguration { enabled: Boolean @@ -2382,7 +2367,7 @@ const typeDefs = gql` } """ - HistoryRetentionPolicyConfiguration is the type for history retention policies + HistoryRetentionPolicyConfiguration is the type for history retention policies configuration """ type HistoryRetentionPolicyConfiguration { enabled: Boolean @@ -2447,6 +2432,13 @@ const typeDefs = gql` source: String } + union RetentionPolicy = HarborRetentionPolicy | HistoryRetentionPolicy + + enum RetentionPolicyType { + HARBOR + HISTORY + } + """ AddHarborRetentionPolicyInput is used as the input for creating a harbor retention policy """