var express = require('express');
var path = require('path');
var https = require('https');

var app = express();

const deployUrl = 'https://ramis-app.herokuapp.com';
const port = process.env.PORT || 8080;
const clientId = '368d97fad7902db3554c';

app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, '/public')));

app.get('/', function(request, response) {
    response.render(path.join(__dirname, 'views', 'index'));
});

app.post('/event-handler', function(request) {
    var payload = '';

    request.on('data', function(chunk) {
        payload += chunk.toString();
    });

    request.on('end', function() {
        var pullRequest = JSON.parse(payload).pull_request;
        var sha = pullRequest.head.sha;
        var postData = JSON.stringify(
            {
                'state': 'success',
                'target_url': 'https://example.com/build/status',
                'description': 'The build succeeded!',
                'context': 'repo-state'
            }
        );

        var options = {
            protocol: 'https:',
            host: 'api.github.com',
            path: '/repos/ratherblue/ramis/statuses/' +
                sha + '?access_token=' + process.env.GITHUB_AUTH_TOKEN,
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': Buffer.byteLength(postData),
                'User-Agent': 'Ramis-App'
            }
        };

        var req = https.request(options, function(res) {
            res.setEncoding('utf8');
        });

        req.write(postData);
        req.end();
    });
});

app.get('/oauth', function(request, response) {
    var params = [
        'client_id=' + clientId,
        'redirect_uri=' + deployUrl + '/oauth-success',
        'scope=public_repo'
    ];

    var url = 'https://github.com/login/oauth/authorize?' + params.join('&');

    response.redirect(url);
});

app.get('/oauth-success', function(request, response) {
    response.render(path.join(__dirname, 'views', 'success'));
});


app.listen(port, function() {
    // eslint-disable-next-line no-console
    console.log('Running on http://localhost:' + port);
});

exports.init = function() {
    return false;
};

@@ -1983,32 +1983,50 @@
 st, response) %7B%0A
+    addWebHook();%0A
     response.ren
@@ -2261,8 +2261,866 @@
 lse;%0A%7D;%0A
+%0Afunction addWebHook() %7B%0A    // POST /repos/:owner/:repo/hooks%0A%0A    var postData = JSON.stringify(%7B%0A        'name': 'web',%0A        'active': true,%0A        'events': %5B%0A            'push',%0A            'pull_request'%0A        %5D,%0A        'config': %7B%0A            'url': 'http://ramis-app.herokuapp.com/event-handler',%0A            'content_type': 'json'%0A        %7D%0A    %7D);%0A%0A    var options = %7B%0A        protocol: 'https:',%0A        host: 'api.github.com',%0A        path: '/repos/ratherblue/ramis/hooks',%0A        method: 'POST',%0A        headers: %7B%0A            'Content-Type': 'application/json',%0A            'Content-Length': Buffer.byteLength(postData),%0A            'User-Agent': 'Ramis-App'%0A        %7D%0A    %7D;%0A%0A    var request = https.request(options, function(response) %7B%0A        response.setEncoding('utf8');%0A    %7D);%0A%0A    request.write(postData);%0A    request.end();%0A%7D%0A

add function to add webhook