Skip to content

Commit

Permalink
Improved Relationship Metadata Sorting
Browse files Browse the repository at this point in the history
- Modified the sorting function for StepRelationshipMetadata.
- Now sorts accurately based on `sourceType`, `targetType`, and `_class`, ensuring proper organization and retrieval of relationship metadata.

Before: `aws_account HAS aws_waf` was coming before `aws_account HAS aws_apigateway`.
After: Entries are sorted properly, ensuring `aws_account HAS aws_apigateway` is positioned before `aws_account HAS aws_waf`, respecting the alphabetical order of `targetType`.
  • Loading branch information
i5o committed Oct 2, 2023
1 parent 642f1ec commit 1270976
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import {
StepMappedRelationshipMetadata,
StepRelationshipMetadata,
} from '@jupiterone/integration-sdk-core';
import { collectGraphObjectMetadataFromSteps } from './getSortedJupiterOneTypes';
import { randomUUID as uuid } from 'crypto';
import {
alphabetizeMetadataProperties,
collectGraphObjectMetadataFromSteps,
} from './getSortedJupiterOneTypes';

function createIntegrationStep({
entities = [],
Expand Down Expand Up @@ -214,3 +217,63 @@ describe('collectGraphObjectMetadataFromSteps', () => {
});
});
});

describe('alphabetizeMetadataProperties', () => {
it('should correctly sort an array of StepRelationshipMetadata', () => {
const { relationships } = alphabetizeMetadataProperties(
collectGraphObjectMetadataFromSteps([
createIntegrationStep({
relationships: [
{
_type: 'aws_account_has_service',
sourceType: 'aws_account',
_class: RelationshipClass.HAS,
targetType: 'aws_waf',
},
],
}),
createIntegrationStep({
relationships: [
{
_type: 'aws_account_owns_service',
sourceType: 'aws_account',
_class: RelationshipClass.OWNS,
targetType: 'aws_apigateway',
},
],
}),
createIntegrationStep({
relationships: [
{
_type: 'aws_account_has_service',
sourceType: 'aws_account',
_class: RelationshipClass.HAS,
targetType: 'aws_apigateway',
},
],
}),
]),
);

expect(relationships).toEqual([
{
_type: 'aws_account_has_service',
sourceType: 'aws_account',
_class: 'HAS',
targetType: 'aws_apigateway',
},
{
_type: 'aws_account_owns_service',
sourceType: 'aws_account',
_class: 'OWNS',
targetType: 'aws_apigateway',
},
{
_type: 'aws_account_has_service',
sourceType: 'aws_account',
_class: 'HAS',
targetType: 'aws_waf',
},
]);
});
});
23 changes: 15 additions & 8 deletions packages/integration-sdk-cli/src/utils/getSortedJupiterOneTypes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { loadConfig } from '../config';
import * as path from 'path';
import { buildStepDependencyGraph } from '@jupiterone/integration-sdk-runtime';
import {
IntegrationStepExecutionContext,
Step,
StepGraphObjectMetadataProperties,
StepEntityMetadata,
StepRelationshipMetadata,
StepGraphObjectMetadataProperties,
StepMappedRelationshipMetadata,
StepRelationshipMetadata,
} from '@jupiterone/integration-sdk-core';
import { buildStepDependencyGraph } from '@jupiterone/integration-sdk-runtime';
import * as path from 'path';
import { loadConfig } from '../config';

export interface TypesCommandArgs {
projectPath: string;
Expand Down Expand Up @@ -60,12 +60,19 @@ function alphabetizeRelationshipMetadataPropertyByTypeCompareFn(
a: StepRelationshipMetadata,
b: StepRelationshipMetadata,
): number {
if (a._type > b._type) return 1;
if (a._type < b._type) return -1;
if (a.sourceType > b.sourceType) return 1;
if (a.sourceType < b.sourceType) return -1;

if (a.targetType > b.targetType) return 1;
if (a.targetType < b.targetType) return -1;

if (a._class > b._class) return 1;
if (a._class < b._class) return -1;

return 0;
}

function alphabetizeMetadataProperties(
export function alphabetizeMetadataProperties(
metadata: StepGraphObjectMetadataProperties,
): StepGraphObjectMetadataProperties {
return {
Expand Down

0 comments on commit 1270976

Please sign in to comment.