diff --git a/lib/codeplex.js b/lib/codeplex.js index 1580b22..7db0118 100644 --- a/lib/codeplex.js +++ b/lib/codeplex.js @@ -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); @@ -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--) { @@ -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(); } \ No newline at end of file diff --git a/package.json b/package.json index 7762dc6..a46e2d2 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" + } } diff --git a/webapp.js b/webapp.js index 75da0f9..dda1484 100644 --- a/webapp.js +++ b/webapp.js @@ -16,9 +16,10 @@ module.exports = { }, config: { - sourceType: String, - sourceUrl: String, - repoName: String + type: String, + url: String, + repo: String, + auth: {} }, // oauth global routes @@ -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')); @@ -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) { @@ -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' + } } } } \ No newline at end of file diff --git a/worker.js b/worker.js index e69de29..2520a9d 100644 --- a/worker.js +++ b/worker.js @@ -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); + }); + } + } +} \ No newline at end of file