<commit_before>import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
import * as express from 'express';
import * as fs from 'fs-extra';
import gql from 'graphql-tag';
import {Server} from 'http';
import * as path from 'path';
import * as request from 'request-promise-native';

import {GitHub} from '../gql';
import {ViewerPullRequestsQuery} from '../gql-types';

const app = express();
const github = new GitHub();

const prsQuery = gql`
query ViewerPullRequests {
	viewer {
    pullRequests(last: 10, states: [OPEN]) {
      nodes {
        ...fullPR
      }
    }
  }
  incomingReviews: search(type: ISSUE, query: "is:open is:pr review-requested:samuelli archived:false", last: 10) {
    nodes {
      __typename
      ... on PullRequest {
        ...fullPR
      }
    }
  }
  rateLimit {
    cost
    limit
    remaining
    resetAt
    nodeCount
  }
}

fragment userFields on User {
  avatarUrl
  login
  resourcePath
  url
}

fragment fullPR on PullRequest {
  author {
    ...userFields
  }
  title
  repository {
    nameWithOwner
  }
  state
  createdAt
  lastEditedAt
  url
  number
  reviews(last: 10) {
    totalCount
    nodes {
      state
      author {
        ...userFields
      }
    }
  }
  reviewRequests(last: 2) {
    totalCount
    nodes {
      requestedReviewer {
        __typename
        ... on User {
          ...userFields
        }
      }
    }
  }
	commits(last: 1) {
    nodes {
      commit {
        status {
          contexts {
            id
            context
            state
            createdAt
          }
          state
        }
      }
    }
  }
}`;

async function fetchUserData(token: string) {
  const result = await github.query<ViewerPullRequestsQuery>(
      {query: prsQuery, fetchPolicy: 'network-only', context: {token}});
  return result.data;<commit_msg>Transform data on server (#37)<commit_after>import * as bodyParser from 'body-parser';
import * as cookieParser from 'cookie-parser';
import * as express from 'express';
import * as fs from 'fs-extra';
import gql from 'graphql-tag';
import {Server} from 'http';
import * as path from 'path';
import * as request from 'request-promise-native';

import {GitHub} from '../gql';
import {ViewerLoginQuery, ViewerPullRequestsQuery} from '../gql-types';

const app = express();
const github = new GitHub();

const viewerLoginQuery = gql`
query ViewerLogin {
  viewer {
    login
  }
}
`;

const prsQuery = gql`
query ViewerPullRequests($query: String!) {
	viewer {
    pullRequests(last: 10, states: [OPEN]) {
      nodes {
        ...fullPR
      }
    }
  }
  incomingReviews: search(type: ISSUE, query: $query, last: 10) {
    nodes {
      __typename
      ... on PullRequest {
        ...fullPR
      }
    }
  }
  rateLimit {
    cost
    limit
    remaining
    resetAt
    nodeCount
  }
}

fragment userFields on User {
  avatarUrl
  login
  resourcePath
  url
}

fragment fullPR on PullRequest {
  author {
    ...userFields
  }
  title
  repository {
    nameWithOwner
  }
  state
  createdAt
  lastEditedAt
  url
  number
  reviews(last: 10) {
    totalCount
    nodes {
      state
      author {
        ...userFields
      }
    }
  }
  reviewRequests(last: 2) {
    totalCount
    nodes {
      requestedReviewer {
        __typename
        ... on User {
          ...userFields
        }
      }
    }
  }
	commits(last: 1) {
    nodes {
      commit {
        status {
          contexts {
            id
            context
            state
            createdAt
          }
          state
        }
      }
    }
  }
}`;

interface PullRequest {
  repository: string;
  title: string;
  number: number;
  avatarUrl: string;
  approvedBy: string[];
  changesRequestedBy: string[];
  commentedBy: string[];
  pendingReviews: string[];
  statusState: 'passed'|'pending'|'failed';
}

interface DashResponse {
  prs: PullRequest[];
}

async function fetchUserData(token: string): Promise<DashResponse> {
  const loginResult = await github.query<ViewerLoginQuery>(
      {query: viewerLoginQuery, context: {token}});
  const login = loginResult.data.viewer.login;
  const incomingReviewsQuery =
      `is:open is:pr review-requested:${login} archived:false`;

  const result = await github.query<ViewerPullRequestsQuery>({
    query: prsQuery,
    variables: {query: incomingReviewsQuery},
    fetchPolicy: 'network-only',
    context: {token}
  });
  const prs = [];
  for (const pr of result.data.viewer.pullRequests.nodes || []) {
    if (!pr) {
      continue;
    }
    const object: PullRequest = {
      repository: pr.repository.nameWithOwner,
      title: pr.title,
      number: pr.number,
      avatarUrl: '',
      approvedBy: [],
      changesRequestedBy: [],
      commentedBy: [],
      pendingReviews: [],
      statusState: 'passed',
    };
    if (pr.author && pr.author.__typename === 'User') {
      object.avatarUrl = pr.author.avatarUrl;
    }
    prs.push(object);
  }
  return {prs};
{'fraction_non_alphanumeric': 0.11403861625100563, 'fraction_numerical': 0.0036202735317779565, 'mean_word_length': 2.2418513689700132, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5860567', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1579, 'n_words': 389}