[gitlab] refactor creating issues to gql api, finally

This commit is contained in:
Laura Liberda 2021-02-03 14:50:34 +01:00
parent 2496327b39
commit f6f71b64d2
2 changed files with 33 additions and 11 deletions

View file

@ -18,6 +18,7 @@ import {
MERGE_REQUEST_MERGABILITY,
ENTITY_TYPE,
} from '../../types';
import { GLIssue, GLMutation, GLMutationCreateIssueArgs } from './api-types';
import { GL4MergeRequestCommit } from './rest-api-types';
import GitLabVendorManager from './vendormgr';
import assert from 'assert';
@ -113,9 +114,13 @@ export default class GitHubRepoManager implements RepoManager {
assert(resp.project, 'no project');
assert(resp.project.issue, 'no issue');
const { issue } = resp.project;
return this._parseIssue(issue);
}
public _parseIssue(issue: GLIssue): Issue {
return {
type: ENTITY_TYPE.ISSUE,
id: number,
id: issue.iid,
content: issue.description || '',
title: issue.title,
repo: this.repo,
@ -130,17 +135,34 @@ 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`,
const resp = await this.vendorMgr._doRequest_gql<GLMutation, GLMutationCreateIssueArgs>(
`
mutation ($input: CreateIssueInput!) {
createIssue(input: $input) {
issue {
iid
title
description
state
}
}
}
`,
{
title: issue.title,
description: issue.content,
input: {
projectPath: this.repoPath,
title: issue.title,
description: issue.content,
},
},
);
assert(resp.iid);
return this.getIssue(resp.iid.toString());
assert(resp.createIssue, 'creating issue failed for unknown reason');
assert(
!Array.isArray(resp.createIssue.errors) || resp.createIssue.errors.length === 0,
`GitLab said: ${(resp.createIssue.errors || []).map((e) => `"${e}"`).join(', ')}`,
);
assert(resp.createIssue.issue, 'creating issue failed for unknown reason (2)');
return this._parseIssue(resp.createIssue.issue);
}
public async getMergeRequest(number: string): Promise<MergeRequest> {

View file

@ -46,7 +46,7 @@ export default class GitLabVendorManager implements VendorManager<GitLabConfig>
}
/** internal and for RepoManager */
public async _doRequest_gql(query: string, variables: any) {
public async _doRequest_gql<T = GLQuery, D = any>(query: string, variables: D) {
return got
.post(this.gqlEndpoint, {
body: JSON.stringify({ query, variables }),
@ -56,7 +56,7 @@ export default class GitLabVendorManager implements VendorManager<GitLabConfig>
},
})
.then((res) => JSON.parse(res.body))
.then((res) => res.data) as Promise<GLQuery>;
.then((res) => res.data) as Promise<T>;
}
/** internal and for RepoManager */