<commit_before>const requestify = require('requestify');

let getBaseUrl = (url) => {
  if (!url) {
    throw new Error('url not specified - can\'t get base url');
  }
  let index = url.indexOf('rest/');
  if (index === -1) {
    throw new Error('Invalid url (can\'t find \'rest/\' string) - can\'t get base url');
  }
  let baseUrl = url.substr(0, index + 5);

  return baseUrl;
};

let extractSessionCookie = (cookies, cookieName) => {
  if (!cookieName) {
    throw new Error('cookie name not specified - can\'t extract session cookie');
  }
  if (!cookies) {
    throw new Error('cookies not specified - can\'t extract session cookie');
  }
  if (!Array.isArray(cookies)) {
    throw new Error('cookies is not an array - can\'t extract session cookie');
  }

  try {
    var sessionCookie = cookies.find((cookie) => { // eslint-disable-line no-var
      let name = cookie.split('=')[0];
      let value = cookie.split('=')[1].split(';')[0];
      return ((name === cookieName) && (value != '""'));
    }).split(';')[0];
  } catch (error) {
    throw new Error('cookie name not found in cookies - can\'t extract session cookie');
  }

  return sessionCookie;
};

module.exports = {
  getCookie: (userName, password, url) => new Promise((resolve, reject) => {
    try {
      let loginUrl = getBaseUrl(url) + 'auth/1/session';
      let body = {'username': userName, 'password': password};
      let options = {
        cache: {
          cache: false,
        },
      };

      requestify.post(loginUrl, body, options)
        .then((response) => {
          resolve(extractSessionCookie(response.headers['set-cookie'], JSON.parse(response.body).session.name));
        })
        .catch((error) => {
          reject(new Error('Can\'t get cookie [' + error.message + ']'));
        });
    } catch (error) {
      reject(new Error('Can\'t get cookie [' + error.message + ']'));
    }
  }),
};<commit_msg>provide login and header for CBA<commit_after>const requestify = require('requestify');

let getBaseUrl = (url) => {
  if (!url) {
    throw new Error('url not specified - can\'t get base url');
  }
  let index = url.indexOf('rest/');
  if (index === -1) {
    throw new Error('Invalid url (can\'t find \'rest/\' string) - can\'t get base url');
  }
  let baseUrl = url.substr(0, index + 5);

  return baseUrl;
};

let extractSessionCookie = (cookies, cookieName) => {
  if (!cookieName) {
    throw new Error('cookie name not specified - can\'t extract session cookie');
  }
  if (!cookies) {
    throw new Error('cookies not specified - can\'t extract session cookie');
  }
  if (!Array.isArray(cookies)) {
    throw new Error('cookies is not an array - can\'t extract session cookie');
  }

  try {
    var sessionCookie = cookies.find((cookie) => { // eslint-disable-line no-var
      let name = cookie.split('=')[0];
      let value = cookie.split('=')[1].split(';')[0];
      return ((name === cookieName) && (value != '""'));
    }).split(';')[0];
  } catch (error) {
    throw new Error('cookie name not found in cookies - can\'t extract session cookie');
  }

  return sessionCookie;
};

module.exports = {
  login: (userName, password, url) => new Promise((resolve, reject) => {
    try {
      let loginUrl = getBaseUrl(url) + 'auth/1/session';
      let body = {'username': userName, 'password': password};
      let options = {
        cache: {
          cache: false,
        },
      };

      requestify.post(loginUrl, body, options)
        .then((response) => {
          resolve(extractSessionCookie(response.headers['set-cookie'], JSON.parse(response.body).session.name));
        })
        .catch((error) => {
          reject(new Error('Can\'t get cookie [' + error.message + ']'));
        });
    } catch (error) {
      reject(new Error('Can\'t get cookie [' + error.message + ']'));
    }
  }),
  getHeader: (sessionCookie) => {
    return {'cookie': sessionCookie, 'Cache-Control': 'public, max-age=60'};
  },
};
{'fraction_non_alphanumeric': 0.17776652317042288, 'fraction_numerical': 0.004558115978728792, 'mean_word_length': 2.8386783284742467, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1047186', 'n_tokens_mistral': 1230, 'n_tokens_neox': 1217, 'n_words': 329}