All files / utils args.ts

100% Statements 19/19
100% Branches 23/23
100% Functions 3/3
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63                    5x 5x 8x 8x 6x 6x     8x                                 4x 4x 4x 4x 4x   4x 2x             2x 2x     2x                  
/**
 * Shared CLI argument parser and env var loader for agent scripts
 * (add-comment.js, update-comment.js, delete-comment.js).
 */
 
/**
 * Parses CLI arguments in --key value format.
 * Converts kebab-case keys to camelCase (e.g. --file-path → filePath).
 */
export function parseArgs(argv: string[]): Record<string, string> {
    const result: Record<string, string> = {};
    for (let i = 0; i < argv.length; i++) {
        const arg = argv[i];
        if (arg.startsWith('--') && i + 1 < argv.length) {
            const key = arg.slice(2).replace(/-([a-z])/g, (_, c: string) => c.toUpperCase());
            result[key] = argv[++i];
        }
    }
    return result;
}
 
export interface AdoEnv {
    token: string;
    authType: 'Bearer' | 'Basic';
    collectionUri: string;
    project: string;
    repository: string;
    prId: string;
}
 
/**
 * Reads ADO environment variables. Returns null if any required var is missing.
 * Logs a warning with the missing variable names using the provided script label.
 */
export function loadAdoEnv(scriptLabel: string): AdoEnv | null {
    const token = process.env['AZUREDEVOPS_TOKEN'];
    const collectionUri = process.env['AZUREDEVOPS_COLLECTION_URI'];
    const project = process.env['PROJECT'];
    const repository = process.env['REPOSITORY'];
    const prId = process.env['PRID'];
 
    if (!token || !collectionUri || !project || !repository || !prId) {
        const missing = [
            !token && 'AZUREDEVOPS_TOKEN',
            !collectionUri && 'AZUREDEVOPS_COLLECTION_URI',
            !project && 'PROJECT',
            !repository && 'REPOSITORY',
            !prId && 'PRID',
        ].filter(Boolean);
        console.warn(`${scriptLabel}: Missing env vars: ${missing.join(', ')}`);
        return null;
    }
 
    return {
        token,
        authType: (process.env['AZUREDEVOPS_AUTH_TYPE'] ?? 'Basic') as 'Bearer' | 'Basic',
        collectionUri,
        project,
        repository,
        prId,
    };
}