Skip to content

Instantly share code, notes, and snippets.

@joshvermaire
Forked from jmreidy/env.sample.js
Created March 12, 2012 18:47
Show Gist options
  • Save joshvermaire/2023909 to your computer and use it in GitHub Desktop.
Save joshvermaire/2023909 to your computer and use it in GitHub Desktop.
Pivotal to Sprintly importer
module.exports = {
pivotal: {
PID: 'PID',
TOKEN: 'TOKEN'
},
sprintly: {
USER: "USER_EMAIL",
ID: 'PRODUCT_ID',
KEY: 'API_KEY'
},
}
var http = require("http"),
https = require("https"),
parser = require("xml2json"),
fs = require("fs"),
qs = require("querystring"),
env = require(__dirname+"/env");
var pivotalOptions = {
host: 'www.pivotaltracker.com',
port: 80,
path: "/services/v3/projects/"+env.pivotal.PID+"/stories",
headers: { "X-TrackerToken": env.pivotal.TOKEN}
};
var sprintlyOptions = {
hostname: 'sprint.ly',
path: '/api/products/'+env.sprintly.ID+'/items.json',
auth: env.sprintly.USER+':'+env.sprintly.KEY,
method: 'POST'
}
var getPivotalStories = function(options) {
if (options.local) {
fs.readFile("pivotalData.xml", "utf-8",function(err,response) {
handleStories(parser.toJson(response, {object:true}));
});
}
else {
var response = '';
http.get(pivotalOptions, function(res) {
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function() {
fs.writeFileSync("pivotalData.xml", response);
handleStories(parser.toJson(response, {object:true}));
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
}
};
var addToSprintly = function(story) {
var options = sprintlyOptions;
options['headers'] = {
"Content-Type": 'application/x-www-form-urlencoded',
"Content-Length": story.length
};
request = https.request(options, function(res) {
var response = '';
res.on('data', function(chunk) {
response += chunk;
});
res.on('end', function() {
console.log("Story Added!", response);
});
});
request.write(story);
request.end();
}
var handleStories = function(data) {
var stories = data.stories.story;
stories = parseStories(stories);
var j = 0;
var poll = setInterval(function() {
if ( j < stories.length) {
addToSprintly(qs.stringify(stories[j]));
j++;
}
else {
clearInterval(poll);
}
},250);
}
var parseStories = function(stories) {
var whatSearch = new RegExp(/I want (to [\w\s\.\'\"]+)/i);
var whySearch = new RegExp(/In order to ([\w\s\.\'\"]+)/i);
var whoSearch = new RegExp(/As an? (\w+)/i);
var addStory = function(feature) {
var description;
if (feature.description) {
description = feature.description.replace(/(As|I)/g, function(str) {
return ","+str;
});
description = description.replace("-","");
} else {
description = "";
}
return {
type: "story",
who: translate(description, whoSearch),
what: translate(description, whatSearch),
why: "I can "+translate(description, whySearch),
score: parseScore(feature.estimate),
tags: feature.labels
};
}
var addDefect = function(bug) {
return {
type: "defect",
title: bug.name,
tags: bug.labels
}
}
var addTask = function(pivotalTask) {
return {
type: "task" ,
title: pivotalTask.name,
tags: pivotalTask.labels
};
}
var translate = function(description, re) {
var result = re.exec(description);
if (result && result.length > 0) {
return result[1]
}
else {
return "unknown";
}
};
var parseScore = function(score) {
var result = +score['$t']
if (!result) result = "~";
else if (result==1) result = "S"
else if (result==2) result = "M"
else if (result==4) result = "L"
else if (result==8) result = "XL"
return result;
}
return stories.map (function(story) {
switch(story.story_type) {
case 'feature':
return addStory(story);
case 'bug':
return addDefect(story);
case 'chore':
return addTask(story);
}
});
}
getPivotalStories({local:true});
{
"author": "Justin Reidy <[email protected]> (http://rzrsharp.net)",
"name": "pivotal-to-sprintly",
"description": "Convert Pivotal Tracker stories to Sprint.ly stories",
"version": "0.0.1",
"repository": {
"url": ""
},
"engines": {
"node": "~0.6.0"
},
"dependencies": {
"xml2json" : "0.2.x",
"querystring": "0.1.x"
},
"devDependencies": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment