Skip to content

Commit

Permalink
refactor: use union type for listing policies
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Oct 18, 2024
1 parent 6b3dce4 commit 8949ac1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 94 deletions.
37 changes: 23 additions & 14 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,9 @@ const {
updateHistoryRetentionPolicy,
deleteHarborRetentionPolicy,
deleteHistoryRetentionPolicy,
getHarborRetentionPoliciesByProjectId,
getHarborRetentionPoliciesByOrganizationId,
getHistoryRetentionPoliciesByProjectId,
getHistoryRetentionPoliciesByOrganizationId,
listHarborRetentionPolicies,
listHistoryRetentionPolicies,
getRetentionPoliciesByProjectId,
getRetentionPoliciesByOrganizationId,
listAllRetentionPolicies,
addHarborRetentionPolicyLink,
addHistoryRetentionPolicyLink,
removeHarborRetentionPolicyLink,
Expand Down Expand Up @@ -405,6 +402,10 @@ const resolvers = {
DAYS: 'days',
MONTHS: 'months',
},
RetentionPolicyType: {
HARBOR: 'harbor',
HISTORY: 'history',
},
Openshift: {
projectUser: getProjectUser,
token: getToken,
Expand All @@ -427,8 +428,7 @@ const resolvers = {
groups: getGroupsByProjectId,
privateKey: getPrivateKey,
publicKey: getProjectDeployKey,
harborRetentionPolicies: getHarborRetentionPoliciesByProjectId,
historyRetentionPolicies: getHistoryRetentionPoliciesByProjectId,
retentionPolicies: getRetentionPoliciesByProjectId,
},
GroupInterface: {
__resolveType(group) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -617,8 +627,7 @@ const resolvers = {
getProjectGroupOrganizationAssociation,
getEnvVariablesByProjectEnvironmentName,
checkBulkImportProjectsAndGroupsToOrganization,
listHarborRetentionPolicies,
listHistoryRetentionPolicies
listAllRetentionPolicies
},
Mutation: {
addProblem,
Expand Down
70 changes: 14 additions & 56 deletions services/api/src/resources/retentionpolicy/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,34 +217,25 @@ 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);
}

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) => {
Expand Down Expand Up @@ -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 }
Expand All @@ -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 }
Expand All @@ -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;
};
40 changes: 16 additions & 24 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
"""
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
}
"""
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
"""
Expand Down

0 comments on commit 8949ac1

Please sign in to comment.