'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

require('source-map-support/register');

var _mocha = require('mocha');

var _integrationSpecInt = require('./_integration.spec-int.js');

var _integrationSpecInt2 = _interopRequireDefault(_integrationSpecInt);

var _gtmGithubUtils = require('../src/serverless/gtmGithubUtils.js');

var _gtmGithubUtils2 = _interopRequireDefault(_gtmGithubUtils);

var _formatJson = require('format-json');

var _formatJson2 = _interopRequireDefault(_formatJson);

var _firstline = require('firstline');

var _firstline2 = _interopRequireDefault(_firstline);

var _util = require('util');

var _util2 = _interopRequireDefault(_util);

var _dotenv = require('dotenv');

var _dotenv2 = _interopRequireDefault(_dotenv);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

//import { expect } from 'chai';
//import supertest from 'supertest';
_dotenv2.default.config();

(0, _mocha.describe)('GitHub Task Manager', function () {
    (0, _mocha.describe)('should correctly update a pull request', function () {
        /**
         * 1. fork github repo from template at github.com/zotoio/gtm-test
         * 2. add webhook with secret
         * 3. create a branch
         * 4. create a file
         * 5. open a pull request back to master
         *
         */

        //console.log(integration);
        //let request = supertest(integration.urlPrefix);

        (0, _mocha.it)('should trigger behaviors if hostname matches exactly', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            var testName, hookUrl, github, gh;
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            testName = 'test_' + Date.now();
                            _context.next = 3;
                            return (0, _firstline2.default)('./sls-hook-url.out');

                        case 3:
                            hookUrl = _context.sent;

                            console.log('hook url: ' + hookUrl);
                            github = _gtmGithubUtils2.default.connect();
                            gh = {
                                repos: {
                                    fork: _util2.default.promisify(github.repos.fork),
                                    createHook: _util2.default.promisify(github.repos.createHook),
                                    createFile: _util2.default.promisify(github.repos.createFile)
                                },
                                gitdata: {
                                    getReference: _util2.default.promisify(github.gitdata.getReference),
                                    createReference: _util2.default.promisify(github.gitdata.createReference)
                                },
                                pullRequests: {
                                    create: _util2.default.promisify(github.pullRequests.create)
                                }
                            };
                            return _context.abrupt('return', gh.repos.fork({
                                owner: 'zotoio',
                                repo: _integrationSpecInt2.default.config.testRepoName
                            }).then(function (res) {
                                console.log('fork: ' + _formatJson2.default.plain(res));

                                return gh.repos.createHook({
                                    owner: process.env.GTM_GITHUB_OWNER,
                                    repo: _integrationSpecInt2.default.config.testRepoName,
                                    name: 'web',
                                    events: ['pull_request'],
                                    active: true,
                                    config: {
                                        url: hookUrl,
                                        secret: process.env.GTM_GITHUB_WEBHOOK_SECRET
                                    }
                                });
                            }).then(function (res) {
                                console.log('hook: ' + _formatJson2.default.plain(res));

                                return gh.gitdata.getReference({
                                    owner: process.env.GTM_GITHUB_OWNER,
                                    repo: _integrationSpecInt2.default.config.testRepoName,
                                    ref: 'heads/master'
                                });
                            }).then(function (res) {
                                console.log('master: ' + _formatJson2.default.plain(res));

                                var sha = res.data.object.sha;

                                return gh.gitdata.createReference({
                                    owner: process.env.GTM_GITHUB_OWNER,
                                    repo: _integrationSpecInt2.default.config.testRepoName,
                                    ref: 'refs/heads/' + testName,
                                    sha: sha
                                });
                            }).then(function (res) {
                                console.log('branch: ' + _formatJson2.default.plain(res));

                                return gh.repos.createFile({
                                    owner: process.env.GTM_GITHUB_OWNER,
                                    repo: _integrationSpecInt2.default.config.testRepoName,
                                    path: testName + '.txt',
                                    content: Buffer.from(testName.toString()).toString('base64'),
                                    message: 'updated/' + testName + '.txt',
                                    branch: 'refs/heads/' + testName
                                });
                            }).then(function (res) {
                                console.log('file: ' + _formatJson2.default.plain(res));

                                return gh.pullRequests.create({
                                    owner: process.env.GTM_GITHUB_OWNER,
                                    repo: _integrationSpecInt2.default.config.testRepoName,
                                    title: 'new file: ' + testName + '.txt',
                                    head: 'refs/heads/' + testName,
                                    base: 'refs/heads/master'
                                });
                            }).then(function (res) {
                                console.log('pull_request: ' + _formatJson2.default.plain(res));
                            }));

                        case 8:
                        case 'end':
                            return _context.stop();
                    }
                }
            }, _callee, undefined);
        })));
    });
});