From 76ed401ba6286fb33b4229787a6b04b273c61181 Mon Sep 17 00:00:00 2001 From: Adrien Walkowiak Date: Sun, 6 Mar 2016 08:35:48 -0600 Subject: [PATCH 1/2] Add runBackup function --- lib/jira.js | 110 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 12 deletions(-) diff --git a/lib/jira.js b/lib/jira.js index 190e2661..d708ebb1 100644 --- a/lib/jira.js +++ b/lib/jira.js @@ -1268,7 +1268,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor }); }; - + // ## Add component to Jira ## // ### Takes ### // @@ -1289,29 +1289,29 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor json: true, body: component }; - + this.doRequest(options, function (error, response, body) { - + if (error) { callback(error, null); return; } - + if (response.statusCode === 400) { callback(body); return; } - + if ((response.statusCode !== 200) && (response.statusCode !== 201)) { callback(response.statusCode + ': Unable to connect to JIRA during search.'); return; } - + callback(null, body); }); }; - + // ## Delete component to Jira ## // ### Takes ### // @@ -1331,19 +1331,19 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor followAllRedirects: true, json: true }; - + this.doRequest(options, function (error, response) { - + if (error) { callback(error, null); return; } - + if (response.statusCode === 204) { callback(null, "Success"); return; } - + callback(response.statusCode + ': Error while deleting'); }); @@ -1656,7 +1656,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor callback("Invalid Fields: " + JSON.stringify(body)); return; }; - + callback(response.statusCode + ': Error while adding comment'); }); }; @@ -2139,4 +2139,90 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor }); }; + /** + * Creates a new backup of JIRA and returns its URI + * User needs to be in the administrator group to run backups and access webdav + * + * @param callback + */ + this.runBackup = function(callback) { + + //build today's backup file URI + var today = new Date().toISOString().replace(/T.+/, '').replace(/-/g,''); + var todayBackupUri = this.makeUri('webdav/backupmanager/JIRA-backup-'+today+'.zip', '', ''); + var runBackupUri = this.makeUri('/runbackup', 'rest/obm/', '1.0'); + + //prepare a HEAD request to check if today's backup exists + var optionsTodayBackup = { + uri: todayBackupUri, + method: 'HEAD', + encoding: null, + json:false + }; + + this.doRequest(optionsTodayBackup, function(error, response) { + if (error) { + callback(error, reponse); + return; + } + + if (response.statusCode === 404) { + //today's backup does not exists - we need to run the backup + options = { + rejectUnauthorized: this.strictSSL, + uri: runBackupUri, + method: 'POST', + followAllRedirects: true, + json:true, + body: { + cbAttachments: true + } + }; + + this.doRequest(options, function(error, response) { + + if (error) { + callback(error, response); + return; + } + + if (response.statusCode === 404) { + callback('Invalid URL: '+options.uri, response); + return; + } + + if (response.statusCode !== 200) { + //if another backup has been made less than 48h ago in JIRA cloud + //a 500 is returned here with an explicit body message + callback(response.statusCode + ': Unable to run backup.', response); + return; + } else { + //check that the backup does exist now + this.doRequest(optionsTodayBackup, function(error, response) { + if (error, reponse) { + callback(error); + return; + } + + if (response.statusCode === 200) { + callback(null, todayBackupUri); + return; + } else { + callback('Unable to download file: '+todayBackupUri, reponse); + return; + } + }.bind(this)); + } + }); + } else { + if (response.statusCode === 200) { + return callback(null, todayBackupUri); + } else { + callback(options.statusCode+' error received', response); + return; + } + } + }.bind(this)); + } + }).call(JiraApi.prototype); From 4d36c78ca56ccc5ddd997f7454b9a790784fdbd6 Mon Sep 17 00:00:00 2001 From: Adrien Walkowiak Date: Sun, 6 Mar 2016 08:50:13 -0600 Subject: [PATCH 2/2] fix whitespace trimming issues --- lib/jira.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/jira.js b/lib/jira.js index d708ebb1..577b79e9 100644 --- a/lib/jira.js +++ b/lib/jira.js @@ -1268,7 +1268,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor }); }; - + // ## Add component to Jira ## // ### Takes ### // @@ -1289,29 +1289,29 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor json: true, body: component }; - + this.doRequest(options, function (error, response, body) { - + if (error) { callback(error, null); return; } - + if (response.statusCode === 400) { callback(body); return; } - + if ((response.statusCode !== 200) && (response.statusCode !== 201)) { callback(response.statusCode + ': Unable to connect to JIRA during search.'); return; } - + callback(null, body); }); }; - + // ## Delete component to Jira ## // ### Takes ### // @@ -1331,19 +1331,19 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor followAllRedirects: true, json: true }; - + this.doRequest(options, function (error, response) { - + if (error) { callback(error, null); return; } - + if (response.statusCode === 204) { callback(null, "Success"); return; } - + callback(response.statusCode + ': Error while deleting'); }); @@ -1656,7 +1656,7 @@ var JiraApi = exports.JiraApi = function(protocol, host, port, username, passwor callback("Invalid Fields: " + JSON.stringify(body)); return; }; - + callback(response.statusCode + ': Error while adding comment'); }); };