2017-09-20 11:30:07 +02:00
|
|
|
/**
|
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var express = require("express");
|
|
|
|
var runtime;
|
|
|
|
var settings;
|
2017-12-04 12:42:44 +01:00
|
|
|
var needsPermission = require("../../auth").needsPermission;
|
2017-09-20 11:30:07 +02:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: function(_runtime) {
|
|
|
|
runtime = _runtime;
|
|
|
|
settings = runtime.settings;
|
|
|
|
},
|
|
|
|
app: function() {
|
|
|
|
var app = express();
|
|
|
|
|
2017-12-17 00:43:08 +01:00
|
|
|
app.use(function(req,res,next) {
|
|
|
|
if (!runtime.storage.projects) {
|
|
|
|
res.status(404).end();
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
// Projects
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// List all projects
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/", needsPermission("projects.read"), function(req,res) {
|
|
|
|
runtime.storage.projects.listProjects(req.user, req.user).then(function(list) {
|
|
|
|
var active = runtime.storage.projects.getActiveProject(req.user);
|
2017-09-20 11:30:07 +02:00
|
|
|
var response = {
|
|
|
|
projects: list
|
|
|
|
};
|
2017-12-19 01:56:02 +01:00
|
|
|
if (active) {
|
|
|
|
response.active = active.name;
|
|
|
|
}
|
2017-09-20 11:30:07 +02:00
|
|
|
res.json(response);
|
2017-10-07 01:18:20 +02:00
|
|
|
}).catch(function(err) {
|
|
|
|
console.log(err.stack);
|
2017-09-20 11:30:07 +02:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Create project
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/", needsPermission("projects.write"), function(req,res) {
|
|
|
|
runtime.storage.projects.createProject(req.user, req.body).then(function(data) {
|
2017-10-17 11:14:50 +02:00
|
|
|
res.json(data);
|
2017-10-07 01:18:20 +02:00
|
|
|
}).catch(function(err) {
|
|
|
|
console.log(err.stack);
|
2017-09-20 11:30:07 +02:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Update a project
|
2017-12-04 12:42:44 +01:00
|
|
|
app.put("/:id", needsPermission("projects.write"), function(req,res) {
|
2017-09-20 11:30:07 +02:00
|
|
|
//TODO: validate the payload properly
|
|
|
|
if (req.body.active) {
|
2017-12-04 12:42:44 +01:00
|
|
|
var currentProject = runtime.storage.projects.getActiveProject(req.user);
|
2017-12-20 15:37:34 +01:00
|
|
|
if (!currentProject || req.params.id !== currentProject.name) {
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.setActiveProject(req.user, req.params.id).then(function() {
|
2017-09-20 11:30:07 +02:00
|
|
|
res.redirect(303,req.baseUrl + '/');
|
2017-10-07 01:18:20 +02:00
|
|
|
}).catch(function(err) {
|
2017-09-20 11:30:07 +02:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
2017-10-19 22:38:53 +02:00
|
|
|
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
2018-01-08 15:46:38 +01:00
|
|
|
} else if (req.body.initialise) {
|
|
|
|
// Initialised set when creating default files for an empty repo
|
|
|
|
runtime.storage.projects.initialiseProject(req.user, req.params.id, req.body).then(function() {
|
|
|
|
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
|
|
|
}).catch(function(err) {
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
2017-09-20 23:51:28 +02:00
|
|
|
} else if (req.body.hasOwnProperty('credentialSecret') ||
|
|
|
|
req.body.hasOwnProperty('description') ||
|
|
|
|
req.body.hasOwnProperty('dependencies')||
|
2017-10-19 22:38:53 +02:00
|
|
|
req.body.hasOwnProperty('summary') ||
|
2017-11-23 01:27:13 +01:00
|
|
|
req.body.hasOwnProperty('files') ||
|
2017-12-04 12:42:44 +01:00
|
|
|
req.body.hasOwnProperty('git')) {
|
|
|
|
runtime.storage.projects.updateProject(req.user, req.params.id, req.body).then(function() {
|
2017-10-19 22:38:53 +02:00
|
|
|
res.redirect(303,req.baseUrl + '/'+ req.params.id);
|
2017-10-07 01:18:20 +02:00
|
|
|
}).catch(function(err) {
|
2017-09-20 11:30:07 +02:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
2017-10-19 22:38:53 +02:00
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:"invalid_request"});
|
2017-09-20 11:30:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get project metadata
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id", needsPermission("projects.read"), function(req,res) {
|
|
|
|
runtime.storage.projects.getProject(req.user, req.params.id).then(function(data) {
|
2017-09-20 11:30:07 +02:00
|
|
|
if (data) {
|
|
|
|
res.json(data);
|
|
|
|
} else {
|
|
|
|
res.status(404).end();
|
|
|
|
}
|
2017-10-07 01:18:20 +02:00
|
|
|
}).catch(function(err) {
|
2017-09-20 11:30:07 +02:00
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-12-08 17:31:42 +01:00
|
|
|
// Delete project
|
2017-12-04 12:42:44 +01:00
|
|
|
app.delete("/:id", needsPermission("projects.write"), function(req,res) {
|
2017-12-07 15:28:26 +01:00
|
|
|
runtime.storage.projects.deleteProject(req.user, req.params.id).then(function() {
|
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()})
|
|
|
|
});
|
2017-09-20 11:30:07 +02:00
|
|
|
});
|
|
|
|
|
2017-11-22 00:31:41 +01:00
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get project status - files, commit counts, branch info
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/status", needsPermission("projects.read"), function(req,res) {
|
2018-02-09 00:30:07 +01:00
|
|
|
var includeRemote = req.query.remote;
|
|
|
|
|
|
|
|
runtime.storage.projects.getStatus(req.user, req.params.id, includeRemote).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
if (data) {
|
|
|
|
res.json(data);
|
|
|
|
} else {
|
|
|
|
res.status(404).end();
|
|
|
|
}
|
|
|
|
}).catch(function(err) {
|
2018-01-08 15:46:38 +01:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
2017-11-22 00:31:41 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Project file listing
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/files", needsPermission("projects.read"), function(req,res) {
|
|
|
|
runtime.storage.projects.getFiles(req.user, req.params.id).then(function(data) {
|
2018-01-08 15:46:38 +01:00
|
|
|
// console.log("TODO: REMOVE /:id/files as /:id/status is better!")
|
2017-09-20 11:30:07 +02:00
|
|
|
res.json(data);
|
|
|
|
})
|
2017-10-07 01:18:20 +02:00
|
|
|
.catch(function(err) {
|
2018-01-08 15:46:38 +01:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
|
|
|
|
// Get file content in a given tree (index/stage)
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/files/:treeish/*", needsPermission("projects.read"), function(req,res) {
|
2017-10-25 16:26:24 +02:00
|
|
|
var projectId = req.params.id;
|
|
|
|
var treeish = req.params.treeish;
|
|
|
|
var filePath = req.params[0];
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getFile(req.user, projectId,filePath,treeish).then(function(data) {
|
2017-10-25 16:26:24 +02:00
|
|
|
res.json({content:data});
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-12-11 18:05:12 +01:00
|
|
|
// Revert a file
|
|
|
|
app.delete("/:id/files/_/*", needsPermission("projects.write"), function(req,res) {
|
|
|
|
var projectId = req.params.id;
|
|
|
|
var filePath = req.params[0];
|
|
|
|
|
|
|
|
runtime.storage.projects.revertFile(req.user, projectId,filePath).then(function() {
|
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Stage a file
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
|
2017-10-25 16:26:24 +02:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var file = req.params[0];
|
2017-10-07 01:18:20 +02:00
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.stageFile(req.user, projectName,file).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
2017-11-23 01:27:13 +01:00
|
|
|
|
|
|
|
// Stage multiple files
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/stage", needsPermission("projects.write"), function(req,res) {
|
2017-10-07 01:18:20 +02:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var files = req.body.files;
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.stageFile(req.user, projectName,files).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Commit changes
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/commit", needsPermission("projects.write"), function(req,res) {
|
2017-10-07 01:18:20 +02:00
|
|
|
var projectName = req.params.id;
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.commit(req.user, projectName,req.body).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
.catch(function(err) {
|
2018-02-05 16:59:11 +01:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Unstage a file
|
2017-12-04 12:42:44 +01:00
|
|
|
app.delete("/:id/stage/*", needsPermission("projects.write"), function(req,res) {
|
2017-10-25 16:26:24 +02:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var file = req.params[0];
|
2017-10-07 01:18:20 +02:00
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.unstageFile(req.user, projectName,file).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
2017-11-23 01:27:13 +01:00
|
|
|
|
|
|
|
// Unstage multiple files
|
2017-12-04 12:42:44 +01:00
|
|
|
app.delete("/:id/stage", needsPermission("projects.write"), function(req, res) {
|
2017-10-07 01:18:20 +02:00
|
|
|
var projectName = req.params.id;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.unstageFile(req.user, projectName).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/status");
|
2017-10-07 01:18:20 +02:00
|
|
|
})
|
|
|
|
.catch(function(err) {
|
2017-09-20 11:30:07 +02:00
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get a file diff
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/diff/:type/*", needsPermission("projects.read"), function(req,res) {
|
2017-10-25 16:26:24 +02:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var type = req.params.type;
|
|
|
|
var file = req.params[0];
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getFileDiff(req.user, projectName,file,type).then(function(data) {
|
2017-10-09 01:11:07 +02:00
|
|
|
res.json({
|
|
|
|
diff: data
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get a list of commits
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/commits", needsPermission("projects.read"), function(req, res) {
|
2017-10-10 00:37:19 +02:00
|
|
|
var projectName = req.params.id;
|
2017-11-22 00:31:41 +01:00
|
|
|
var options = {
|
|
|
|
limit: req.query.limit||20,
|
|
|
|
before: req.query.before
|
|
|
|
};
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getCommits(req.user, projectName,options).then(function(data) {
|
2017-10-10 00:37:19 +02:00
|
|
|
res.json(data);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
2017-11-22 00:31:41 +01:00
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
2017-10-10 00:37:19 +02:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get an individual commit details
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/commits/:sha", needsPermission("projects.read"), function(req, res) {
|
2017-10-10 00:37:19 +02:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var sha = req.params.sha;
|
|
|
|
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getCommit(req.user, projectName,sha).then(function(data) {
|
2017-10-10 00:37:19 +02:00
|
|
|
res.json({commit:data});
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Push local commits to remote
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/push/?*", needsPermission("projects.write"), function(req,res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var remoteBranchName = req.params[0]
|
|
|
|
var setRemote = req.query.u;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.push(req.user, projectName,remoteBranchName,setRemote).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2017-11-23 01:27:13 +01:00
|
|
|
|
|
|
|
// Pull remote commits
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/pull/?*", needsPermission("projects.write"), function(req,res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var remoteBranchName = req.params[0];
|
2018-02-09 00:21:14 +01:00
|
|
|
var setUpstream = req.query.setUpstream;
|
|
|
|
var allowUnrelatedHistories = req.query.allowUnrelatedHistories;
|
|
|
|
runtime.storage.projects.pull(req.user, projectName,remoteBranchName,setUpstream,allowUnrelatedHistories).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Abort an ongoing merge
|
2017-12-04 12:42:44 +01:00
|
|
|
app.delete("/:id/merge", needsPermission("projects.write"), function(req, res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.abortMerge(req.user, projectName).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Resolve a merge
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/resolve/*", needsPermission("projects.write"), function(req, res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var file = req.params[0];
|
|
|
|
var resolution = req.body.resolutions;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.resolveMerge(req.user, projectName,file,resolution).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get a list of local branches
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/branches", needsPermission("projects.read"), function(req, res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getBranches(req.user, projectName,false).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.json(data);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2017-11-23 01:27:13 +01:00
|
|
|
|
2017-12-08 17:31:42 +01:00
|
|
|
// Delete a local branch - ?force=true
|
|
|
|
app.delete("/:id/branches/:branchName", needsPermission("projects.write"), function(req, res) {
|
|
|
|
var projectName = req.params.id;
|
|
|
|
var branchName = req.params.branchName;
|
|
|
|
var force = !!req.query.force;
|
|
|
|
runtime.storage.projects.deleteBranch(req.user, projectName, branchName, false, force).then(function(data) {
|
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get a list of remote branches
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/branches/remote", needsPermission("projects.read"), function(req, res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getBranches(req.user, projectName,true).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.json(data);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
2018-02-02 17:26:55 +01:00
|
|
|
console.log(err);
|
2017-11-22 00:31:41 +01:00
|
|
|
if (err.code) {
|
2018-02-02 17:26:55 +01:00
|
|
|
res.status(400).json({error:err.code, message: err.message, remote: err.remote});
|
2017-11-22 00:31:41 +01:00
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Get branch status - commit counts/ahead/behind
|
2017-12-04 12:42:44 +01:00
|
|
|
app.get("/:id/branches/remote/*/status", needsPermission("projects.read"), function(req, res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var branch = req.params[0];
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.getBranchStatus(req.user, projectName,branch).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.json(data);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-11-23 01:27:13 +01:00
|
|
|
// Set the active local branch
|
2017-12-04 12:42:44 +01:00
|
|
|
app.post("/:id/branches", needsPermission("projects.write"), function(req, res) {
|
2017-11-22 00:31:41 +01:00
|
|
|
var projectName = req.params.id;
|
|
|
|
var branchName = req.body.name;
|
|
|
|
var isCreate = req.body.create;
|
2017-12-04 12:42:44 +01:00
|
|
|
runtime.storage.projects.setBranch(req.user, projectName,branchName,isCreate).then(function(data) {
|
2017-11-22 00:31:41 +01:00
|
|
|
res.json(data);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2017-12-20 15:37:34 +01:00
|
|
|
// Get a list of remotes
|
|
|
|
app.get("/:id/remotes", needsPermission("projects.read"), function(req, res) {
|
|
|
|
var projectName = req.params.id;
|
|
|
|
runtime.storage.projects.getRemotes(req.user, projectName).then(function(data) {
|
|
|
|
res.json(data);
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
|
2017-12-20 15:37:34 +01:00
|
|
|
// Add a remote
|
|
|
|
app.post("/:id/remotes", needsPermission("projects.write"), function(req,res) {
|
|
|
|
var projectName = req.params.id;
|
2018-02-02 23:43:29 +01:00
|
|
|
if (/^https?:\/\/[^/]+@/i.test(req.body.url)) {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:"Git http url must not include username/password"});
|
|
|
|
return;
|
|
|
|
}
|
2017-12-20 15:37:34 +01:00
|
|
|
runtime.storage.projects.addRemote(req.user, projectName, req.body).then(function() {
|
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/remotes");
|
|
|
|
}).catch(function(err) {
|
|
|
|
console.log(err.stack);
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
|
2017-12-20 15:37:34 +01:00
|
|
|
// Delete a remote
|
|
|
|
app.delete("/:id/remotes/:remoteName", needsPermission("projects.write"), function(req, res) {
|
|
|
|
var projectName = req.params.id;
|
|
|
|
var remoteName = req.params.remoteName;
|
|
|
|
runtime.storage.projects.removeRemote(req.user, projectName, remoteName).then(function(data) {
|
|
|
|
res.redirect(303,req.baseUrl+"/"+projectName+"/remotes");
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2017-11-22 00:31:41 +01:00
|
|
|
|
2017-12-21 18:40:24 +01:00
|
|
|
// Update a remote
|
|
|
|
app.put("/:id/remotes/:remoteName", needsPermission("projects.write"), function(req,res) {
|
|
|
|
var projectName = req.params.id;
|
|
|
|
var remoteName = req.params.remoteName;
|
|
|
|
runtime.storage.projects.updateRemote(req.user, projectName, remoteName, req.body).then(function(data) {
|
|
|
|
res.status(204).end();
|
|
|
|
})
|
|
|
|
.catch(function(err) {
|
|
|
|
if (err.code) {
|
|
|
|
res.status(400).json({error:err.code, message: err.message});
|
|
|
|
} else {
|
|
|
|
res.status(400).json({error:"unexpected_error", message:err.toString()});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-09-20 11:30:07 +02:00
|
|
|
return app;
|
|
|
|
}
|
|
|
|
}
|