|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright 2025 The Go Authors. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as cp from 'child_process'; |
| 7 | +import * as fs from 'fs'; |
| 8 | +import * as path from 'path'; |
| 9 | +import * as util from 'util'; |
| 10 | +import { URL } from 'url'; |
| 11 | +import { getStateConfig } from '../goSurvey'; |
| 12 | +import { getBinPath } from '../util'; |
| 13 | +import { updateGlobalState } from '../stateUtils'; |
| 14 | +import { outputChannel } from '../goStatus'; |
| 15 | + |
| 16 | +/** |
| 17 | + * DeveloperSurveyConfig holds the configuration for the Go Developer survey. |
| 18 | + */ |
| 19 | +export interface DeveloperSurveyConfig { |
| 20 | + /** The start date for the survey promotion. The survey will not be prompted before this date. */ |
| 21 | + StartDate: Date; |
| 22 | + /** The end date for the survey promotion. The survey will not be prompted after this date. */ |
| 23 | + EndDate: Date; |
| 24 | + /** The URL for the survey. */ |
| 25 | + URL: string; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * DEVELOPER_SURVEY_CONFIG_STATE_KEY is the key for the latest go developer |
| 30 | + * survey config stored in VSCode memento. It should not be changed to maintain |
| 31 | + * backward compatibility with previous extension versions. |
| 32 | + */ |
| 33 | +export const DEVELOPER_SURVEY_CONFIG_STATE_KEY = 'developerSurveyConfigState'; |
| 34 | + |
| 35 | +/** |
| 36 | + * DeveloperSurveyConfigState holds the most recently fetched survey |
| 37 | + * configuration, along with metadata about when it was fetched and its version. |
| 38 | + * This data is stored in the global memento to be used as a cache. |
| 39 | + */ |
| 40 | +export interface DeveloperSurveyConfigState { |
| 41 | + config: DeveloperSurveyConfig; |
| 42 | + version: string; |
| 43 | + lastDateUpdated: Date; |
| 44 | +} |
| 45 | + |
| 46 | +export function getDeveloperSurveyConfigState(): DeveloperSurveyConfigState { |
| 47 | + return getStateConfig(DEVELOPER_SURVEY_CONFIG_STATE_KEY) as DeveloperSurveyConfigState; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * getLatestDeveloperSurvey fetches the latest Go Developer Survey configuration. |
| 52 | + * |
| 53 | + * It first checks for a cached version of the survey config and returns it if it's |
| 54 | + * less than 24 hours old. Otherwise, it attempts to download the latest survey |
| 55 | + * configuration by fetching the specified Go module. If the download fails, |
| 56 | + * it falls back to returning the stale cached config if available. |
| 57 | + * |
| 58 | + * @returns A Promise that resolves to the DeveloperSurveyConfig, or undefined. |
| 59 | + */ |
| 60 | +export async function getLatestDeveloperSurvey(now: Date): Promise<DeveloperSurveyConfig | undefined> { |
| 61 | + const oldState = getDeveloperSurveyConfigState(); |
| 62 | + if (oldState && oldState.config) { |
| 63 | + const SURVEY_CACHE_DURATION_MS = 24 * 60 * 60 * 1000; // 24 hours |
| 64 | + |
| 65 | + if (now.getTime() - oldState.lastDateUpdated.getTime() <= SURVEY_CACHE_DURATION_MS) { |
| 66 | + outputChannel.info(`Using cached Go developer survey: ${oldState.version}`); |
| 67 | + outputChannel.info( |
| 68 | + `Survey active from ${oldState.config.StartDate.toDateString()} to ${oldState.config.EndDate.toDateString()}` |
| 69 | + ); |
| 70 | + return oldState.config; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Fetch the latest go developer survey module and flush it to momento. |
| 75 | + const res = await fetchRemoteSurveyConfig(); |
| 76 | + if (!res) { |
| 77 | + if (oldState && oldState.config) { |
| 78 | + outputChannel.info(`Falling back to cached Go developer survey: ${oldState.version}`); |
| 79 | + outputChannel.info( |
| 80 | + `Survey active from ${oldState.config.StartDate.toDateString()} to ${oldState.config.EndDate.toDateString()}` |
| 81 | + ); |
| 82 | + return oldState.config; |
| 83 | + } else { |
| 84 | + return undefined; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + const [content, version] = res; |
| 89 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 90 | + const config = JSON.parse(content.toString(), (key: string, value: any) => { |
| 91 | + // Manually parse date fields. |
| 92 | + if (key === 'StartDate' || key === 'EndDate') { |
| 93 | + return new Date(value); |
| 94 | + } |
| 95 | + return value; |
| 96 | + }) as DeveloperSurveyConfig; |
| 97 | + |
| 98 | + const newState: DeveloperSurveyConfigState = { |
| 99 | + config: config, |
| 100 | + version: version, |
| 101 | + lastDateUpdated: now |
| 102 | + }; |
| 103 | + |
| 104 | + // The survey URL stored in config.json is the raw survey URL. We add the |
| 105 | + // s=v (for vscode) query parameter to identify the source of the survey |
| 106 | + // respondent. |
| 107 | + const url = new URL(config.URL); |
| 108 | + url.searchParams.append('s', 'v'); |
| 109 | + config.URL = url.toString(); |
| 110 | + |
| 111 | + updateGlobalState(DEVELOPER_SURVEY_CONFIG_STATE_KEY, JSON.stringify(newState)); |
| 112 | + |
| 113 | + outputChannel.info(`Using fetched Go developer survey: ${newState.version}`); |
| 114 | + outputChannel.info( |
| 115 | + `Survey active from ${newState.config.StartDate.toDateString()} to ${newState.config.EndDate.toDateString()}` |
| 116 | + ); |
| 117 | + return config; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Fetches the latest survey config file from its Go module. |
| 122 | + * @returns A tuple containing the file content and the module version. |
| 123 | + * |
| 124 | + * This is defined as a const function expression rather than a function |
| 125 | + * declaration to allow it to be stubbed in tests. By defining it as a const, |
| 126 | + * it becomes a property on the module's exports object, which can be |
| 127 | + * replaced by test spies (e.g., using sandbox.stub). |
| 128 | + */ |
| 129 | +export const fetchRemoteSurveyConfig = async (): Promise<[string, string] | undefined> => { |
| 130 | + const SURVEY_MODULE_PATH = 'github.com/golang/vscode-go/survey'; |
| 131 | + |
| 132 | + outputChannel.info('Fetching latest go developer survey'); |
| 133 | + const goRuntimePath = getBinPath('go'); |
| 134 | + if (!goRuntimePath) { |
| 135 | + console.warn('Failed to run "go mod download" as the "go" binary cannot be found'); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const execFile = util.promisify(cp.execFile); |
| 140 | + |
| 141 | + try { |
| 142 | + const { stdout } = await execFile(goRuntimePath, ['mod', 'download', '-json', `${SURVEY_MODULE_PATH}@latest`]); |
| 143 | + |
| 144 | + /** |
| 145 | + * Interface for the expected JSON output from `go mod download -json`. |
| 146 | + * See https://go.dev/ref/mod#go-mod-download for details. |
| 147 | + */ |
| 148 | + interface DownloadModuleOutput { |
| 149 | + Path: string; |
| 150 | + Version: string; |
| 151 | + Dir: string; |
| 152 | + } |
| 153 | + const info = JSON.parse(stdout) as DownloadModuleOutput; |
| 154 | + return [fs.readFileSync(path.join(info.Dir, 'config.json')).toString(), info.Version]; |
| 155 | + } catch (err) { |
| 156 | + outputChannel.error( |
| 157 | + `Failed to download the go developer survey module and parse "config.json": ${SURVEY_MODULE_PATH}:${err}` |
| 158 | + ); |
| 159 | + return; |
| 160 | + } |
| 161 | +}; |
0 commit comments