'use strict'

const assert = require('assert')
const request = require('request')
const express = require('express')
const bodyParser = require('body-parser')

assert(process.env.GITHUB_TOKEN, 'GITHUB_TOKEN must be set')
assert(process.env.GIT_REPO, 'GIT_REPO must be set')

const API_TOKEN = process.env.GITHUB_TOKEN
const REPO = process.env.GIT_REPO

const app = express()
const travis = request.defaults({
  baseUrl: 'https://api.travis-ci.org',
  json: true
})

app.use(bodyParser.json())

app.get('/', (req, res, next) => {
  res.status(200).json({status: 'OK'})
})

app.get('/build', (req, res, next) => {
  authorize((err, token) => {
    if (err) next(err)
    else triggerBuild(token, (err, data) => {
      if (err) next(err)
      else res.status(200).json(data)
    })
  })
})

function authorize(cb) {
  travis.post('/auth/github', {
    body: {github_token: API_TOKEN},
    headers: {'User-Agent': 'Travis/1.0'}
  }, (err, _res) => {
    if (err) cb(err)
    else cb(null, _res.body.access_token)
  })
}

function triggerBuild(token, cb) {
  let path = 'repo/' + encodeURIComponent(REPO) + '/requests'
  let body = {branch: 'master', message: 'API request'}
  let headers = {
    'User-Agent': 'Travis/1.0',
    'Travis-API-Version': '3',
    'Authorization': `token ${token}`
  }
  travis.post(path, {body, headers}, (err, res) => {
    if (err) cb(null, err)
    else if (!/^2/.test(res.statusCode.toString())) cb(res.body)
    else cb(null, res.body)
  })
}

app.listen(9000, _ => console.log(':9000'))

@@ -344,16 +344,54 @@
 GIT_REPO
+%0Aconst PORT = process.env.PORT %7C%7C 9000
 %0A%0Aconst 
@@ -1518,20 +1518,20 @@
 .listen(
-9000
+PORT
 , _ =%3E c
@@ -1547,12 +1547,15 @@
 g(':
-9000'
+' + PORT
 ))%0A

Read PORT from env if set