From f0f1392cddc7b1d4db362b5522d9cd98fd4e209a Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Tue, 30 Jan 2018 15:13:32 +0100 Subject: [PATCH 01/10] Check remote branch actually exists when editing tasks --- dana-bot/src/gitForBot.js | 21 +++++++++++++++++++++ dana-bot/src/www.js | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index c45cbc9..ba3e2db 100644 --- a/dana-bot/src/gitForBot.js +++ b/dana-bot/src/gitForBot.js @@ -344,5 +344,26 @@ module.exports = { l.push(entry); } return l; + }, + checkRemoteBranchExists: function (branchName) { + currentRepoPath = './tmp/service.repo/R8' + if (currentRepoPath === undefined) { + logger.error('getPatchInfo repository not set'); + return false; + } + var args = ['branch', '-r', '--list', branchName] + var cmdout = run.execSync('git', args, currentRepoPath); + if (cmdout.err) { + console.log('ERR', cmdout.code); + return false; + } + var stdout = cmdout.stdout; + + var result = stdout.trim().length !== 0; + if (!result) { + logger.error("Couldn't find remote branch '" + branchName + "'"); + } + + return (stdout.trim().length !== 0); } }; diff --git a/dana-bot/src/www.js b/dana-bot/src/www.js index 8be4eb9..2e7053a 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; @@ -551,6 +552,11 @@ app.post('/bot/saveTask', return; } + if (!gitForBot.checkRemoteBranchExists(req.body.branch)) { + appError(req, res, '/bot/saveTask, branch \'' + req.body.branch + '\' undefined'); + return; + } + var active; if (req.body.active === 'Yes') active = true; if (req.body.active === 'No') active = false; From b660d8070e7f8f833becfb05b16daf8e300964c8 Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Tue, 30 Jan 2018 15:19:14 +0100 Subject: [PATCH 02/10] Remove line used for debug --- dana-bot/src/gitForBot.js | 1 - 1 file changed, 1 deletion(-) diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index ba3e2db..9c1fc2c 100644 --- a/dana-bot/src/gitForBot.js +++ b/dana-bot/src/gitForBot.js @@ -346,7 +346,6 @@ module.exports = { return l; }, checkRemoteBranchExists: function (branchName) { - currentRepoPath = './tmp/service.repo/R8' if (currentRepoPath === undefined) { logger.error('getPatchInfo repository not set'); return false; From eeb854700a296ad015fed6c9e43ff34044772e4b Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Tue, 30 Jan 2018 15:20:57 +0100 Subject: [PATCH 03/10] Return result in LV --- dana-bot/src/gitForBot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index 9c1fc2c..3c4a660 100644 --- a/dana-bot/src/gitForBot.js +++ b/dana-bot/src/gitForBot.js @@ -363,6 +363,6 @@ module.exports = { logger.error("Couldn't find remote branch '" + branchName + "'"); } - return (stdout.trim().length !== 0); + return result; } }; From 19c5b437097fe2cea16165d85cead91f7d4a8aed Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Tue, 30 Jan 2018 15:24:11 +0100 Subject: [PATCH 04/10] Remove useless LV --- dana-bot/src/gitForBot.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index 3c4a660..b467913 100644 --- a/dana-bot/src/gitForBot.js +++ b/dana-bot/src/gitForBot.js @@ -356,9 +356,8 @@ module.exports = { console.log('ERR', cmdout.code); return false; } - var stdout = cmdout.stdout; - - var result = stdout.trim().length !== 0; + + var result = cmdout.stdout.trim().length !== 0; if (!result) { logger.error("Couldn't find remote branch '" + branchName + "'"); } From e70ee9239275b6f9287b4cdf1974dfc0bedfdd16 Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Tue, 30 Jan 2018 18:23:04 +0100 Subject: [PATCH 05/10] Check task base commit exists in specified branch --- dana-bot/src/gitForBot.js | 26 +++++++++++++++++++------- dana-bot/src/www.js | 11 +++++++++-- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index b467913..0837497 100644 --- a/dana-bot/src/gitForBot.js +++ b/dana-bot/src/gitForBot.js @@ -345,23 +345,35 @@ module.exports = { } return l; }, - checkRemoteBranchExists: function (branchName) { - if (currentRepoPath === undefined) { - logger.error('getPatchInfo repository not set'); - return false; - } + checkRemoteBranchExists: function (branchName, repoName) { var args = ['branch', '-r', '--list', branchName] - var cmdout = run.execSync('git', args, currentRepoPath); + 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 2e7053a..40779f5 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -552,8 +552,15 @@ app.post('/bot/saveTask', return; } - if (!gitForBot.checkRemoteBranchExists(req.body.branch)) { - appError(req, res, '/bot/saveTask, branch \'' + req.body.branch + '\' undefined'); + var task = globalWWW.config.globalBot.tasks[req.body.taskName]; + + if (!gitForBot.checkRemoteBranchExists(req.body.branch, task.repository)) { + appError(req, res, '/bot/saveTask, branch \'' + req.body.branch + '\' does not exist'); + return; + } + + if (!gitForBot.checkCommitExists(req.body.branch, req.body.base, task.repository)) { + appError(req, res, '/bot/saveTask, commit \'' + req.body.base + '\' does not exist in branch \'' + req.body.branch + '\''); return; } From 5d320bd9a2862f3fa7a41433565d0253d3e65467 Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Wed, 31 Jan 2018 15:55:49 +0100 Subject: [PATCH 06/10] Clone repo as soon as it is added This way, checks on task form entries concerning branch or commit id can work. --- dana-bot/src/bot.js | 2 +- dana-bot/src/www.js | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) 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/www.js b/dana-bot/src/www.js index 40779f5..0be3d37 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -397,12 +397,19 @@ app.post('/bot/addRepository', appError(req, res, '/bot/addRepository, repository of ' + req.body.repoName + ' already exist'); return; } - globalWWW.config.globalBot.repositories[req.body.repoName] = { + var repository = { name: req.body.repoName, git: { url: req.body.giturl } } + if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + req.body.repoName)) { + console.log('/bot/addRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); + gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); + gitForBot.setRepo(repository); + gitForBot.clone(); + } + 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'); @@ -429,6 +436,18 @@ app.post('/bot/saveRepository', return; } + if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + req.body.repoName)) { + console.log('/bot/addRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); + gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); + gitForBot.setRepo({ + name: req.body.repoName, + git: { + url: req.body.giturl + } + }); + gitForBot.clone(); + } + globalWWW.config.globalBot.repositories[req.body.repoName].git.url = req.body.giturl; fs.writeFileSync(cwd + '/configs/repositories.js', JSON.stringify(globalWWW.config.globalBot.repositories)); updateRepositories(); @@ -554,12 +573,12 @@ app.post('/bot/saveTask', var task = globalWWW.config.globalBot.tasks[req.body.taskName]; - if (!gitForBot.checkRemoteBranchExists(req.body.branch, task.repository)) { + 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, task.repository)) { + 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; } From 289c3f3d826d1828fd2812014a9a3ff928ed4ea5 Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Tue, 6 Feb 2018 15:31:35 +0100 Subject: [PATCH 07/10] Handle error when cloning repo --- dana-bot/src/www.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/dana-bot/src/www.js b/dana-bot/src/www.js index 0be3d37..d14ff91 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -407,7 +407,12 @@ app.post('/bot/addRepository', console.log('/bot/addRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); gitForBot.setRepo(repository); - gitForBot.clone(); + var cloneOut = gitForBot.clone(); + if (cloneOut.err) { + console.log('/bot/addRepository -- error while cloning ' + req.body.repoName + ': ' + JSON.stringify(cloneOut, null, 2)); + appError(req, res, '/bot/addRepository, repository of ' + req.body.repoName + ' could not be cloned'); + return; + } } globalWWW.config.globalBot.repositories[req.body.repoName] = repository; fs.writeFileSync(cwd + '/configs/repositories.js', JSON.stringify(globalWWW.config.globalBot.repositories)); @@ -437,7 +442,7 @@ app.post('/bot/saveRepository', } if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + req.body.repoName)) { - console.log('/bot/addRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); + console.log('/bot/saveRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); gitForBot.setRepo({ name: req.body.repoName, @@ -445,7 +450,12 @@ app.post('/bot/saveRepository', url: req.body.giturl } }); - gitForBot.clone(); + var cloneOut = gitForBot.clone(); + if (cloneOut.err) { + console.log('/bot/saveRepository -- error while cloning ' + req.body.repoName + ': ' + JSON.stringify(cloneOut, null, 2)); + appError(req, res, '/bot/saveRepository, repository of ' + req.body.repoName + ' could not be cloned'); + return; + } } globalWWW.config.globalBot.repositories[req.body.repoName].git.url = req.body.giturl; From 1367ab906bc3666770c1500d75fbba71c721e6e3 Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Thu, 8 Feb 2018 12:18:37 +0100 Subject: [PATCH 08/10] Add fetch, and make clone when creating/editing task --- dana-bot/src/www.js | 70 ++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/dana-bot/src/www.js b/dana-bot/src/www.js index d14ff91..2165b97 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -382,6 +382,27 @@ app.get('/bot/editRepository', }); }); + +function fetchRepo(req, res, repoName, serviceName) { + if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + repoName)) { + console.log('/bot/' + serviceName + ' -- Cloning repo ' + repoName + ' in ' + globalWWW.config.globalBot.repoPath); + gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); + gitForBot.setRepo(globalWWW.config.globalBot.repositories[repoName]); + 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; + } + 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) { @@ -397,23 +418,7 @@ app.post('/bot/addRepository', appError(req, res, '/bot/addRepository, repository of ' + req.body.repoName + ' already exist'); return; } - var repository = { - name: req.body.repoName, - git: { - url: req.body.giturl - } - } - if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + req.body.repoName)) { - console.log('/bot/addRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); - gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); - gitForBot.setRepo(repository); - var cloneOut = gitForBot.clone(); - if (cloneOut.err) { - console.log('/bot/addRepository -- error while cloning ' + req.body.repoName + ': ' + JSON.stringify(cloneOut, null, 2)); - appError(req, res, '/bot/addRepository, repository of ' + req.body.repoName + ' could not be cloned'); - return; - } - } + globalWWW.config.globalBot.repositories[req.body.repoName] = repository; fs.writeFileSync(cwd + '/configs/repositories.js', JSON.stringify(globalWWW.config.globalBot.repositories)); updateRepositories(); @@ -441,23 +446,6 @@ app.post('/bot/saveRepository', return; } - if (!fs.existsSync(globalWWW.config.globalBot.repoPath + '/' + req.body.repoName)) { - console.log('/bot/saveRepository -- Cloning repo ' + req.body.repoName + ' in ' + globalWWW.config.globalBot.repoPath); - gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); - gitForBot.setRepo({ - name: req.body.repoName, - git: { - url: req.body.giturl - } - }); - var cloneOut = gitForBot.clone(); - if (cloneOut.err) { - console.log('/bot/saveRepository -- error while cloning ' + req.body.repoName + ': ' + JSON.stringify(cloneOut, null, 2)); - appError(req, res, '/bot/saveRepository, repository of ' + req.body.repoName + ' could not be cloned'); - return; - } - } - globalWWW.config.globalBot.repositories[req.body.repoName].git.url = req.body.giturl; fs.writeFileSync(cwd + '/configs/repositories.js', JSON.stringify(globalWWW.config.globalBot.repositories)); updateRepositories(); @@ -530,6 +518,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; @@ -581,7 +581,7 @@ app.post('/bot/saveTask', return; } - var task = globalWWW.config.globalBot.tasks[req.body.taskName]; + 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'); From 66c14817ccce1a16b2e3bcfbda6bf95715801aac Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Thu, 8 Feb 2018 14:17:59 +0100 Subject: [PATCH 09/10] Fix fetchRepo() by always fetching Also make clone() return output of command, so error can be checked by caller. --- dana-bot/src/gitForBot.js | 2 +- dana-bot/src/www.js | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/dana-bot/src/gitForBot.js b/dana-bot/src/gitForBot.js index 0837497..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) { diff --git a/dana-bot/src/www.js b/dana-bot/src/www.js index 2165b97..601b669 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -384,22 +384,24 @@ 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); - gitForBot.setRepoPath(globalWWW.config.globalBot.repoPath); - gitForBot.setRepo(globalWWW.config.globalBot.repositories[repoName]); 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; } - 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; - } + } + + 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; } } From 46295c2283f5c268d6053b50e9591735fc24cd05 Mon Sep 17 00:00:00 2001 From: Jean-Marie Henaff Date: Thu, 8 Feb 2018 15:17:46 +0100 Subject: [PATCH 10/10] Fetch only if repo has not just been cloned in fetchRepo() --- dana-bot/src/www.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/dana-bot/src/www.js b/dana-bot/src/www.js index 601b669..fd4ba87 100644 --- a/dana-bot/src/www.js +++ b/dana-bot/src/www.js @@ -395,13 +395,14 @@ function fetchRepo(req, res, repoName, serviceName) { appError(req, res, '/bot/' + serviceName + ', repository of ' + repoName + ' could not be cloned'); return; } - } - - 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; + } 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; + } } }