[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, MERGE_REQUEST_MERGABILITY,
ENTITY_TYPE, ENTITY_TYPE,
} from '../../types'; } from '../../types';
import { GLIssue, GLMutation, GLMutationCreateIssueArgs } from './api-types';
import { GL4MergeRequestCommit } from './rest-api-types'; import { GL4MergeRequestCommit } from './rest-api-types';
import GitLabVendorManager from './vendormgr'; import GitLabVendorManager from './vendormgr';
import assert from 'assert'; import assert from 'assert';
@ -113,9 +114,13 @@ export default class GitHubRepoManager implements RepoManager {
assert(resp.project, 'no project'); assert(resp.project, 'no project');
assert(resp.project.issue, 'no issue'); assert(resp.project.issue, 'no issue');
const { issue } = resp.project; const { issue } = resp.project;
return this._parseIssue(issue);
}
public _parseIssue(issue: GLIssue): Issue {
return { return {
type: ENTITY_TYPE.ISSUE, type: ENTITY_TYPE.ISSUE,
id: number, id: issue.iid,
content: issue.description || '', content: issue.description || '',
title: issue.title, title: issue.title,
repo: this.repo, repo: this.repo,
@ -130,17 +135,34 @@ export default class GitHubRepoManager implements RepoManager {
} }
public async replicateIssue(issue: Issue) { public async replicateIssue(issue: Issue) {
// yes, v4, gitlab doesn't support creating issues with graphql api 🤦‍♀️ const resp = await this.vendorMgr._doRequest_gql<GLMutation, GLMutationCreateIssueArgs>(
const resp = await this.vendorMgr._doRequest_v4( `
'POST', mutation ($input: CreateIssueInput!) {
`projects/${encodeURIComponent(this.repoId)}/issues`, createIssue(input: $input) {
issue {
iid
title
description
state
}
}
}
`,
{ {
title: issue.title, input: {
description: issue.content, projectPath: this.repoPath,
title: issue.title,
description: issue.content,
},
}, },
); );
assert(resp.iid); assert(resp.createIssue, 'creating issue failed for unknown reason');
return this.getIssue(resp.iid.toString()); 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> { public async getMergeRequest(number: string): Promise<MergeRequest> {

View file

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