github: issue replicating

This commit is contained in:
selfisekai 2020-08-26 21:34:43 +02:00
parent a94ce60ec5
commit b942645273
3 changed files with 42 additions and 3 deletions

View file

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

View file

@ -18,10 +18,12 @@ import {
} from '../../types';
import GitHubVendorManager from './vendormgr';
import assert from 'assert';
import { GHMutation, GHMutationCreateIssueArgs } from './api-types';
export default class GitHubRepoManager implements RepoManager {
vendorMgr: GitHubVendorManager;
repo: Repo;
repoId: string;
constructor(vendorMgr: GitHubVendorManager, repoPath: string) {
this.vendorMgr = vendorMgr;
@ -39,6 +41,7 @@ export default class GitHubRepoManager implements RepoManager {
},
name: repoName,
};
this.repoId = '';
}
public async initialize() {
@ -46,6 +49,7 @@ export default class GitHubRepoManager implements RepoManager {
`
query Query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
name
owner {
login
@ -61,6 +65,7 @@ export default class GitHubRepoManager implements RepoManager {
);
assert(meta.repository);
assert(meta.repository.owner);
this.repoId = meta.repository.id;
this.repo.name = meta.repository.name;
this.repo.owner.username = meta.repository.owner.login;
// @ts-ignore graphql-codegen ignores built-in graphql values
@ -106,6 +111,40 @@ export default class GitHubRepoManager implements RepoManager {
};
}
public async replicateIssue(issue: Issue) {
const resp = await this.vendorMgr._doRequest<GHMutation, GHMutationCreateIssueArgs>(
`
mutation ($input: CreateIssueInput!) {
createIssue(input: $input) {
issue {
number
title
body
closed
}
}
}
`,
{
input: {
repositoryId: this.repoId,
title: issue.title,
body: issue.content,
},
},
);
assert(resp.createIssue, 'creating issue failed');
assert(resp.createIssue.issue, 'creating issue failed');
const replicated = resp.createIssue.issue;
return {
id: replicated.number.toString(),
content: replicated.body,
title: replicated.title,
repo: this.repo,
state: replicated.closed ? ISSUE_STATE.CLOSED : ISSUE_STATE.OPEN,
};
}
public async getMergeRequest(number: string): Promise<MergeRequest> {
const resp = await this.vendorMgr._doRequest(
`

View file

@ -47,13 +47,13 @@ export default class GitHubVendorManager implements VendorManager<GitHubConfig>
}
/** internal and for RepoManager */
public async _doRequest(query: string, variables: any) {
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<GHQuery>;
.then((res) => res.data) as Promise<T>;
}
}