/* * Copycat. Copyright (C) 2020 selfisekai and other contributors. * * This is free software, and you are welcome to redistribute it * under the GNU General Public License 3.0 or later; see the LICENSE file for details, * or, if the file is unavailable, visit . */ import got from 'got'; import { VendorManager, VENDOR_TYPE, Vendor, RepoManager } from '../../types'; import { GHQuery } from './api-types'; import GitHubRepoManager from './repomgr'; export interface GitHubConfig { token: string; domain?: string | null; } export default class GitHubVendorManager implements VendorManager { vendor: Vendor; config: GitHubConfig; gqlEndpoint: string; constructor(config: GitHubConfig) { this.vendor = { display: config.domain && config.domain !== 'github.com' ? `Github Enterprise @ ${config.domain}` : 'Microsoft GitHub', type: VENDOR_TYPE.GITHUB, domain: config.domain || 'github.com', }; this.config = config; this.gqlEndpoint = `https://${ this.vendor.domain === 'github.com' ? 'api.github.com' : /* github enterprise server */ `${this.vendor.domain}/api` }/graphql`; } public async initialize() { return this; } public async getRepo(path: string) { return new GitHubRepoManager(this, path).initialize() as Promise; } /** internal and for RepoManager */ public async _doRequest(query: string, variables: D) { return got .post(this.gqlEndpoint, { body: JSON.stringify({ query, variables }), headers: { Authorization: `Bearer ${this.config.token}` }, }) .then((res) => JSON.parse(res.body)) .then((res) => res.data) as Promise; } /** used to provide diff/patch contents */ public async _http_get(url: string | URL): Promise { return got .get(url, { headers: { Authorization: `Bearer ${this.config.token}`, }, }) .then((res) => res.body); } }