<commit_before>import ApolloClient from "apollo-client"
import gql from "graphql-tag"
import fetch from "node-fetch"
import { createHttpLink } from "apollo-link-http"
import { InMemoryCache } from "apollo-cache-inmemory"
import { EntryType } from "../models/Entry"
import { LoaderEntriesCache } from "./LoaderEntriesCache"
import { GitHubRepository } from "../models/GitHubRepository"
import { loadSecret } from "../helpers/loadSecret"

const query = gql`
  query {
    user(login: "josephduffy") {
      contributionsCollection {
        commitContributionsByRepository(maxRepositories: 100) {
          repository {
            name
            description
            url
            isPrivate
            owner {
              login
            }
          }
          url
          contributions(first: 1) {
            nodes {
              occurredAt
              commitCount
            }
          }
        }
      }
    }
  }
`

interface QueryResult {
  user: {
    contributionsCollection: {
      commitContributionsByRepository: ContributionByRepository[]
    }
  }
}

interface ContributionByRepository {
  repository: {
    name: string
    description?: string
    url: string
    isPrivate: boolean
    owner: {
      login: string
    }
  }
  contributions: {
    nodes: {
      occurredAt: string
      commitCount: number
    }[]
  }
}

export class GitHubRepositoriesLoader {
  private cache: LoaderEntriesCache<GitHubRepository>

  constructor() {
    if (process.env["GITHUB_REPOS_CACHE_TIMEOUT"] !== undefined) {
      this.cache = new LoaderEntriesCache(
        this.loadReleases.bind(this),
        parseInt(process.env["GITHUB_REPOS_CACHE_TIMEOUT"]),
      )
    } else if (process.env["CACHE_TIMEOUT"] !== undefined) {
      this.cache = new LoaderEntriesCache(
        this.loadReleases.bind(this),
        parseInt(process.env["CACHE_TIMEOUT"]),
      )
    } else {
      this.cache = new LoaderEntriesCache(this.loadReleases.bind(this))
    }
  }

  getRepositories(): Promise<GitHubRepository[]> {
    return this.cache.entries
  }

  private async loadReleases(): Promise<GitHubRepository[]> {
    const accessToken = await loadSecret("GITHUB_ACCESS_TOKEN")

    if (accessToken === undefined) {
      console.warn(
        "GITHUB_ACCESS_TOKEN is not set; GitHub releases will not be loaded",
      )
      return []
    }

    const link = createHttpLink({
      uri: "https://api.github.com/graphql",
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      fetch: fetch as any,
      headers: {
        Authorization: `bearer ${accessToken}`,
      },
    })
    const client = new ApolloClient({
      link: link,
      cache: new InMemoryCache(),
    })
    console.debug("Loading GitHub repositories")
    const result = await client.query({ query })
    if (result.errors) {
      console.error("Error loading GitHub repositories", result.errors)
      throw result.errors
    }

    const data = result.data as QueryResult
    const repositories = data.user.contributionsCollection.commitContributionsByRepository
      .filter(repo => !repo.repository.isPrivate)
        const mostRecentContribution =
          contributionByRepository.contributions.nodes[0]

        if (!mostRecentContribution) {
          console.warn(
            "Got a repository with no recent contribution:",
            contributionByRepository,
          )
          return repositories
        }

        repositories.push({
          description: contributionByRepository.repository.description,
          name: contributionByRepository.repository.name,
          url: contributionByRepository.repository.url,
          owner: contributionByRepository.repository.owner.login,
          allContributionsURL: `${contributionByRepository.repository.url}/commits?author=JosephDuffy`,<commit_msg>Fix GitHubRepositoriesLoader compilation<commit_after>import ApolloClient from "apollo-client"
import gql from "graphql-tag"
import fetch from "node-fetch"
import { createHttpLink } from "apollo-link-http"
import { InMemoryCache } from "apollo-cache-inmemory"
import { EntryType } from "../models/Entry"
import { LoaderEntriesCache } from "./LoaderEntriesCache"
import { GitHubRepository } from "../models/GitHubRepository"
import { loadSecret } from "../helpers/loadSecret"

const query = gql`
  query {
    user(login: "josephduffy") {
      contributionsCollection {
        commitContributionsByRepository(maxRepositories: 100) {
          repository {
            name
            description
            url
            isPrivate
            owner {
              login
            }
          }
          url
          contributions(first: 1) {
            nodes {
              occurredAt
              commitCount
            }
          }
        }
      }
    }
  }
`

interface QueryResult {
  user: {
    contributionsCollection: {
      commitContributionsByRepository: ContributionByRepository[]
    }
  }
}

interface ContributionByRepository {
  repository: {
    name: string
    description?: string
    url: string
    isPrivate: boolean
    owner: {
      login: string
    }
  }
  contributions: {
    nodes: {
      occurredAt: string
      commitCount: number
    }[]
  }
}

export class GitHubRepositoriesLoader {
  private cache: LoaderEntriesCache<GitHubRepository>

  constructor() {
    if (process.env["GITHUB_REPOS_CACHE_TIMEOUT"] !== undefined) {
      this.cache = new LoaderEntriesCache(
        this.loadReleases.bind(this),
        parseInt(process.env["GITHUB_REPOS_CACHE_TIMEOUT"]),
      )
    } else if (process.env["CACHE_TIMEOUT"] !== undefined) {
      this.cache = new LoaderEntriesCache(
        this.loadReleases.bind(this),
        parseInt(process.env["CACHE_TIMEOUT"]),
      )
    } else {
      this.cache = new LoaderEntriesCache(this.loadReleases.bind(this))
    }
  }

  getRepositories(): Promise<GitHubRepository[]> {
    return this.cache.entries
  }

  private async loadReleases(): Promise<GitHubRepository[]> {
    const accessToken = await loadSecret("GITHUB_ACCESS_TOKEN")

    if (accessToken === undefined) {
      console.warn(
        "GITHUB_ACCESS_TOKEN is not set; GitHub releases will not be loaded",
      )
      return []
    }

    const link = createHttpLink({
      uri: "https://api.github.com/graphql",
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      fetch: fetch as any,
      headers: {
        Authorization: `bearer ${accessToken}`,
      },
    })
    const client = new ApolloClient({
      link: link,
      cache: new InMemoryCache(),
    })
    console.debug("Loading GitHub repositories")
    const result = await client.query({ query })
    if (result.errors) {
      console.error("Error loading GitHub repositories", result.errors)
      throw result.errors
    }

    const data = result.data as QueryResult
    const repositories = data.user.contributionsCollection.commitContributionsByRepository
      .filter(
        contributionByRepository =>
          !contributionByRepository.repository.isPrivate,
      )
      .reduce((repositories: GitHubRepository[], contributionByRepository) => {
        const mostRecentContribution =
          contributionByRepository.contributions.nodes[0]

        if (!mostRecentContribution) {
          console.warn(
            "Got a repository with no recent contribution:",
            contributionByRepository,
          )
          return repositories
        }

        repositories.push({
          description: contributionByRepository.repository.description,
          name: contributionByRepository.repository.name,
          url: contributionByRepository.repository.url,
          owner: contributionByRepository.repository.owner.login,
          allContributionsURL: `${contributionByRepository.repository.url}/commits?author=JosephDuffy`,
{'fraction_non_alphanumeric': 0.0963302752293578, 'fraction_numerical': 0.0012742099898063201, 'mean_word_length': 2.5435665914221217, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '3686692', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2105, 'n_words': 439}