74 lines
2.3 KiB
TypeScript
74 lines
2.3 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 qs from 'querystring';
|
|
import { VendorManager, VENDOR_TYPE, Vendor, RepoManager } from '../../types';
|
|
import { GLQuery } from './api-types';
|
|
import GitLabRepoManager from './repomgr';
|
|
|
|
export interface GitLabConfig {
|
|
/** not required for read actions on public repos */
|
|
token?: string | null;
|
|
domain: string;
|
|
}
|
|
|
|
export default class GitLabVendorManager implements VendorManager<GitLabConfig> {
|
|
vendor: Vendor;
|
|
config: GitLabConfig;
|
|
apiEndpoint: string;
|
|
v4Endpoint: string;
|
|
gqlEndpoint: string;
|
|
|
|
constructor(config: GitLabConfig) {
|
|
this.vendor = {
|
|
display: `GitLab @ ${config.domain}`,
|
|
type: VENDOR_TYPE.GITLAB,
|
|
domain: config.domain,
|
|
};
|
|
this.config = config;
|
|
this.apiEndpoint = `https://${this.vendor.domain}/api`;
|
|
this.v4Endpoint = `${this.apiEndpoint}/v4`;
|
|
this.gqlEndpoint = `${this.apiEndpoint}/graphql`;
|
|
}
|
|
|
|
public async initialize() {
|
|
return this;
|
|
}
|
|
|
|
public async getRepo(path: string) {
|
|
return new GitLabRepoManager(this, path).initialize() as Promise<RepoManager>;
|
|
}
|
|
|
|
/** internal and for RepoManager */
|
|
public async _doRequest_gql<T = GLQuery, D = any>(query: string, variables: D) {
|
|
return got
|
|
.post(this.gqlEndpoint, {
|
|
body: JSON.stringify({ query, variables }),
|
|
headers: {
|
|
Authorization: `Bearer ${this.config.token}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
.then((res) => JSON.parse(res.body))
|
|
.then((res) => res.data) as Promise<T>;
|
|
}
|
|
|
|
/** internal and for RepoManager */
|
|
public async _doRequest_v4<T = any>(method: 'GET' | 'POST', path: string, query?: any) {
|
|
// did you know that gitlab graphql api is fucked up
|
|
// and does not support some of the core functionality like creating issues?
|
|
return got(`${this.v4Endpoint}/${path}?${qs.stringify(query)}`, {
|
|
method,
|
|
headers: {
|
|
Authorization: `Bearer ${this.config.token}`,
|
|
},
|
|
}).then((res) => JSON.parse(res.body) as T);
|
|
}
|
|
}
|