<commit_before>'use strict';

const Assert = require('chai').assert;
const request = require('../support/request');
const TIMEOUT = 240 * 1000;

module.exports = function server() {
    // eslint-disable-next-line new-cap
    this.Before({
        tags: ['@secrets']
    }, () => {
        this.repoOrg = 'screwdriver-cd-test';
        this.repoName = 'functional-secrets';
        this.pipelineId = null;
        this.secretId = null;
    });

    this.Given(/^an existing repository for secret with these users and permissions:$/,
        { timeout: TIMEOUT }, table =>
        this.getJwt(this.accessKey)
        .then((response) => {
            this.jwt = response.body.token;

            return request({
                uri: `${this.instance}/${this.namespace}/pipelines`,
                method: 'POST',
                auth: {
                    bearer: this.jwt
                },
                body: {
                    checkoutUrl: `<EMAIL>:${this.repoOrg}/${this.repoName}.git#master`
                },
                json: true
            });
        })
        .then((response) => {
            Assert.oneOf(response.statusCode, [409, 201]);

            if (response.statusCode === 201) {
                this.pipelineId = response.body.id;
            } else {
                const str = response.body.message;
                const id = str.split(': ')[1];

                this.pipelineId = id;
            }

            return table;
        })
    );

    this.Given(/^an existing pipeline with that repository with the workflow:$/, table => table);

    this.When(/^a secret "foo" is added globally$/, () =>
        request({
            uri: `${this.instance}/${this.namespace}/secrets`,
            method: 'POST',
            body: {
                pipelineId: this.pipelineId,
                name: 'FOO',
                value: 'secrets',
                allowInPR: false
            },
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            this.secretId = response.body.id;
            Assert.equal(response.statusCode, 201);
        })
    );

    this.When(/^the "main" job is started$/, { timeout: TIMEOUT }, () =>
        request({
            uri: `${this.instance}/${this.namespace}/pipelines/${this.pipelineId}/jobs`,
            method: 'GET',
            json: true
        }).then((response) => {
            this.jobId = response.body[0].id;
            this.secondJobId = response.body[1].id;
            if (2 in response.body) this.thirdJobId = response.body[2].id;
            this.lastJobId = response.body[response.body.length - 1].id;

            Assert.equal(response.statusCode, 200);
        })
        .then(() =>
            request({
                uri: `${this.instance}/${this.namespace}/builds`,
                method: 'POST',
                body: {
                    jobId: this.jobId
                },
                auth: {<commit_msg>Modify not to use `in`<commit_after>'use strict';

const Assert = require('chai').assert;
const request = require('../support/request');
const TIMEOUT = 240 * 1000;

module.exports = function server() {
    // eslint-disable-next-line new-cap
    this.Before({
        tags: ['@secrets']
    }, () => {
        this.repoOrg = 'screwdriver-cd-test';
        this.repoName = 'functional-secrets';
        this.pipelineId = null;
        this.secretId = null;
    });

    this.Given(/^an existing repository for secret with these users and permissions:$/,
        { timeout: TIMEOUT }, table =>
        this.getJwt(this.accessKey)
        .then((response) => {
            this.jwt = response.body.token;

            return request({
                uri: `${this.instance}/${this.namespace}/pipelines`,
                method: 'POST',
                auth: {
                    bearer: this.jwt
                },
                body: {
                    checkoutUrl: `<EMAIL>:${this.repoOrg}/${this.repoName}.git#master`
                },
                json: true
            });
        })
        .then((response) => {
            Assert.oneOf(response.statusCode, [409, 201]);

            if (response.statusCode === 201) {
                this.pipelineId = response.body.id;
            } else {
                const str = response.body.message;
                const id = str.split(': ')[1];

                this.pipelineId = id;
            }

            return table;
        })
    );

    this.Given(/^an existing pipeline with that repository with the workflow:$/, table => table);

    this.When(/^a secret "foo" is added globally$/, () =>
        request({
            uri: `${this.instance}/${this.namespace}/secrets`,
            method: 'POST',
            body: {
                pipelineId: this.pipelineId,
                name: 'FOO',
                value: 'secrets',
                allowInPR: false
            },
            auth: {
                bearer: this.jwt
            },
            json: true
        }).then((response) => {
            this.secretId = response.body.id;
            Assert.equal(response.statusCode, 201);
        })
    );

    this.When(/^the "main" job is started$/, { timeout: TIMEOUT }, () =>
        request({
            uri: `${this.instance}/${this.namespace}/pipelines/${this.pipelineId}/jobs`,
            method: 'GET',
            json: true
        }).then((response) => {
            this.jobId = response.body[0].id;
            this.secondJobId = response.body[1].id;
            this.thirdJobId = typeof response.body[2] === 'object' ? response.body[2].id : null;
            this.lastJobId = (function getLastJobId() {
                for (let i = response.body.length - 1; i >= 0; i -= 1) {
                    if (typeof response.body[i] === 'object') {
                        return response.body[i].id;
                    }
                }

                return null;
            }(response));

            Assert.equal(response.statusCode, 200);
        })
        .then(() =>
            request({
                uri: `${this.instance}/${this.namespace}/builds`,
                method: 'POST',
                body: {
                    jobId: this.jobId
                },
                auth: {
{'fraction_non_alphanumeric': 0.14410688722761253, 'fraction_numerical': 0.009225385716557977, 'mean_word_length': 1.3908745247148289, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4536935', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1686, 'n_words': 344}