<commit_before>import BaseImporter from 'paw-base-importer'
import RequestContext, {
    Group,
    Parser
} from 'api-flow'

@registerImporter // eslint-disable-line
export default class PostmanImporter extends BaseImporter {
    static identifier = 'com.luckymarmot.PawExtensions.PostmanImporter';
    static title = 'Postman Importer';

    static fileExtensions = [];
    static inputs = [];

    constructor() {
        super()
        this.ENVIRONMENT_DOMAIN_NAME = 'Postman Environments'
    }

    canImport(context, items) {
        let sum = 0
        for (let item of items) {
            sum += ::this._canImportItem(context, item)
        }
        return items.length > 0 ? sum / items.length : 0
    }

    _canImportItem(context, item) {
        let postman
        try {
            postman = JSON.parse(item.content)
        }
        catch (jsonParseError) {
            return 0
        }
        if (postman) {
            if (
                postman.info ||
                postman.item ||
                postman.variables ||
                postman.variable
            ) {
                return 0.5
            }
            let score = 0
            score += postman.collections ? 1 / 2 : 0
            score += postman.environments ? 1 / 2 : 0
            score += postman.id && postman.name && postman.timestamp ? 1 / 2 : 0
            score += postman.requests ? 1 / 2 : 0
            score += postman.values ? 1 / 2 : 0
            score = score < 1 ? score : 1
            return score
        }
        return 0
    }

    /*
      @params:
        - reqContexts
        - context
        - items
        - options
    */
    createRequestContext(reqContexts, context, item) {
        const parser = new Parser.Postman()
        try {
            let reqContext = parser.parse(item.content)
        }
        catch (e) {
            throw new Error(
                'Postman recently changed their data format. We\'re ' +
                'currently working on a fix. In the meantime, you can ' +
                'convert your file manually, read more at:\n\n' +
                'https://github.com/luckymarmot/Paw-PostmanImporter/issues/13'
            )
        }

        let current = reqContexts[0] || {
            context: new RequestContext(),
            items: []
        }

        let currentReqContext = current.context

        currentReqContext = currentReqContext
            .mergeEnvironments(reqContext.get('environments'))

        if (
            currentReqContext.getIn([ 'group', 'name' ])
            ===
            'Postman'
        ) {
            if (reqContext.getIn([ 'group', 'children' ]).size > 0) {
                let name = reqContext.getIn([ 'group', 'name' ]) ||
                    ((item || {}).file || {}).name ||<commit_msg>fixed a really stupid yet critical bug introduced with the warning<commit_after>import BaseImporter from 'paw-base-importer'
import RequestContext, {
    Group,
    Parser
} from 'api-flow'

@registerImporter // eslint-disable-line
export default class PostmanImporter extends BaseImporter {
    static identifier = 'com.luckymarmot.PawExtensions.PostmanImporter';
    static title = 'Postman Importer';

    static fileExtensions = [];
    static inputs = [];

    constructor() {
        super()
        this.ENVIRONMENT_DOMAIN_NAME = 'Postman Environments'
    }

    canImport(context, items) {
        let sum = 0
        for (let item of items) {
            sum += ::this._canImportItem(context, item)
        }
        return items.length > 0 ? sum / items.length : 0
    }

    _canImportItem(context, item) {
        let postman
        try {
            postman = JSON.parse(item.content)
        }
        catch (jsonParseError) {
            return 0
        }
        if (postman) {
            if (
                postman.info ||
                postman.item ||
                postman.variables ||
                postman.variable
            ) {
                return 0.5
            }
            let score = 0
            score += postman.collections ? 1 / 2 : 0
            score += postman.environments ? 1 / 2 : 0
            score += postman.id && postman.name && postman.timestamp ? 1 / 2 : 0
            score += postman.requests ? 1 / 2 : 0
            score += postman.values ? 1 / 2 : 0
            score = score < 1 ? score : 1
            return score
        }
        return 0
    }

    /*
      @params:
        - reqContexts
        - context
        - items
        - options
    */
    createRequestContext(reqContexts, context, item) {
        const parser = new Parser.Postman()
        let reqContext
        try {
            reqContext = parser.parse(item.content)
        }
        catch (e) {
            throw new Error(
                'Postman recently changed their data format. We\'re ' +
                'currently working on a fix. In the meantime, you can ' +
                'convert your file manually, read more at:\n\n' +
                'https://github.com/luckymarmot/Paw-PostmanImporter/issues/13'
            )
        }

        let current = reqContexts[0] || {
            context: new RequestContext(),
            items: []
        }

        let currentReqContext = current.context

        currentReqContext = currentReqContext
            .mergeEnvironments(reqContext.get('environments'))

        if (
            currentReqContext.getIn([ 'group', 'name' ])
            ===
            'Postman'
        ) {
            if (reqContext.getIn([ 'group', 'children' ]).size > 0) {
                let name = reqContext.getIn([ 'group', 'name' ]) ||
                    ((item || {}).file || {}).name ||
{'fraction_non_alphanumeric': 0.10472496473906912, 'fraction_numerical': 0.010225669957686883, 'mean_word_length': 1.477292576419214, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2724388', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1524, 'n_words': 391}