Skip to content

Commit

Permalink
fix: schema key Santization, remove labels, fix telemetry (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb authored Jan 31, 2023
1 parent cd5a9c7 commit d967f8c
Show file tree
Hide file tree
Showing 10 changed files with 1,564 additions and 1,835 deletions.
3,169 changes: 1,438 additions & 1,731 deletions src/__tests__/__data__/plan-v1.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/__tests__/__helpers__/oclif-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { stdout, stderr } from "stdout-stderr";
export const run = async (argv: string[]) => {
oclif.settings.debug = true;
oclif.settings.tsnodeEnabled = true;
process.env.CI = "true";
stdout.start();
stderr.start();
const onError = (reason: any) => {
Expand Down
176 changes: 88 additions & 88 deletions src/__tests__/commands/__snapshots__/build.test.ts.snap

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/__tests__/commands/__snapshots__/production.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ function withTypewriterContext(message: Options = {}): Options {
...(message.context || {}),
typewriter: {
language: 'typescript',
version: '8.0.8',
version: '8.0.10',
},
},
};
Expand Down Expand Up @@ -2317,7 +2317,7 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
...(message.context || {}),
typewriter: {
language: 'typescript',
version: '8.0.8',
version: '8.0.10',
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/commands/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("build", () => {
outputPath,
legacyId
);
await run(["build", "-c", testPath]);
const { stdout } = await run(["build", "-c", testPath]);
expect(
fs.readFileSync(path.join(testPath, filename), {
encoding: "utf-8",
Expand Down
1 change: 1 addition & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export namespace SegmentAPI {
key: string;
type: RuleType;
description?: string;
labels?: Record<string, string>;
jsonSchema: SomeJSONSchema;
version: number;
};
Expand Down
22 changes: 19 additions & 3 deletions src/api/trackingplans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@ const getChildrenOfProp = (
return [];
};

const fixRemoveLabels = (
plan: SegmentAPI.RuleMetadata
): SegmentAPI.RuleMetadata => {
delete plan.labels;
delete plan.jsonSchema.labels;
return plan;
};

const formatSchemaId = (id: string): string => {
return encodeURIComponent(id.replace(/\s/g, "_"));
};

/**
* Fixes the id -> $id issue of the API JSONSchema objects as AJV will mark them as non-compliant to the Schema Draft7+
*/
Expand All @@ -279,8 +291,10 @@ const fixJSONSchemaIds = (
}

// The first level is missing the .id, uses .key instead so we set it here so that the rest can be executed as normal
plan.jsonSchema.$id = plan.key;
plan.jsonSchema.id = plan.key;
const validKey = formatSchemaId(plan.key);
plan.jsonSchema.$id = validKey;
plan.jsonSchema.id = validKey;
debug(`Setting Plan: ${plan.key} to ID: ${plan.jsonSchema.id}`);

const toFix = [plan.jsonSchema];

Expand Down Expand Up @@ -332,7 +346,9 @@ export function sanitizeTrackingPlan(
// The Tracking Plan returned by PAPI wraps the event in a context and other properties object, we unwrap it here as we only care about the inner type
.map(fixProperties)
// Fix the id -> $id problem with the JSON Schema returned by the API
.map(fixJSONSchemaIds),
.map(fixJSONSchemaIds)
// Remove the labels dictionary
.map(fixRemoveLabels),
};

return sortKeys(trackingPlan, { deep: true });
Expand Down
16 changes: 9 additions & 7 deletions src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,15 @@ export abstract class BaseCommand extends Command {
err: Error & { exitCode?: number | undefined }
): Promise<any> {
this.segmentClient.commandError({
command: this.id,
errorMessage: `Error: ${err.message}\n${err.stack}`,
error: err,
rawCommand: this.argv.join(" "),
errorCode: err.exitCode,
isCI: `${this.isCI}`,
} as CommandError);
properties: {
command: this.id,
errorMessage: `Error: ${err.message}\n${err.stack}`,
error: err,
rawCommand: this.argv.join(" "),
errorCode: err.exitCode,
isCI: `${this.isCI}`,
} as CommandError,
});
// We do a flush here manually cause oclif doesn't run the postrun hook for errors
try {
await this.segmentClient.flush();
Expand Down
6 changes: 4 additions & 2 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export default class HelpCommand extends BaseCommand {
await help.showHelp(argv);

this.segmentClient.helpCommand({
rawCommand: this.rawCommand,
} as CommandHelp);
properties: {
rawCommand: this.rawCommand,
} as CommandHelp,
});
}
}
2 changes: 1 addition & 1 deletion src/telemetry/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ function withTypewriterContext<P, T extends TrackMessage<P>>(
...(message.context || {}),
typewriter: {
language: 'typescript',
version: '8.0.9',
version: '8.0.10',
},
},
}
Expand Down

0 comments on commit d967f8c

Please sign in to comment.