<commit_before>const fs = require('fs');
const config = require('../../config');
const https = require('https');
const url = require('url');
const request = require('request');

const authenticatedRouter = require('./authenticatedRouter');

const GITHUB_API_ROOT = 'https://api.github.com';

authenticatedRouter.get('/repos/:organisation/:repo/issues', function(req, res) {
  console.log('Request App received request to: ', req.url);

  if (!validateRepository(req.params.organisation, req.params.repo, appData.projects))
    res.status(403).send({ error: 'Invalid repository name' });

  console.log(`Request App: Listing issues on repository ${req.params.organisation}/${req.params.repo}`);

  const gitHubRequest = request(getProxyRequestOptions(req.url)); // add the authorization that github wants
  
  console.log('Request App: Proxying request to: ', getProxyRequestOptions(req.url).url);
  
  req
  .pipe(gitHubRequest, genericErrorHandler) // send to git hub
  .on('response', response => rewriteResponseHeaders(req, response)) // response from github, monkey with the headers for some reason. Looking at the logs it doesn't seem to do anything
  .pipe(res); // pipe response from github back to response from this route / function
});

// I don't think this is required.
// appOrRouter.get('/repositories/:repoId/issues', function(req, res) {
//   console.log(`going through pages on repository ${req.params.repoId}`);
//   const r = request(getProxyRequestOptions(req.url));
//   console.log('Proxied request to: ', getProxyRequestOptions(req.url).url);
//   req.pipe(r, genericErrorHandler).on('response', response => rewriteResponseHeaders(req, response)).pipe(res);
// });

authenticatedRouter.post('/repos/:organisation/:repo/issues', function(req, res) {
  if (!validateRepository(req.params.organisation, req.params.repo, appData.projects))
    res.status(403).send({ error: 'Invalid repository name' });

  console.log(`creating issue on repository ${req.params.organisation}/${req.params.repo}`);
  const r = request.post(getProxyRequestOptions(req.url));
  req.pipe(r, genericErrorHandler).pipe(res);
});



const getlocalAppRootUrl = request => {
  return process.env.NODE_ENV == 'production'
    ? url.format({
        protocol: 'https', // request.protocol returns http for now since the node server itself is only using http. However the api is used over https thanks to azure / IIS
        host: request.hostname,
        // port: request.port,
        pathname: ''
      })
    : 'https://localhost:3000'; // hard coded value in development because the request came through the webpack dev server on a different port and via https.
};

function loadAppData() {
  return JSON.parse(fs.readFileSync(`${__dirname}/../../${config.app.groupConfigPath}`, 'utf-8'));
}

const appData = loadAppData();

const getProxyRequestOptions = url => ({
  url: GITHUB_API_ROOT + url.replace('/api', ''),
  headers: {
    Authorization: `token ${config.github.botToken}`<commit_msg> appdata instead of repeating ourselves<commit_after>const fs = require('fs');
const config = require('../../config');
const https = require('https');
const url = require('url');
const request = require('request');

const authenticatedRouter = require('./authenticatedRouter');

const GITHUB_API_ROOT = 'https://api.github.com';

authenticatedRouter.get('/repos/:organisation/:repo/issues', function(req, res) {
  console.log('Request App received request to: ', req.url);

  if (!validateRepository(req.params.organisation, req.params.repo, config.appData.projects))
    res.status(403).send({ error: 'Invalid repository name' });

  console.log(`Request App: Listing issues on repository ${req.params.organisation}/${req.params.repo}`);

  const gitHubRequest = request(getProxyRequestOptions(req.url)); // add the authorization that github wants
  
  console.log('Request App: Proxying request to: ', getProxyRequestOptions(req.url).url);
  
  req
  .pipe(gitHubRequest, genericErrorHandler) // send to git hub
  .on('response', response => rewriteResponseHeaders(req, response)) // response from github, monkey with the headers for some reason. Looking at the logs it doesn't seem to do anything
  .pipe(res); // pipe response from github back to response from this route / function
});

// I don't think this is required.
// appOrRouter.get('/repositories/:repoId/issues', function(req, res) {
//   console.log(`going through pages on repository ${req.params.repoId}`);
//   const r = request(getProxyRequestOptions(req.url));
//   console.log('Proxied request to: ', getProxyRequestOptions(req.url).url);
//   req.pipe(r, genericErrorHandler).on('response', response => rewriteResponseHeaders(req, response)).pipe(res);
// });

authenticatedRouter.post('/repos/:organisation/:repo/issues', function(req, res) {
  if (!validateRepository(req.params.organisation, req.params.repo, config.appData.projects))
    res.status(403).send({ error: 'Invalid repository name' });

  console.log(`creating issue on repository ${req.params.organisation}/${req.params.repo}`);
  const r = request.post(getProxyRequestOptions(req.url));
  req.pipe(r, genericErrorHandler).pipe(res);
});



const getlocalAppRootUrl = request => {
  return process.env.NODE_ENV == 'production'
    ? url.format({
        protocol: 'https', // request.protocol returns http for now since the node server itself is only using http. However the api is used over https thanks to azure / IIS
        host: request.hostname,
        // port: request.port,
        pathname: ''
      })
    : 'https://localhost:3000'; // hard coded value in development because the request came through the webpack dev server on a different port and via https.
};

const getProxyRequestOptions = url => ({
  url: GITHUB_API_ROOT + url.replace('/api', ''),
  headers: {
    Authorization: `token ${config.github.botToken}`
{'fraction_non_alphanumeric': 0.14570892339191263, 'fraction_numerical': 0.0035830063129158847, 'mean_word_length': 4.504225352112676, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 4, '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': '6630012', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1642, 'n_words': 460}