Feature: List repositories

As a vision user
I want to see a list of all repositories for a GitHub account
So that I can select and monitor repositories for my project

Let's add a test to ./test/github.js for our feature List repositories. This resource will GET all repositories for a project from the route project/:id/repos and return a 200 Ok status:

describe('when requesting an available resource /project/:id/repos', function(){
  it('should respond with 200', function(done){
    this.timeout(5000);
    request(app)
    .get('/project/' + id + '/repos/')
    .expect('Content-Type', /json/)
    .expect(200)
    .end(function (err, res) {
      var repo = _.first(JSON.parse(res.text))
      assert(_.has(repo, 'id'));
      assert(_.has(repo, 'name'));
      assert(_.has(repo, 'description'));
      done();
    });
  });
});

The first thing we need to do is create a GitHubRepo module in ./lib/github/index.js. We start by importing the required modules including github. We define a constructor function that accepts as input a GitHub access token and a user. We then instantiate a GitHubApi module, calling github.authenticate, which authenticates based on the token:

var GitHubApi = require("github")
, config = require('../configuration')
, async =  require("async")
, moment = require('moment')
, _ =  require("underscore")

function GitHubRepo(token, user) {
  this.token = token;
  this.user = user;

  this.github = new GitHubApi({
    version: "3.0.0",
    timeout: 5000 });

  this.github.authenticate({
    type: "oauth",
    token: token
  });
};

module.exports = GitHubRepo;

Let's implement the feature List repositories and add it to our new GitHubRepo module in ./lib/github/index.js. We start by defining our prototype function repositories. We call getAll on the github module. If we get an error, we return the error; if no repositories are found we return a null value. If we find repositories, we use the map function to create a new array of items using the underscore pick function to select the three attributes id, name, and description. We return these items via callback:

GitHubRepo.prototype.repositories = function(callback) {
  this.github.repos.getAll({}, function(error, response) {
    if (error) return callback(error, null);
    if (response == null) return callback(null, null);

    var items = response.map(function(model) {
      return _.pick(model, ['id','name', 'description']);
    });

    callback(null, items);
  });
};

Let's add a repos function to ./lib/project/index.js. We start by importing the GitHubRepo module and we attempt to retrieve the project by calling the static function Project.findOne. If we get an error, we return the error; if the project does not exist we return a null value. If we find the project, we create a GithubRepo module and initialize it with a token and a user, and assign it to git. We then call git.repositories which returns a response. If we get an error, we return an error, if we do not find any repositories, we return a null value. If we find repositories, we use the map function to create a new array of items using underscore pick function to select three attributes, including id, name, and description. We add a fourth attribute, enabled, which signifies if our project has the repository assigned to it and returns all the repositories:

, GitHubRepo = require('../github')

Project.prototype.repos = function(id, callback){
  ProjectSchema.findOne({_id: id}, function(error, project) {
    if (error) return callback(error, null);
    if (project == null) return callback(null, null);

    var git = new GitHubRepo(project.token, project.user);

    git.repositories(function(error, response){
      if (error) return callback(error, null);
      if (response == null) return callback("error", null);

      items = response.map(function(model) {
        var item = _.pick(model, ['id','name', 'description''description']);
        var enabled = _.find(project.repositories, function(p){ return p == item.name; });
        (enabled) ? item.enabled = 'checked' : item.enabled = '';
        return item;
      });

      return callback(null, items);
    });
  });
};

Let's add a new route repos to ./lib/routes/github.js. We instantiate a new ProjectService and then attempt to retrieve the projects repositories by calling the function Project.repos. If we get an error, we return 500 Internal Server Error. If no repositories are returned, we return a 404 Not Found status. If we receive repositories, we return a 200 OK status with the repositories.

, ProjectService = require('../project')
, Project = new ProjectService();

exports.repos = function(req, res){
  logger.info('Request.' + req.url);

  Project.repos(req.params.id, function(error, repos) {
    if (error) return res.json(500, 'Internal Server Error'),
    if (repos == null) return res.json(404, 'Not Found'),
    return res.json(200, repos);
  });
};

Now, add the following route to ./lib/express/index.js:

app.get('/project/:id/repos', github.repos);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.195.107