71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
/*
|
|
* Copycat. Copyright (C) 2020 selfisekai <laura@selfisekai.rocks> 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 <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
|
|
*/
|
|
|
|
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<GitHubConfig> {
|
|
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<RepoManager>;
|
|
}
|
|
|
|
/** internal and for RepoManager */
|
|
public async _doRequest<T = GHQuery, D = any>(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<T>;
|
|
}
|
|
|
|
/** used to provide diff/patch contents */
|
|
public async _http_get(url: string | URL): Promise<string> {
|
|
return got
|
|
.get(url, {
|
|
headers: {
|
|
Authorization: `Bearer ${this.config.token}`,
|
|
},
|
|
})
|
|
.then((res) => res.body);
|
|
}
|
|
}
|