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