Skip to content

Commit

Permalink
Got codeplex integration working to pull down code from repos.
Browse files Browse the repository at this point in the history
  • Loading branch information
abe545 committed Jun 1, 2014
1 parent ac69d6a commit 0339f8b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 66 deletions.
86 changes: 37 additions & 49 deletions lib/codeplex.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
var https = require('https')
, TokenProvider = require('refresh-token')
, git = require('git-node');
var TokenProvider = require('refresh-token')
, git = require('git-node')
, request = require('request');

module.exports = {
getRepos: function(account, appConfig, callback) {
getCodeplex(account, appConfig, '/api/user/projects', callback);
},
getBranches: function(account, config, callback) {
var sourceType = config.sourceType;
if (sourceType === 'git') {
var remote = git.remote(config.sourceUrl);
if (config.type === 'git') {
var remote = git.remote(config.url);
remote.discover(function(err, refs) {
if (err) {
callback(err, null);
Expand All @@ -29,11 +28,11 @@ module.exports = {
});
}
});
} else if (sourceType === 'mercurial') {
request(config.sourceUrl + '/branches?style=raw', function(error, response, body) {
} else if (config.type === 'mercurial') {
request(config.url + '/branches?style=raw', function(error, response, body) {
if (error) return callback(error, null);
var branches = [];
var rows = rawText.split(/\r\n|\r|\n/g);
var rows = body.split(/\r\n|\r|\n/g);

// ignore the last row... it is always blank
for (var i = rows.length - 2; i >= 0; i--) {
Expand All @@ -54,54 +53,43 @@ module.exports = {
function getCodeplex(account, appConfig, resourcePath, callback) {

var options = {
hostname: 'www.codeplex.com',
path: resourcePath,
method: 'GET',
headers: {'x-ms-version': '2012-09-01' }
uri: 'https://www.codeplex.com' + resourcePath,
headers: {'x-ms-version': '2012-09-01' },
json: true
};

if (account && account.accessToken) {
options.headers.Authorization = 'Bearer ' + account.accessToken;
}

var req = https.request(options, function(res) {
var json = '';
res.on('data', function(d) {
json += d;
});
res.on('error', function(err) {
callback(err, null);
});
res.on('end', function() {
var fin = JSON.parse(json);
request(options, function(err, response, body) {
if (err) {
return callback(err);
}

// this is an error message
if (fin.Message) {
if (appConfig.clientId && appConfig.clientSecret && fin.Message === 'Authentication required.') {
var tokenProvider = new TokenProvider('https://www.codeplex.com/oauth/token', {
refresh_token: account.refreshToken,
client_id: appConfig.clientId,
client_secret: appConfig.clientSecret
});
// this is an error message
if (body.Message) {
if (appConfig.clientId && appConfig.clientSecret && body.Message === 'Authentication required.') {
var tokenProvider = new TokenProvider('https://www.codeplex.com/oauth/token', {
refresh_token: account.refreshToken,
client_id: appConfig.clientId,
client_secret: appConfig.clientSecret
});

tokenProvider.getToken(function (err, token) {
if (err) { callback(err, null); }
else {
account.accessToken = token;
getCodeplex(account, appConfig, resourcePath, callback);
}
});
}
else {
callback(fin.Message, null);
}
tokenProvider.getToken(function (er, token) {
if (err) { callback(er); }
else {
account.accessToken = token;
getCodeplex(account, appConfig, resourcePath, callback);
}
});
}
else { callback(null, fin); }
});
});

req.on('error', function(err) {
callback(err, null);
else {
callback(body.Message, null);
}
}
else {
callback(null, body);
}
});
req.end();
}
33 changes: 25 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "codeplex-for-strider",
"version": "0.0.0",
"name": "strider-codeplex",
"version": "0.0.2",
"description": "Provides access to codeplex for strider",
"repository": {
"type": "git",
"url": "https://github.com/abe545/codeplex-for-strider.git"
"url": "https://github.com/abe545/strider-codeplex.git"
},
"keywords": [
"codeplex",
Expand All @@ -16,12 +16,29 @@
"license": "GPL-2.0",
"readmeFilename": "Readme.md",
"dependencies": {
"git-node": "0.1.1",
"passport-oauth2": "1.x.x",
"refresh-token": "0.0.2"
"git-node": "~0.1.1",
"hg": "~0.0.6",
"passport-oauth2": "~1.1.2",
"refresh-token": "~0.0.2",
"strider-git": "~0.2.0"
},
"bugs": {
"url": "https://github.com/abe545/codeplex-for-strider/issues"
"url": "https://github.com/abe545/strider-codeplex/issues"
},
"homepage": "https://github.com/abe545/codeplex-for-strider"
"homepage": "https://github.com/abe545/strider-codeplex",
"strider": {
"id": "codeplex",
"title": "Codeplex",
"type": "provider",
"hosted": true,
"config": {
"controller": "CodeplexController"
},
"accountConfig": {
"setupLink": "/ext/codeplex/oauth"
},
"webapp": "webapp.js",
"worker": "worker.js",
"inline_icon": "windows"
}
}
27 changes: 18 additions & 9 deletions webapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ module.exports = {
},

config: {
sourceType: String,
sourceUrl: String,
repoName: String
type: String,
url: String,
repo: String,
auth: {}
},

// oauth global routes
Expand Down Expand Up @@ -54,7 +55,9 @@ module.exports = {
});
},

isSetup: function (account) {},
isSetup: function (account) {
return account.accessToken;
},

setupRepo: function (account, config, project, done) {
if (!account.accessToken) return done(new Error('Codeplex account not configured'));
Expand All @@ -69,9 +72,12 @@ module.exports = {
getBranches: function (account, config, project, done) {
codeplex.getBranches(account, config, done);
},


// native git, hg, and codeplex all don't have a great way to just get a single file at an arbitrary revision
fastFile: false,
getFile: function (filename, ref, account, config, project, done) {
}
done(new Error('not implemented'));
},
}

function validateAuth(req, accessToken, refreshToken, parms, profile, done) {
Expand Down Expand Up @@ -118,9 +124,12 @@ function parseRepo(account, repo) {
group: repo.Role,
'private': !repo.IsPublished,
config: {
sourceType: repo.SourceControl.ServerType,
sourceUrl: repo.SourceControl.Url,
repoName: repo.Name
type: repo.SourceControl.ServerType.toLowerCase(),
url: repo.SourceControl.Url,
repo: repo.Name,
auth: {
type: 'https'
}
}
}
}
48 changes: 48 additions & 0 deletions worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var git = require('strider-git/worker'),
hg = require('hg');

module.exports = {
init: function (dirs, account, config, job, done) {
return done(null, {
config: config,
account: account,
fetch: function (context, done) {
module.exports.fetch(dirs.data, account, config, job, context, done);
}
});
},
fetch: function (dest, account, config, job, context, done) {
if (config.type === 'git') {
if (config.auth.type === 'https' && !config.auth.username) {
config.auth.username = account.accessToken
config.auth.password = ''
}
git.fetch(dest, config, job, context, done);
} else if (config.type === 'mercurial') {
var branch = job.ref.branch;
if (branch === 'master') {
// hg's default branch is default, not master... Ideally this would be handled in the repo setup, but use this as a workaround
branch = 'default';
}
var start = new Date()
, cmd = 'hg clone ' + config.url + ' . -b ' + branch;
context.status('command.start', { command: cmd, time: start, plugin: context.plugin });
hg.clone(config.url, dest, {"-b": branch}, function(err, output) {
var end = new Date()
, elapsed = end.getTime() - start.getTime();
if (err) {
context.out('hg error: ' + err.message);
}

if (output) {
output.forEach(function(line) {
context.out(line.body);
});
}

context.status('command.done', {exitCode: 0, time: end, elapsed: elapsed});
done(err, output);
});
}
}
}

0 comments on commit 0339f8b

Please sign in to comment.