<commit_before>'use strict';

// Dependencies
const Needle = require('needle');
const Q = require('q');

// Declare internals
const Internals = {};

/*
 * Pivotal API module.
 * @param {string} _token pivotaltracker api token
 * @author George <<EMAIL>>
 */
function PivotalAPI (_token) {

   if( !(this instanceof PivotalAPI) ) return new PivotalAPI(_token)
   if (typeof _token == 'undefined') throw new Error('No token provided');


   Internals.baseUrl = 'https://www.pivotaltracker.com/services/v5'
   Internals.options = {
      headers: {
         'X-TrackerToken': _token
      }
      // open_timeout: 20000
   }

   /*
    * Get My Info
    * - Gets pivotal tracker personal info of the token provided
    */
   this.getMyInfo = (callback) => {
      return Internals.apiCall('/me', callback)
   }

   /*
    * Get Accounts
    */
   this.getAccounts = (callback) => {
      return Internals.apiCall('/accounts', callback)
   }

   /*
    * Get Projects
    */
   this.getProjects = (callback) => {
      return Internals.apiCall('/projects', callback)
   }

   /*
    * Get Account Memberships
    */
   this.getAccountMemberships = (id, callback) => {
      return Internals.apiCall(`/accounts/${id}/memberships`, callback)
   }

   /*
    * Get Account Memberships
    */
   this.getAccountsMemberships = (accounts) => {

      var getAccounts = []
      for (var i = 0; i < accounts.length; i++) {
         getAccounts.push(this.getAccountMemberships(accounts[i].id))
      }
      return Q.all(getAccounts)
   }

   /*
    * Get Project Memberships
    */
   this.getProjectMemberships = (id, callback) => {
      return Internals.apiCall(`/projects/${id}/memberships`, callback)
   }

   /*
    * Get Project Memberships
    */
   this.getProjectsMemberships = (projects) => {

      var getProjects = []
      for (var i = 0; i < projects.length; i++) {
         getProjects.push(this.getProjectMemberships(projects[i].id))
      }
      return Q.all(getProjects)
   }

   this.getStories = (options, callback) => {
		const { month, year } = options.filter;
		const { start_month, end_month, start_year, end_year, last_day } = UtilService.getMonthParams(month, year)

		let params = `&accepted_before=${end_year}-${end_month}-01T00:00:00.000Z`
		params += `&accepted_after=${start_year}-${start_month}-${last_day}T00:00:00.000Z`
		const url = `/projects/${options.projectId}/stories?with_state=accepted${params}`

		// return callback(null, {url})
		_apiCall(url, function(err, stories) {

			if (err) return callback(err);
			return callback(null, { size:  stories.length, stories:stories })
		});
	}

	/*
	 * Get Stories' Activities
	 */
	this.getStoryActivities = (projectId, stories) => {
		var getActivities = []

		for (var i = 0; i < stories.length; i++) {
			getActivities.push(this.getStoryActivity(projectId, stories[i].id))
		};

		return Q.all(getActivities)
	}

	/*<commit_msg>change to node v 4<commit_after>'use strict';

// Dependencies
const Needle = require('needle');
const Q = require('q');

// Declare internals
const Internals = {};

/*
 * Pivotal API module.
 * @param {string} _token pivotaltracker api token
 * @author George <<EMAIL>>
 */
function PivotalAPI (_token) {

   if( !(this instanceof PivotalAPI) ) return new PivotalAPI(_token)
   if (typeof _token == 'undefined') throw new Error('No token provided');


   Internals.baseUrl = 'https://www.pivotaltracker.com/services/v5'
   Internals.options = {
      headers: {
         'X-TrackerToken': _token
      }
      // open_timeout: 20000
   }

   /*
    * Get My Info
    * - Gets pivotal tracker personal info of the token provided
    */
   this.getMyInfo = (callback) => {
      return Internals.apiCall('/me', callback)
   }

   /*
    * Get Accounts
    */
   this.getAccounts = (callback) => {
      return Internals.apiCall('/accounts', callback)
   }

   /*
    * Get Projects
    */
   this.getProjects = (callback) => {
      return Internals.apiCall('/projects', callback)
   }

   /*
    * Get Account Memberships
    */
   this.getAccountMemberships = (id, callback) => {
      return Internals.apiCall(`/accounts/${id}/memberships`, callback)
   }

   /*
    * Get Account Memberships
    */
   this.getAccountsMemberships = (accounts) => {

      var getAccounts = []
      for (var i = 0; i < accounts.length; i++) {
         getAccounts.push(this.getAccountMemberships(accounts[i].id))
      }
      return Q.all(getAccounts)
   }

   /*
    * Get Project Memberships
    */
   this.getProjectMemberships = (id, callback) => {
      return Internals.apiCall(`/projects/${id}/memberships`, callback)
   }

   /*
    * Get Project Memberships
    */
   this.getProjectsMemberships = (projects) => {

      var getProjects = []
      for (var i = 0; i < projects.length; i++) {
         getProjects.push(this.getProjectMemberships(projects[i].id))
      }
      return Q.all(getProjects)
   }

   this.getStories = (options, callback) => {
		const month = options.filter.month;
      const year = options.filter.year;
      const start_month = UtilService.getMonthParams(month, year).start_month
      const end_month = UtilService.getMonthParams(month, year).end_month
      const start_year = UtilService.getMonthParams(month, year).start_year
      const end_year = UtilService.getMonthParams(month, year).end_year
      const last_day = UtilService.getMonthParams(month, year).last_day

		let params = `&accepted_before=${end_year}-${end_month}-01T00:00:00.000Z`
		params += `&accepted_after=${start_year}-${start_month}-${last_day}T00:00:00.000Z`
		const url = `/projects/${options.projectId}/stories?with_state=accepted${params}`

		// return callback(null, {url})
		_apiCall(url, function(err, stories) {

			if (err) return callback(err);
			return callback(null, { size:  stories.length, stories:stories })
		});
	}

	/*
	 * Get Stories' Activities
	 */
	this.getStoryActivities = (projectId, stories) => {
		var getActivities = []

		for (var i = 0; i < stories.length; i++) {
			getActivities.push(this.getStoryActivity(projectId, stories[i].id))
		};

		return Q.all(getActivities)
	}

	/*
{'fraction_non_alphanumeric': 0.14465511605099707, 'fraction_numerical': 0.009643674403399805, 'mean_word_length': 3.009829619921363, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1703855', 'n_tokens_mistral': 2123, 'n_tokens_neox': 1974, 'n_words': 402}