-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix telemetry disable command #9290
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5006fa8
Fix telemetry commands (#9289)
DavidMulder0 a307d66
use active tense for command outcomes
dcousens dc983a4
add changeset
dcousens 446ddcc
tidy up chalk
dcousens 394eaa3
move to a new version 2 telemetry endpoint (failing for now)
dcousens d237d20
add version 3 telemetry configuration to re-inform unknown opt-ins
dcousens 8745a39
update test titles after review
dcousens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@keystone-6/core": patch | ||
--- | ||
|
||
Fixes the `keystone telemetry disable` command for opting out of telemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,19 @@ import https from 'node:https' | |
|
||
import ci from 'ci-info' | ||
import Conf from 'conf' | ||
import chalk from 'chalk' | ||
import { | ||
bold, | ||
yellow as y, | ||
red as r, | ||
green as g | ||
} from 'chalk' | ||
import { | ||
type Configuration, | ||
type Device, | ||
type PackageName, | ||
type Project, | ||
type Telemetry, | ||
type TelemetryVersion1, | ||
type TelemetryVersion2and3, | ||
} from '../types/telemetry' | ||
import { type DatabaseProvider } from '../types' | ||
import { type InitialisedList } from './core/initialise-lists' | ||
|
@@ -25,17 +31,6 @@ const packageNames: PackageName[] = [ | |
'@opensaas/keystone-nextjs-auth', | ||
] | ||
|
||
type TelemetryVersion1 = | ||
| undefined | ||
| false | ||
| { | ||
device: { lastSentDate?: string, informedAt: string } | ||
projects: { | ||
default: { lastSentDate?: string, informedAt: string } | ||
[projectPath: string]: { lastSentDate?: string, informedAt: string } | ||
} | ||
} | ||
|
||
function log (message: unknown) { | ||
if (process.env.KEYSTONE_TELEMETRY_DEBUG === '1') { | ||
console.log(`${message}`) | ||
|
@@ -46,38 +41,46 @@ function getTelemetryConfig () { | |
const userConfig = new Conf<Configuration>({ | ||
projectName: 'keystonejs', | ||
projectSuffix: '', | ||
projectVersion: '2.0.0', | ||
projectVersion: '3.0.0', | ||
migrations: { | ||
'^2.0.0': (store: Conf<Configuration>) => { | ||
const existing = store.get('telemetry') as unknown as TelemetryVersion1 | ||
if (!existing) return | ||
'^2.0.0': (store) => { | ||
const existing = store.get('telemetry') as TelemetryVersion1 | ||
if (!existing) return // skip non-configured or known opt-outs | ||
|
||
const replacement: Telemetry = { | ||
// every informedAt was a copy of device.informedAt, it was copied everywhere | ||
informedAt: existing.device.informedAt, | ||
const replacement: TelemetryVersion2and3 = { | ||
informedAt: null, // re-inform | ||
device: { | ||
lastSentDate: existing.device.lastSentDate ?? null, | ||
}, | ||
projects: {}, // manually copying this below | ||
projects: {}, // see below | ||
} | ||
|
||
// copy existing project lastSentDate's | ||
for (const [projectPath, project] of Object.entries(existing.projects)) { | ||
if (projectPath === 'default') continue // informedAt moved to root | ||
if (projectPath === 'default') continue // informedAt moved to device.lastSentDate | ||
|
||
// dont copy garbage | ||
if (typeof project !== 'object') continue | ||
if (typeof project.lastSentDate !== 'string') continue | ||
if (new Date(project.lastSentDate).toString() === 'Invalid Date') continue | ||
|
||
// only lastSentDate is retained | ||
// retain lastSentDate | ||
replacement.projects[projectPath] = { | ||
lastSentDate: project.lastSentDate, | ||
} | ||
} | ||
|
||
store.set('telemetry', replacement) | ||
}, | ||
'^3.0.0': (store) => { | ||
const existing = store.get('telemetry') as TelemetryVersion2and3 | ||
if (!existing) return // skip non-configured or known opt-outs | ||
|
||
store.set('telemetry', { | ||
...existing, | ||
informedAt: null, // re-inform | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If someone has unknowingly missed their opt-out failing, this will re-inform them |
||
} satisfies TelemetryVersion2and3) | ||
}, | ||
}, | ||
}) | ||
|
||
|
@@ -97,8 +100,8 @@ function getDefaultedTelemetryConfig () { | |
device: { | ||
lastSentDate: null, | ||
}, | ||
projects: {} as Telemetry['projects'], // help Typescript infer the type | ||
}, | ||
projects: {}, | ||
} as TelemetryVersion2and3, // help Typescript infer the type | ||
userConfig, | ||
} | ||
} | ||
|
@@ -147,74 +150,52 @@ function collectPackageVersions () { | |
} | ||
|
||
function printAbout () { | ||
console.log( | ||
`${chalk.yellow('Keystone collects anonymous data when you run')} ${chalk.green( | ||
'"keystone dev"' | ||
)}` | ||
) | ||
console.log(`${y`Keystone collects anonymous data when you run`} ${g`"keystone dev"`}`) | ||
console.log() | ||
console.log( | ||
`For more information, including how to opt-out see https://keystonejs.com/telemetry` | ||
) | ||
console.log(`For more information, including how to opt-out see https://keystonejs.com/telemetry`) | ||
} | ||
|
||
export function printTelemetryStatus () { | ||
const { telemetry } = getTelemetryConfig() | ||
|
||
if (telemetry === undefined) { | ||
console.log(`Keystone telemetry has been reset to ${chalk.yellow('uninitialized')}`) | ||
console.log() | ||
console.log( | ||
`Telemetry will be sent the next time you run ${chalk.green( | ||
'"keystone dev"' | ||
)}, unless you opt-out` | ||
) | ||
} else if (telemetry === false) { | ||
console.log(`Keystone telemetry is ${chalk.red('disabled')}`) | ||
console.log(`Keystone telemetry has been reset to ${y`uninitialized`}`) | ||
console.log() | ||
console.log(`Telemetry will ${chalk.red('not')} be sent by this system user`) | ||
} else if (typeof telemetry === 'object') { | ||
console.log(`Keystone telemetry is ${chalk.green('enabled')}`) | ||
console.log(`Telemetry will be sent the next time you run ${g`"keystone dev"`}, unless you opt-out`) | ||
return | ||
} | ||
|
||
if (telemetry === false) { | ||
console.log(`Keystone telemetry is ${r`disabled`}`) | ||
console.log() | ||
console.log(`Telemetry will ${r`not`} be sent by this system user`) | ||
return | ||
} | ||
|
||
console.log(` Device telemetry was last sent on ${telemetry.device.lastSentDate}`) | ||
for (const [projectPath, project] of Object.entries(telemetry.projects)) { | ||
console.log( | ||
` Project telemetry for "${chalk.yellow(projectPath)}" was last sent on ${ | ||
project?.lastSentDate | ||
}` | ||
) | ||
} | ||
console.log(`Keystone telemetry is ${g`enabled`}`) | ||
console.log() | ||
|
||
console.log() | ||
console.log( | ||
`Telemetry will be sent the next time you run ${chalk.green( | ||
'"keystone dev"' | ||
)}, unless you opt-out` | ||
) | ||
console.log(` Device telemetry was last sent on ${telemetry.device.lastSentDate}`) | ||
for (const [projectPath, project] of Object.entries(telemetry.projects)) { | ||
console.log(` Project telemetry for "${y(projectPath)}" was last sent on ${project?.lastSentDate}`) | ||
} | ||
|
||
console.log() | ||
console.log(`Telemetry will be sent the next time you run ${g`"keystone dev"`}, unless you opt-out`) | ||
} | ||
|
||
function inform () { | ||
const { telemetry, userConfig } = getDefaultedTelemetryConfig() | ||
|
||
// no telemetry? somehow our earlier checks missed an opt out, do nothing | ||
if (telemetry === false) return | ||
// no telemetry? somehow our earlier missed something, do nothing | ||
if (!telemetry) return | ||
|
||
console.log() // gap to help visiblity | ||
console.log(`${chalk.bold('Keystone Telemetry')}`) | ||
console.log(`${bold('Keystone Telemetry')}`) | ||
printAbout() | ||
console.log( | ||
`You can use ${chalk.green( | ||
'"keystone telemetry --help"' | ||
)} to update your preferences at any time` | ||
) | ||
console.log(`You can use ${g`"keystone telemetry --help"`} to update your preferences at any time`) | ||
console.log() | ||
console.log( | ||
`No telemetry data has been sent yet, but telemetry will be sent the next time you run ${chalk.green( | ||
'"keystone dev"' | ||
)}, unless you opt-out` | ||
) | ||
console.log(`No telemetry data has been sent, but telemetry will be sent the next time you run ${g`"keystone dev"`}, unless you opt-out`) | ||
console.log() // gap to help visiblity | ||
|
||
// update the informedAt | ||
|
@@ -224,7 +205,7 @@ function inform () { | |
|
||
async function sendEvent (eventType: 'project' | 'device', eventData: Project | Device) { | ||
const endpoint = process.env.KEYSTONE_TELEMETRY_ENDPOINT || defaultTelemetryEndpoint | ||
const req = https.request(`${endpoint}/v1/event/${eventType}`, { | ||
const req = https.request(`${endpoint}/2/event/${eventType}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
|
@@ -242,8 +223,8 @@ async function sendProjectTelemetryEvent ( | |
) { | ||
const { telemetry, userConfig } = getDefaultedTelemetryConfig() | ||
|
||
// no telemetry? somehow our earlier checks missed an opt out, do nothing | ||
if (telemetry === false) return | ||
// no telemetry? somehow our earlier missed something, do nothing | ||
if (!telemetry) return | ||
|
||
const project = telemetry.projects[cwd] ?? { lastSentDate: null } | ||
const { lastSentDate } = project | ||
|
@@ -268,8 +249,8 @@ async function sendProjectTelemetryEvent ( | |
async function sendDeviceTelemetryEvent () { | ||
const { telemetry, userConfig } = getDefaultedTelemetryConfig() | ||
|
||
// no telemetry? somehow our earlier checks missed an opt out, do nothing | ||
if (telemetry === false) return | ||
// no telemetry? somehow our earlier missed something, do nothing | ||
if (!telemetry) return | ||
|
||
const { lastSentDate } = telemetry.device | ||
if (lastSentDate && lastSentDate >= todaysDate) { | ||
|
@@ -305,7 +286,8 @@ export async function runTelemetry ( | |
const { telemetry } = getDefaultedTelemetryConfig() | ||
|
||
// don't run if the user has opted out | ||
if (telemetry === false) return | ||
// or if somehow our defaults are problematic, do nothing | ||
if (!telemetry) return | ||
|
||
// don't send telemetry before we inform the user, allowing opt-out | ||
if (!telemetry.informedAt) return inform() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If someone has unknowingly missed their opt-out failing, this will re-inform them