Created
February 4, 2017 14:36
-
-
Save pitkane/83b07ee2e5646556713ffe8596501ce7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const router = express.Router(); | |
const path = require('path'); | |
const chalk = require('chalk'); | |
const Git = require('nodegit'); | |
const walk = require('walk'); | |
const fs = require('fs'); | |
const rimraf = require('rimraf'); | |
const _ = require('lodash'); | |
const log = console.log; | |
// const config = require('../config'); | |
const tests = require('./bottests'); // imports: testPerl, testXML, testHTML | |
router.post('/vilkastestbot', (req, res) => { | |
log(`Starting to process request for ${chalk.blue('vilkastestbot')}`) | |
if (!req.body) return res.sendStatus(400); // should be eliminated with middleware | |
const payload = req.body; | |
validateRequest(payload) | |
.then(cleanupStart) | |
.then(() => runTests(payload)) | |
.then(() => { | |
log("Great success"); | |
res.sendStatus(200); | |
}) | |
.catch((err) => { | |
log(chalk.red('Something went terribly wrong, yell to Mikko')); | |
log(err); | |
res.sendStatus(430); | |
}) | |
}); | |
const cleanupStart = () => new Promise((resolve) => { | |
rimraf(path.join(__dirname, "/../tmp/"), () => { | |
log("cleanupStart: rimraffing /tmp folder"); | |
}); | |
setTimeout( () => { | |
log("Startup cleaning done"); | |
resolve(); | |
}, 100); | |
}); | |
const validateRequest = (payload) => new Promise((resolve, reject) => { | |
log("Request validation started"); | |
const action = _.get(payload, 'action'); | |
if( action !== "opened" && action !== "synchronize" ) { | |
reject("Not supported payload action"); | |
} else { | |
resolve(); | |
} | |
}); | |
const runTests = (payload) => new Promise((resolve,reject) => { | |
// dump the repo | |
const cloneUrl = _.get(payload, 'pull_request.head.repo.html_url'); | |
const headRef = _.get(payload, 'pull_request.head.ref'); | |
const headSHA = _.get(payload, 'pull_request.head.sha'); | |
log(`Got URL: ${cloneUrl} with headRef of: ${headRef} aaaand with SHA: ${headSHA}`); | |
const cartridge = 'Verifone'; | |
const builtPath = path.join(__dirname, "/../tmp/", headSHA, "/", cartridge); | |
_fetchRepo(cloneUrl, headRef, headSHA) | |
.then(() => { | |
// collect files | |
let testFiles; | |
try { | |
testFiles = _collectFiles(builtPath); // sync | |
return testFiles; | |
} catch (error) { | |
reject(error); | |
} | |
}).then((files) => { | |
// run tests | |
Promise.all([tests.testHTML(files), tests.testPerl(files), tests.testXML(files)]) | |
.then(() => { // removed parameter returnValues | |
log("All tests resolved."); | |
resolve(); | |
}) | |
.catch((error) => { | |
log("Something critical errored on runtests") | |
reject(error); | |
}); | |
}); | |
}); | |
const _collectFiles = (builtPath) => { | |
log(`Path to walk: ${builtPath}`); | |
log(`I walk a lonely road. The only one that I have ever known (cos i'm really an object, which doesn't exist after this express request)`); | |
const filesWTF = {htmlFiles: [], perlFiles: [], xmlFiles: []}; | |
const walkerOptions = { | |
listeners: { | |
file: (root, fileStats, next) => { | |
if (path.extname(fileStats.name) === '.html') { | |
filesWTF.htmlFiles.push(root + '/' + fileStats.name); | |
} else if (path.extname(fileStats.name) === '.pm') { | |
filesWTF.perlFiles.push(root + '/' + fileStats.name); | |
} else if (path.extname(fileStats.name) === '.xml') { | |
filesWTF.xmlFiles.push(root + '/' + fileStats.name); | |
} | |
next(); | |
} | |
} | |
}; | |
walker = walk.walkSync(builtPath, walkerOptions); // eslint-disable-line | |
log("all done"); | |
// log(filesWTF); | |
return filesWTF; | |
} | |
const _fetchRepo = (url, ref, sha) => new Promise((resolve) => { // removed parameter reject | |
log('Starting to fetch the repo...'); | |
const cloneOptions = {}; | |
cloneOptions.checkoutBranch = ref; | |
cloneOptions.fetchOpts = { | |
callbacks: { | |
certificateCheck: () => { return 1; }, | |
credentials: (url, userName) => { | |
return Git.Cred.sshKeyFromAgent(userName); | |
} | |
} | |
}; | |
log("Downloading the repo, please wait"); | |
// dump it to ref dir | |
Git.Clone(url, "./tmp/" + sha, cloneOptions) | |
.then(() => { // remove parameter repository | |
resolve(); | |
}).catch( (error) => { | |
log(error); | |
}); | |
// log('Repo dumped to /tmp...'); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment