initial issue replicating (gitlab)

This commit is contained in:
selfisekai 2020-08-23 13:16:38 +02:00
parent 5708778ab3
commit a94ce60ec5
3 changed files with 45 additions and 10 deletions

View file

@ -53,6 +53,7 @@ export interface RepoManager {
repo: Repo;
initialize: () => Promise<RepoManager>;
getIssue: (id: string) => Promise<Issue>;
replicateIssue?: (issue: Issue) => Promise<Issue>;
getMergeRequest: (id: string) => Promise<MergeRequest>;
}

View file

@ -23,6 +23,7 @@ export default class GitHubRepoManager implements RepoManager {
vendorMgr: GitLabVendorManager;
repo: Repo;
repoPath: string;
repoId: string;
constructor(vendorMgr: GitLabVendorManager, repoPath: string) {
this.vendorMgr = vendorMgr;
@ -43,17 +44,16 @@ export default class GitHubRepoManager implements RepoManager {
name: repoSmallPath,
};
this.repoPath = repoPath;
this.repoId = ''; // for strict null check, is replaced in .initialize
}
public async initialize() {
let meta = await this.vendorMgr._doRequest(
const meta = await this.vendorMgr._doRequest_gql(
`
query ($path: ID!, $ownerStr: String!, $ownerID: ID!) {
project(fullPath: $path) {
id
path
namespace {
path
}
}
group(fullPath: $ownerID) {
id
@ -73,8 +73,11 @@ export default class GitHubRepoManager implements RepoManager {
ownerID: this.repo.owner.username,
},
);
assert(meta.project);
assert(meta.group || meta.user);
assert(meta.project, 'no project');
assert(meta.group || meta.user, 'no top-level project owner');
const [, repoId] = /\/(\d+)$/.exec(meta.project.id) || [,];
assert(repoId, 'broken project id');
this.repoId = repoId;
this.repo.owner.username = meta.project.path;
if (meta.group) {
this.repo.owner.type = ACTOR_TYPE.ORG;
@ -85,7 +88,7 @@ export default class GitHubRepoManager implements RepoManager {
}
public async getIssue(number: string): Promise<Issue> {
const resp = await this.vendorMgr._doRequest(
const resp = await this.vendorMgr._doRequest_gql(
`
query ($path: ID!, $id: String!) {
project(fullPath: $path) {
@ -119,8 +122,22 @@ export default class GitHubRepoManager implements RepoManager {
};
}
public async replicateIssue(issue: Issue) {
// yes, v4, gitlab doesn't support creating issues with graphql api 🤦‍♀️
const resp = await this.vendorMgr._doRequest_v4(
'POST',
`projects/${encodeURIComponent(this.repoId)}/issues`,
{
title: issue.title,
description: issue.content,
},
);
assert(resp.iid);
return this.getIssue(resp.iid.toString());
}
public async getMergeRequest(number: string): Promise<MergeRequest> {
const resp = await this.vendorMgr._doRequest(
const resp = await this.vendorMgr._doRequest_gql(
`
query ($path: ID!, $id: String!) {
project(fullPath: $path) {

View file

@ -7,6 +7,7 @@
*/
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';
@ -20,6 +21,8 @@ export interface GitLabConfig {
export default class GitLabVendorManager implements VendorManager<GitLabConfig> {
vendor: Vendor;
config: GitLabConfig;
apiEndpoint: string;
v4Endpoint: string;
gqlEndpoint: string;
constructor(config: GitLabConfig) {
@ -29,7 +32,9 @@ export default class GitLabVendorManager implements VendorManager<GitLabConfig>
domain: config.domain,
};
this.config = config;
this.gqlEndpoint = `https://${this.vendor.domain}/api/graphql`;
this.apiEndpoint = `https://${this.vendor.domain}/api`;
this.v4Endpoint = `${this.apiEndpoint}/v4`;
this.gqlEndpoint = `${this.apiEndpoint}/graphql`;
}
public async initialize() {
@ -41,7 +46,7 @@ export default class GitLabVendorManager implements VendorManager<GitLabConfig>
}
/** internal and for RepoManager */
public async _doRequest(query: string, variables: any) {
public async _doRequest_gql(query: string, variables: any) {
return got
.post(this.gqlEndpoint, {
body: JSON.stringify({ query, variables }),
@ -53,4 +58,16 @@ export default class GitLabVendorManager implements VendorManager<GitLabConfig>
.then((res) => JSON.parse(res.body))
.then((res) => res.data) as Promise<GLQuery>;
}
/** internal and for RepoManager */
public async _doRequest_v4(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));
}
}