diff --git a/src/types.ts b/src/types.ts index 6799d7d..d78c682 100644 --- a/src/types.ts +++ b/src/types.ts @@ -45,15 +45,46 @@ export interface RepoManager { repo: Repo; initialize: () => Promise; getIssue: (id: string) => Promise; + getMergeRequest: (id: string) => Promise; } -export interface Issue { +/** issue, merge request or whatever the fuck you need */ +export interface RepoElement { id: string; title: string; content: string; repo: Repo; } +/** basic */ +export enum ISSUE_STATE { + OPEN = 'open', + CLOSED = 'closed', +} + +export interface Issue extends RepoElement { + state: ISSUE_STATE; +} + +export enum MERGE_REQUEST_STATE { + OPEN = 'open', + /** closed without merging */ + CLOSED = 'closed', + MERGED = 'merged', +} + +export enum MERGE_REQUEST_MERGABILITY { + MERGEABLE = 'mergeable', + CONFLICTING = 'conflicting', +} + +export interface MergeRequest extends RepoElement { + state: MERGE_REQUEST_STATE; + /** null if unknown */ + mergability: MERGE_REQUEST_MERGABILITY | null; + isDraft: boolean; +} + /** the account that did an action */ export enum ACTOR_TYPE { /** human person, absolutely not a sentient lizard */ diff --git a/src/vendor/github/repomgr.ts b/src/vendor/github/repomgr.ts index 78cc757..e79c6d4 100644 --- a/src/vendor/github/repomgr.ts +++ b/src/vendor/github/repomgr.ts @@ -1,4 +1,13 @@ -import { RepoManager, Issue, Repo, ACTOR_TYPE } from '../../types'; +import { + RepoManager, + Issue, + Repo, + ACTOR_TYPE, + ISSUE_STATE, + MergeRequest, + MERGE_REQUEST_STATE, + MERGE_REQUEST_MERGABILITY, +} from '../../types'; import GitHubVendorManager from './vendormgr'; import assert from 'assert'; @@ -91,10 +100,63 @@ export default class GitHubRepoManager implements RepoManager { assert(resp.repository.issue, 'no issue'); const { issue } = resp.repository; return { - id: issue.number.toString(), + id: number, content: issue.body, title: issue.title, repo: this.repo, + state: issue.closed ? ISSUE_STATE.CLOSED : ISSUE_STATE.OPEN, + }; + } + + public async getMergeRequest(number: string): Promise { + const resp = await this.vendorMgr._doRequest( + ` + query Query($owner: String!, $name: String!, $number: Int!) { + repository(owner: $owner, name: $name) { + pullRequest(number: $number) { + title + body + isDraft + closed + closedAt + mergeable + merged + mergedAt + mergedBy { + login + } + labels(first: 0) { + nodes { + name + color + description + } + } + } + } + } + `, + { + owner: this.repo.owner.username, + name: this.repo.name, + number: parseInt(number, 10), + }, + ); + assert(resp.repository, 'no repository'); + assert(resp.repository.pullRequest, 'no pull request'); + const { pullRequest } = resp.repository; + return { + id: number, + content: pullRequest.body, + title: pullRequest.title, + repo: this.repo, + state: pullRequest.closed ? MERGE_REQUEST_STATE.CLOSED : MERGE_REQUEST_STATE.OPEN, + mergability: { + MERGEABLE: MERGE_REQUEST_MERGABILITY.MERGEABLE, + CONFLICTING: MERGE_REQUEST_MERGABILITY.CONFLICTING, + UNKNOWN: null, + }[pullRequest.mergeable], + isDraft: pullRequest.isDraft, }; } }