<commit_before>import * as fs from 'fs';
import * as path from 'path';
import * as Task from 'vsts-task-lib';
import * as Request from 'request';
import * as RequestPromise from 'request-promise';

import { ReportType } from './enums';
import { Constants } from './constants';
import * as ZapRequest from './zapRequest';
import * as ZapReport from './zapReporting';


export function getActiveScanStatus(scanId: number, apiKey: string, zapApiUrl: string): Promise<number> {
    let statusOptions: ZapRequest.ZapActiveScanStatusOptions = {
        zapapiformat: 'JSON',
        apikey: apiKey,
        formMethod: 'GET',
        scanId: scanId
    };

    let requestOptions: Request.UriOptions & RequestPromise.RequestPromiseOptions = {
        uri: `http://${zapApiUrl}/JSON/ascan/view/status/`,
        qs: statusOptions
    };

    Task.debug('*** Get Active Scan Status ***');
    Task.debug(`ZAP API Call: http://${zapApiUrl}/JSON/ascan/view/status/`);
    Task.debug(`Request Options: ${JSON.stringify(statusOptions)}`);

    return new Promise<number>((resolve, reject) => {
        RequestPromise(requestOptions)
            .then((res: any) => {
                let result: ZapRequest.ZapActiveScanStatus = JSON.parse(res);
                Task.debug(`Scan Status Result: ${JSON.stringify(result)}`);
                
                resolve(result.status);
            })
            .error((err: any) => {
                reject(-1);
            });
    });
}

export function getActiveScanResults(apiKey: string, zapApiUrl: string, type: ReportType = ReportType.XML): Promise<string> {

    let reportType: string = 'xmlreport';

    if (type == ReportType.XML) { reportType = Constants.XmlReport; }
    if (type == ReportType.HTML) { reportType = Constants.HtmlReport; } 
    if (type == ReportType.MD) { reportType = Constants.MdReport; } 

    let reportOptions: ZapRequest.ZapScanReportOptions = {
        apikey: apiKey,
        formMethod: 'GET'
    };

    let requestOptions: Request.UriOptions & RequestPromise.RequestPromiseOptions = {
        uri: `http://${zapApiUrl}/OTHER/core/other/${reportType}/`,
        qs: reportOptions
    };<commit_msg>Moved the logic to check active scan status in to helper.ts<commit_after>import { triggerAsyncId } from 'async_hooks';
import * as fs from 'fs';
import * as path from 'path';
import * as Task from 'vsts-task-lib';
import * as Request from 'request';
import * as RequestPromise from 'request-promise';
import * as sleep from 'thread-sleep';

import { ReportType } from './enums';
import { Constants } from './constants';
import * as ZapRequest from './zapRequest';
import * as ZapReport from './zapReporting';


export function getActiveScanStatus(scanId: number, apiKey: string, zapApiUrl: string): Promise<number> {
    let statusOptions: ZapRequest.ZapActiveScanStatusOptions = {
        zapapiformat: 'JSON',
        apikey: apiKey,
        formMethod: 'GET',
        scanId: scanId
    };

    let requestOptions: Request.UriOptions & RequestPromise.RequestPromiseOptions = {
        uri: `http://${zapApiUrl}/JSON/ascan/view/status/`,
        qs: statusOptions
    };

    Task.debug('*** Get Active Scan Status ***');
    Task.debug(`ZAP API Call: http://${zapApiUrl}/JSON/ascan/view/status/`);
    Task.debug(`Request Options: ${JSON.stringify(statusOptions)}`);

    return new Promise<number>((resolve, reject) => {
        RequestPromise(requestOptions)
            .then((res: any) => {
                let result: ZapRequest.ZapActiveScanStatus = JSON.parse(res);
                Task.debug(`Scan Status Result: ${JSON.stringify(result)}`);
                
                resolve(result.status);
            })
            .error((err: any) => {
                reject(-1);
            });
    });
}

export async function checkActiveScanStatus(scanId: number, apiKey: string, zapApiUrl: string): Promise<boolean>{
    let previousScanStatus: number = 0;
    let scanCompleted: boolean = false;

    return new Promise<boolean>(async (resolve, reject) => {
        try {
            while (true) {
                sleep(10000);
                let scanStatus: number = await getActiveScanStatus(scanId, apiKey, zapApiUrl);

                if(scanStatus >= 100) {
                    console.log(`Scan In Progress: ${scanStatus}%`);
                    console.log('Active scan complete...');
                    scanCompleted = true;
                    break;
                }

                if (previousScanStatus != scanStatus) {
                    console.log(`Scan In Progress: ${scanStatus}%`);
                    scanCompleted = false;
                }

                previousScanStatus = scanStatus;
            }

            resolve(scanCompleted);

        } catch (error) {
            reject(scanCompleted);
        }
    });

    
}

export function getActiveScanResults(apiKey: string, zapApiUrl: string, type: ReportType = ReportType.XML): Promise<string> {

    let reportType: string = 'xmlreport';

    if (type == ReportType.XML) { reportType = Constants.XmlReport; }
    if (type == ReportType.HTML) { reportType = Constants.HtmlReport; } 
    if (type == ReportType.MD) { reportType = Constants.MdReport; } 

    let reportOptions: ZapRequest.ZapScanReportOptions = {
        apikey: apiKey,
        formMethod: 'GET'
    };

    let requestOptions: Request.UriOptions & RequestPromise.RequestPromiseOptions = {
        uri: `http://${zapApiUrl}/OTHER/core/other/${reportType}/`,
        qs: reportOptions
    };
{'fraction_non_alphanumeric': 0.12864121584946625, 'fraction_numerical': 0.001990229781074724, 'mean_word_length': 2.5595621377978106, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3288166', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1531, 'n_words': 372}