copykitku/src/vendor/github/vendormgr.ts
2020-08-05 23:33:36 +02:00

50 lines
1.4 KiB
TypeScript

import got from 'got';
import { VendorManager, VENDOR_TYPE, Vendor, Issue, Repo, RepoManager } from '../../types';
import { GHRepository, GHQuery } from './api-types';
import assert from 'assert';
import GitHubRepoManager from './repomgr';
export interface GitHubConfig {
token: string;
domain?: string | null;
}
export default class GitHubVendorManager implements VendorManager<GitHubConfig> {
vendor: Vendor;
config: GitHubConfig;
gqlEndpoint: string;
constructor(config: GitHubConfig) {
this.vendor = {
display: '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<RepoManager>;
}
/** internal and for RepoManager */
public async _doRequest(query: string, variables: any) {
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<GHQuery>;
}
}