diff --git a/dana-bot/src/bot.js b/dana-bot/src/bot.js index 5d8a939..93060f2 100644 --- a/dana-bot/src/bot.js +++ b/dana-bot/src/bot.js @@ -133,7 +133,7 @@ function loadContext() { for (var ii = 0; ii < keys.length; ii++) { if (globalBot.repositories[globalBot.tasks[keys[ii]].repository] === undefined) { console.log("loadContext -- ERROR - task repository is invalid"); - console.log("loadContext", tasks[keys[ii]], repositories); + console.log("loadContext", globalBot.tasks[keys[ii]], globalBot.repositories); process.exit(1); } } diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index c45cbc9..f68bf83 100644 --- a/dana-bot/src/gitForBot.js +++ b/dana-bot/src/gitForBot.js @@ -97,8 +97,8 @@ module.exports = { var cmdout = run.execSync('git', ['fetch'], currentRepoPath); if (cmdout.err) { logger.error('cannot do git fetch', currentRepoPath); - return; } + return cmdout; }, pull: function() { if (currentRepoPath === undefined) { @@ -344,5 +344,36 @@ module.exports = { l.push(entry); } return l; + }, + checkRemoteBranchExists: function (branchName, repoName) { + var args = ['branch', '-r', '--list', branchName] + var cmdout = run.execSync('git', args, repoPath + '/' + repoName); + if (cmdout.err) { + console.log('ERR', cmdout.code); + return false; + } + + var result = cmdout.stdout.trim().length !== 0; + if (!result) { + logger.error("Couldn't find remote branch '" + branchName + "'"); + } + + return result; + }, + checkCommitExists: function (branchName, commit, repoName) { + var args = ['branch', '-r', '--contains', commit]; + var cmdout = run.execSync('git', args, repoPath + '/' + repoName); + if (cmdout.err) { + console.log('ERR', cmdout.code); + return false; + } + + var lines = cmdout.stdout.split('\n'); + for(var i = 0;i < lines.length;i++){ + if (lines[i].trim() === branchName) { + return true; + } + } + return false; } }; diff --git a/dana-bot/src/www.js b/dana-bot/src/www.js index 8be4eb9..fd4ba87 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -21,6 +21,7 @@ var cwd = process.cwd(); var fs = require('fs'); var util = require('util'); var sendMail = require(cwd + '/src/sendMail'); +var gitForBot = require('./gitForBot'); var WWW_KILL = 1; var WWW_KILL_AND_UPDATE = 2; @@ -381,6 +382,30 @@ app.get('/bot/editRepository', }); }); + +function fetchRepo(req, res, repoName, serviceName) { + gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); + gitForBot.setRepo(globalWWW.config.globalBot.repositories[repoName]); + + if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + repoName)) { + console.log('/bot/' + serviceName + ' -- Cloning repo ' + repoName + ' in ' + globalWWW.config.globalBot.repoPath); + var cloneOut = gitForBot.clone(); + if (cloneOut.err) { + console.log('/bot/' + serviceName + ' -- error while cloning ' + repoName + ': ' + JSON.stringify(cloneOut, null, 2)); + appError(req, res, '/bot/' + serviceName + ', repository of ' + repoName + ' could not be cloned'); + return; + } + } else { + console.log('/bot/' + serviceName + ' -- Fetching repo ' + repoName + ' in ' + globalWWW.config.globalBot.repoPath); + var fetchOut = gitForBot.fetch(); + if (fetchOut.err) { + console.log('/bot/' + serviceName + ' -- error while fetching ' + repoName + ': ' + JSON.stringify(fetchOut, null, 2)); + appError(req, res, '/bot/' + serviceName + ', repository of ' + repoName + ' could not be fetched'); + return; + } + } +} + app.post('/bot/addRepository', // require('connect-ensure-login').ensureLoggedIn(), function(req, res) { @@ -396,12 +421,8 @@ app.post('/bot/addRepository', appError(req, res, '/bot/addRepository, repository of ' + req.body.repoName + ' already exist'); return; } - globalWWW.config.globalBot.repositories[req.body.repoName] = { - name: req.body.repoName, - git: { - url: req.body.giturl - } - } + + globalWWW.config.globalBot.repositories[req.body.repoName] = repository; fs.writeFileSync(cwd + '/configs/repositories.js', JSON.stringify(globalWWW.config.globalBot.repositories)); updateRepositories(); res.redirect('/bot/status'); @@ -500,6 +521,18 @@ app.post('/bot/addTask', return; } + fetchRepo(req, res, req.body.repository, 'addTask') + + if (!gitForBot.checkRemoteBranchExists(req.body.branch, req.body.repository)) { + appError(req, res, '/bot/addTask, branch \'' + req.body.branch + '\' does not exist'); + return; + } + + if (!gitForBot.checkCommitExists(req.body.branch, req.body.base, req.body.repository)) { + appError(req, res, '/bot/addTask, commit \'' + req.body.base + '\' does not exist in branch \'' + req.body.branch + '\''); + return; + } + var active; if (req.body.active === 'Yes') active = true; if (req.body.active === 'No') active = false; @@ -551,6 +584,18 @@ app.post('/bot/saveTask', return; } + fetchRepo(req, res, req.body.repository, 'saveTask') + + if (!gitForBot.checkRemoteBranchExists(req.body.branch, req.body.repository)) { + appError(req, res, '/bot/saveTask, branch \'' + req.body.branch + '\' does not exist'); + return; + } + + if (!gitForBot.checkCommitExists(req.body.branch, req.body.base, req.body.repository)) { + appError(req, res, '/bot/saveTask, commit \'' + req.body.base + '\' does not exist in branch \'' + req.body.branch + '\''); + return; + } + var active; if (req.body.active === 'Yes') active = true; if (req.body.active === 'No') active = false;