Skip to content

Commit

Permalink
added /pull endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Aug 7, 2012
1 parent 3aedbd5 commit 1adc87f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
31 changes: 30 additions & 1 deletion simple-git-server.js
@@ -1,5 +1,6 @@
var _ = require('underscore'),
path = require('path'),
url = require('url'),
express = require('express');

function username(email) {
Expand Down Expand Up @@ -82,6 +83,32 @@ function makeListHandler(git) {
};
}

function makePullHandler(git) {
return function(req, res) {
if (!req.user)
return res.send(403);

if (!(typeof(req.body.repository) == 'string'))
return res.send('repository expected', 400);
var repo = url.parse(req.body.repository);
if (!(repo.protocol == "git:" || repo.protocol == "http:" ||
repo.protocol == "https:"))
return res.send('invalid repository', 400);
if (!(typeof(req.body.refspec) == 'string'))
return res.send('refspec expected', 400);

git.pull({
repository: req.body.repository,
refspec: req.body.refspec,
email: req.user.email,
name: username(req.user.email) + ' from ' + req.user.origin
}, function(err) {
if (err) return res.send('an unknown error occurred', 500);
return res.send(200);
});
}
}

module.exports = function SimpleGitServer(config) {
var git = config.git;
var bic = config.browserIDCORS || require('browserid-cors')();
Expand All @@ -92,14 +119,16 @@ module.exports = function SimpleGitServer(config) {
self.handleCommit = makeCommitHandler(git, function postCommit(git) {
if (git.cmd) git.cmd(['update-server-info']);
});
self.handlePull = makePullHandler(git);

self.use(express.bodyParser());
self.use(bic.accessToken);
self.use(bic.fullCORS);
self.post('/token', bic.handleTokenRequest);
self.post('/commit', self.handleCommit);
self.get('/ls', self.handleList);

self.post('/pull', self.handlePull);

if (git.abspath)
self.use('/static', express.static(git.abspath()));

Expand Down
76 changes: 76 additions & 0 deletions test/test-simple-git-server.js
Expand Up @@ -288,6 +288,82 @@ describe("SimpleGitServer", function() {
.expect(200, {files: ['bloop/bap.js']}, done);
});

it("should fail when pulling w/ no repository", function(done) {
request(cfg(SimpleGitServer({git: {}})))
.post('/pull')
.set('X-Access-Token', 'abcd')
.send()
.expect(400, "repository expected", done);
});

it("should fail when pulling w/ invalid repository", function(done) {
request(cfg(SimpleGitServer({git: {}})))
.post('/pull')
.set('X-Access-Token', 'abcd')
.send({repository: 'lol'})
.expect(400, "invalid repository", done);
});

it("should fail when pulling w/ no refspec", function(done) {
request(cfg(SimpleGitServer({git: {}})))
.post('/pull')
.set('X-Access-Token', 'abcd')
.send({repository: 'http://blah.org/myrepo'})
.expect(400, "refspec expected", done);
});

it("should fail when pulling w/ no token", function(done) {
request(cfg(SimpleGitServer({git: {}})))
.post('/pull')
.send()
.expect(403, done);
});

it("should return 500 on pull failure", function(done) {
request(cfg(SimpleGitServer({
git: {
pull: function(options, cb) {
cb('fail fail fail!');
}
}
})))
.post('/pull')
.set('X-Access-Token', 'abcd')
.send({
repository: 'http://blah.org/myrepo',
refspec: 'master'
})
.expect(500, 'an unknown error occurred', done);
});

it("should pull", function(done) {
var pullOptions;
request(cfg(SimpleGitServer({
git: {
pull: function(options, cb) {
pullOptions = options;
cb(null);
}
}
})))
.post('/pull')
.set('X-Access-Token', 'abcd')
.send({
repository: 'http://blah.org/myrepo',
refspec: 'master'
})
.expect(200, function(err) {
if (err) return done(err);
expect(pullOptions).to.eql({
repository: 'http://blah.org/myrepo',
refspec: 'master',
email: 'foo@foo.org',
name: 'foo from http://bar.org'
});
done();
});
});

it("should have /token endpoint", function(done) {
request(cfg(SimpleGitServer({git: {}})))
.post('/token')
Expand Down

0 comments on commit 1adc87f

Please sign in to comment.