All files / utils task-inputs.ts

87.23% Statements 41/47
85.71% Branches 30/35
100% Functions 2/2
86.95% Lines 40/46

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                12x 12x 2x 3x 2x   2x 1x 1x 1x         11x 11x     11x 2x 2x 1x   1x   1x 1x 9x 8x 8x   1x   1x         9x       12x           9x 9x 1x   1x     8x 8x 1x 1x     7x 7x         7x 12x           7x 12x   12x                      
import * as tl from 'azure-pipelines-task-lib/task';
 
export interface ValidatedTaskInputs {
    githubPat: string;
    azureDevOpsToken: string;
    azureDevOpsAuthType: 'Bearer' | 'Basic';
    resolvedCollectionUri: string;
    project: string;
    repository: string;
    pullRequestId: string;
    timeoutMinutes: number;
    model: string | undefined;
}
 
export function getValidatedTaskInputs(): ValidatedTaskInputs | null {
    // Author filter (check before anything else)
    const authors = tl.getInput('authors');
    if (authors) {
        const requestedForEmail = tl.getVariable('Build.RequestedForEmail') ?? '';
        const authorList = authors.split(',').map(e => e.trim().toLowerCase());
        const currentAuthor = requestedForEmail.toLowerCase();
 
        if (!authorList.includes(currentAuthor)) {
            console.log(`Skipping: PR author (${requestedForEmail || 'unknown'}) is not in the configured authors list.`);
            tl.setResult(tl.TaskResult.Succeeded, 'Skipped: PR author not in configured authors list.');
            return null;
        }
    }
 
    // Azure DevOps authentication
    const useSystemAccessToken = tl.getBoolInput('useSystemAccessToken', false);
    const azureDevOpsPat = tl.getInput('azureDevOpsPat');
    let azureDevOpsToken: string;
    let azureDevOpsAuthType: 'Bearer' | 'Basic';
    if (useSystemAccessToken) {
        const systemToken = tl.getVariable('System.AccessToken');
        if (!systemToken) {
            tl.setResult(tl.TaskResult.Failed,
                'System.AccessToken is not available. Ensure the pipeline has access to the OAuth token.');
            return null;
        }
        azureDevOpsToken = systemToken;
        azureDevOpsAuthType = 'Bearer';
    } else if (azureDevOpsPat) {
        azureDevOpsToken = azureDevOpsPat;
        azureDevOpsAuthType = 'Basic';
    } else {
        tl.setResult(tl.TaskResult.Failed,
            'Azure DevOps authentication is required. Either provide an Azure DevOps PAT or enable "Use System Access Token".');
        return null;
    }
 
    // Collection URI resolution (explicit input → org name → pipeline context)
    const resolvedCollectionUri = (
        tl.getInput('collectionUri')?.replace(/\/+$/, '') ??
        (tl.getInput('organization') ? `https://dev.azure.com/${tl.getInput('organization')}` : undefined) ??
        tl.getVariable('System.CollectionUri')?.replace(/\/+$/, '')
    );
    Iif (!resolvedCollectionUri) {
        tl.setResult(tl.TaskResult.Failed,
            'Collection URI could not be determined. Provide collectionUri, organization, or ensure System.CollectionUri is available.');
        return null;
    }
 
    const githubPat = tl.getInput('githubPat');
    if (!githubPat) {
        tl.setResult(tl.TaskResult.Failed,
            'GitHub PAT is required for GitHub Copilot CLI. Please provide the githubPat input.');
        return null;
    }
 
    const project = tl.getInput('project');
    if (!project) {
        tl.setResult(tl.TaskResult.Failed, 'Project is required.');
        return null;
    }
 
    const repository = tl.getInput('repository');
    Iif (!repository) {
        tl.setResult(tl.TaskResult.Failed, 'Repository is required.');
        return null;
    }
 
    const pullRequestId = tl.getInput('pullRequestId') || tl.getVariable('System.PullRequest.PullRequestId');
    Iif (!pullRequestId) {
        tl.setResult(tl.TaskResult.Failed,
            'Pull Request ID is required. Either provide it as an input or run as part of a PR validation build.');
        return null;
    }
 
    const timeoutMinutes = parseInt(tl.getInput('timeout') ?? '15', 10) || 15;
    const model = tl.getInput('model') || undefined;
 
    return {
        githubPat,
        azureDevOpsToken,
        azureDevOpsAuthType,
        resolvedCollectionUri,
        project,
        repository,
        pullRequestId,
        timeoutMinutes,
        model
    };
}