Wizcorp / node-pivotal

NodeJS API library for PivotalTracker
44 stars 20 forks source link

How to get stories from all projects? #10

Closed regedarek closed 10 years ago

regedarek commented 11 years ago

I am trying something like this:

app.get('/delivered_stories', function(request, response) { 
  pivotal.useToken("token");

  function get_stories(project_ids) {
    var array = []
    project_ids.forEach(function(id) {
      pivotal.getStories(340755, { filter: "state:finished" }, function(err, project_stories) {
        console.log("Project stories: ".blue + project_stories);
        array.push(project_stories);
      });
    });
    response.send(array);
  }

  pivotal.getProjects(function (err, data) {
    var project_ids = data.project.map(function(x) { return parseInt(x.id); });
    get_stories(project_ids);
  });
});

Could you help me with that?

stelcheck commented 11 years ago
var pivotal = require("../index.js"),
    async   = require("async"),
    colors  = require("colors"),
    token   = process.env.token || null,
    debug   = (process.env.debug !== undefined);

pivotal.debug = debug;
pivotal.useToken(token);

app.get('/delivered_stories', function(request, response) { 
function get_stories(project_ids) {
    var results = [];

    async.each(project_ids, function(id, cb) {
        pivotal.getStories(id, { filter: "state:finished" }, function(err, project_stories) {
            if (err)  {
                return console.error("project: ", id, ", getStories ERROR", err);
            }

            // this is just in, but empty results will return null
            if (!project_stories) {
                return console.log("Project: ".green, id,  ", stories: ".blue, "0");
            }

            console.log("Project: ".green, id,  ", stories: ".blue, project_stories.story.length);

            results = results.concat(project_stories.story);

            cb();
        });
    }, function () {
        // This will send what you want
        response.send(results);
    }),

    // This will send an empty array - use async, step or something
    // response.send(results);
}

pivotal.getProjects(function (err, data) {

    // check for errors
    if (err)  {
        return console.error("getProject ERROR", err);
    }

    var project_ids = data.project.map(function(x) {
        return parseInt(x.id, 10);
    });

    console.log("ids", project_ids);
    get_stories(project_ids);
});
};