diff options
author | 2022-06-14 15:30:39 -0500 | |
---|---|---|
committer | 2022-06-14 15:30:39 -0500 | |
commit | 5ab9d573f66beded5050cea6c48d94da33c792dd (patch) | |
tree | ecc543e0aded56e96000f4287bc3fc4730244c3f /packages/telemetry/src | |
parent | 7832c6a2607ade3d4fe0a9ee45734e1c5f35d227 (diff) | |
download | astro-5ab9d573f66beded5050cea6c48d94da33c792dd.tar.gz astro-5ab9d573f66beded5050cea6c48d94da33c792dd.tar.zst astro-5ab9d573f66beded5050cea6c48d94da33c792dd.zip |
Collect project meta info (#3587)
* chore: add project meta
* Update index.ts
Diffstat (limited to 'packages/telemetry/src')
-rw-r--r-- | packages/telemetry/src/index.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/packages/telemetry/src/index.ts b/packages/telemetry/src/index.ts index a0c3f5c43..be3ecfc21 100644 --- a/packages/telemetry/src/index.ts +++ b/packages/telemetry/src/index.ts @@ -3,6 +3,8 @@ import { createHash, randomBytes } from 'node:crypto'; import { isCI } from 'ci-info'; import debug from 'debug'; +// @ts-ignore +import gitUp from 'git-up'; import { getAnonymousMeta } from './anonymous-meta.js'; import { Config } from './config.js'; @@ -10,6 +12,7 @@ import * as KEY from './keys.js'; import { post } from './post.js'; import { getRawProjectId } from './project-id.js'; + export interface AstroTelemetryOptions { version: string; } @@ -19,6 +22,7 @@ export type TelemetryEvent = { eventName: string; payload: Record<string, any> } interface EventContext { anonymousId: string; projectId: string; + projectMetadata: any; sessionId: string; } @@ -77,6 +81,12 @@ export class AstroTelemetry { return this.getWithFallback(KEY.TELEMETRY_NOTIFY_DATE, ''); } + private hash(payload: BinaryLike): string { + const hash = createHash('sha256'); + hash.update(payload); + return hash.digest('hex'); + } + // Create a ONE-WAY hash so there is no way for Astro to decode the value later. private oneWayHash(payload: BinaryLike): string { const hash = createHash('sha256'); @@ -93,6 +103,18 @@ export class AstroTelemetry { return this.oneWayHash(this.rawProjectId); } + private get projectMetadata(): undefined | { owner: string, name: string } { + const projectId = this.rawProjectId; + if (projectId === process.cwd()) { + return; + } + const { pathname, resource } = gitUp(projectId); + const parts = pathname.split('/').slice(1); + const owner = `${resource}${parts[0]}`; + const name = parts[1].replace('.git', ''); + return { owner: this.hash(owner), name: this.hash(name) }; + } + private get isDisabled(): boolean { if (Boolean(this.ASTRO_TELEMETRY_DISABLED || this.TELEMETRY_DISABLED)) { return true; @@ -154,6 +176,7 @@ export class AstroTelemetry { const context: EventContext = { anonymousId: this.anonymousId, projectId: this.projectId, + projectMetadata: this.projectMetadata, sessionId: this.sessionId, }; const meta = getAnonymousMeta(this.astroVersion); |