|
| 1 | +import { checkbox, confirm, input, password, select } from '@inquirer/prompts'; |
| 2 | +import type { AzureDevOpsConnectionConfig } from '@sourcebot/schemas/v3/azuredevops.type'; |
| 3 | +import type { CollectResult, EnvVars } from './utils.js'; |
| 4 | +import { multiInput, note, toEnvKey } from './utils.js'; |
| 5 | + |
| 6 | +export async function collectAzureDevOpsConfig(connectionName: string): Promise<CollectResult> { |
| 7 | + const env: EnvVars = {}; |
| 8 | + |
| 9 | + const deploymentType = await select<'cloud' | 'server'>({ |
| 10 | + message: 'Which Azure DevOps deployment?', |
| 11 | + choices: [ |
| 12 | + { value: 'cloud', name: 'Azure DevOps Cloud', description: 'dev.azure.com' }, |
| 13 | + { value: 'server', name: 'Azure DevOps Server', description: 'self-hosted' }, |
| 14 | + ], |
| 15 | + }); |
| 16 | + |
| 17 | + const config: AzureDevOpsConnectionConfig = { |
| 18 | + type: 'azuredevops', |
| 19 | + deploymentType, |
| 20 | + token: { env: '' }, |
| 21 | + }; |
| 22 | + |
| 23 | + if (deploymentType === 'server') { |
| 24 | + const url = await input({ |
| 25 | + message: 'Azure DevOps Server URL (e.g. https://ado.example.com)', |
| 26 | + validate: (v) => { |
| 27 | + if (!v?.trim()) { |
| 28 | + return 'URL is required'; |
| 29 | + } |
| 30 | + if (!/^https?:\/\//.test(v)) { |
| 31 | + return 'Must start with http:// or https://'; |
| 32 | + } |
| 33 | + return true; |
| 34 | + }, |
| 35 | + }); |
| 36 | + config.url = url; |
| 37 | + |
| 38 | + const useTfsPath = await confirm({ |
| 39 | + message: 'Use legacy TFS path format (/tfs in API URLs)?', |
| 40 | + default: false, |
| 41 | + }); |
| 42 | + if (useTfsPath) { |
| 43 | + config.useTfsPath = true; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + note( |
| 48 | + [ |
| 49 | + 'Create a Personal Access Token at:', |
| 50 | + deploymentType === 'cloud' |
| 51 | + ? ' https://dev.azure.com/<your-org>/_usersSettings/tokens' |
| 52 | + : ' <your-server-url>/_usersSettings/tokens', |
| 53 | + 'Grant `Code (Read)` scope so Sourcebot can find and clone your repos.', |
| 54 | + ].join('\n'), |
| 55 | + 'Azure DevOps Personal Access Token', |
| 56 | + ); |
| 57 | + |
| 58 | + const envKey = toEnvKey(connectionName, 'TOKEN'); |
| 59 | + const token = await password({ |
| 60 | + message: `Azure DevOps Personal Access Token (stored as ${envKey})`, |
| 61 | + mask: true, |
| 62 | + validate: (v) => !v?.trim() ? 'Token is required' : true, |
| 63 | + }); |
| 64 | + env[envKey] = token; |
| 65 | + config.token = { env: envKey }; |
| 66 | + |
| 67 | + const orgLabel = deploymentType === 'cloud' ? 'organization' : 'collection'; |
| 68 | + const orgLabelPlural = deploymentType === 'cloud' ? 'Organizations' : 'Collections'; |
| 69 | + |
| 70 | + const targets = await checkbox<string>({ |
| 71 | + message: 'What do you want to index?', |
| 72 | + choices: [ |
| 73 | + { value: 'orgs', name: orgLabelPlural, description: `all projects in a ${orgLabel}` }, |
| 74 | + { value: 'projects', name: 'Specific projects', description: `${orgLabel}/project format` }, |
| 75 | + { value: 'repos', name: 'Specific repositories', description: `${orgLabel}/project/repo format` }, |
| 76 | + ], |
| 77 | + required: true, |
| 78 | + }); |
| 79 | + |
| 80 | + if (targets.includes('orgs')) { |
| 81 | + config.orgs = await multiInput({ |
| 82 | + message: `${orgLabelPlural} to index`, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + if (targets.includes('projects')) { |
| 87 | + config.projects = await multiInput({ |
| 88 | + message: `Projects to index (${orgLabel}/project)`, |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + if (targets.includes('repos')) { |
| 93 | + config.repos = await multiInput({ |
| 94 | + message: `Repositories to index (${orgLabel}/project/repo)`, |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + return { connections: [{ config }], env }; |
| 99 | +} |
0 commit comments