Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dana-bot/src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
33 changes: 32 additions & 1 deletion dana-bot/src/gitForBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
};
57 changes: 51 additions & 6 deletions dana-bot/src/www.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down