import angular from 'angular';

const apiHost = 'https://api.github.com';
const eventLimit = 2;

class GithubAPI {

  /* @ngInject */
  constructor($http) {
    this.$http = $http;
  }

  getActivity(user, repository, limit, enableCommitStatus) {
    if (!user || user === angular.isUndefined()) {
      return Promise.reject('No user was supplied');
    }

    let urlArray = [apiHost];
    if (repository) {
      urlArray = urlArray.concat(['repos', user, repository]);
    } else {
      urlArray = urlArray.concat(['users', user]);
    }
    urlArray.push('events');
    return this.$http.get(urlArray.join('/'))
      .then((response) => this.handleResponse(response, limit, enableCommitStatus))
      .catch((error) => {
        console.error('XHR Failed for getActivity.\n', angular.toJson(error.data, true));
      });
  }

  handleResponse(response, limit, enableCommitStatus) {
    const maxEvents = limit || eventLimit;
    const events = [];

    for (const event of response.data) {
      if (events.length < maxEvents) {
        if (event.type === 'PushEvent') {
          if (enableCommitStatus) {
            this.getStatus(event);
          }
          events.push(event);
        }
      } else {
        return events;
      }
    }
    return events;
  }

  getStatus(event) {
    const sha = event.payload.commits[event.payload.commits.length - 1].sha;
    const url = 'https://api.github.com/repos/' + event.repo.name + '/commits/' + sha + '/statuses';

    return this.$http.get(url)
      .then((response) => {
        event.status = response.data[0] || {};
        return event;
      })
      .catch((error) => {
        console.error('XHR Failed for getStatus.\n', angular.toJson(error.data, true));
      });
  }
}

export default GithubAPI;

@@ -175,24 +175,870 @@
 $http;%0A  %7D%0A%0A
+  /**%0A   * Hit the Github api for the repositories latest events.%0A   * If enableCommitStatus is true then make additional Github requests%0A   * for the status of the first commit.%0A   *%0A   * If no #repository is passed in then api.github.com/users/:user/events will be hit,%0A   * which will get all events by that user.  Even for :repo, /events is chosen as it lists%0A   * several commits per event.%0A   *%0A   * NOTE: If a repository is big enough and gets more Watch/Star events then there might%0A   * not be any PushEvents for this to catch. TODO maybe switch to /commits?%0A   *%0A   * TODO: Refactor to accept the repo as %7B':user/:repo'%7D%0A   *%0A   * @param string  user               Github username%0A   * @param string  repository         The repository to check%0A   * @param boolean enableCommitStatus Enable the checking of statuses for the commit%0A   */%0A
   getActivit
@@ -1672,16 +1672,480 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Handle the response from the Github api.%0A   * Filter through the commits and find ones that are PushEvents and have%0A   * distinct commits.  If #enableCommitStatus is true, then check the status%0A   * of the commit.%0A   *%0A   * @param object  response           The %7B@link $http%7D response object%0A   * @param number  limit              The maximum number of events to store%0A   * @param boolean enableCommitStatus Check the status of the event's commit%0A   */%0A
   handle
@@ -2586,26 +2586,306 @@
 %0A%0A  
-getStatus(event) %7B
+/**%0A   * Hit the Github commit api to check the status of a commit.%0A   * Status is set by your automated build services (Travis, drone, jenkins, etc.)%0A   *%0A   * @param object  event The push event containing the commits%0A   */%0A  getStatus(event) %7B%0A    // Get the SHA of the first commit in the event
 %0A   
@@ -3060,16 +3060,60 @@
 uses';%0A%0A
+    // Get the first status from the result%0A
     retu

Add comments to github service