2017-02-06 13:40:28 -05:00
|
|
|
// Imports
|
2017-02-28 07:17:20 -05:00
|
|
|
import {assertNotMissingOrEmpty} from '../common/utils';
|
2017-02-06 13:40:28 -05:00
|
|
|
import {GithubApi} from './github-api';
|
|
|
|
|
|
|
|
// Interfaces - Types
|
2017-02-28 14:10:46 -05:00
|
|
|
export interface PullRequest {
|
2017-02-06 13:40:28 -05:00
|
|
|
number: number;
|
2017-02-28 14:10:46 -05:00
|
|
|
user: {login: string};
|
2017-06-18 18:15:07 -04:00
|
|
|
labels: {name: string}[];
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export type PullRequestState = 'all' | 'closed' | 'open';
|
|
|
|
|
|
|
|
// Classes
|
|
|
|
export class GithubPullRequests extends GithubApi {
|
2017-02-28 07:17:20 -05:00
|
|
|
// Constructor
|
|
|
|
constructor(githubToken: string, protected repoSlug: string) {
|
|
|
|
super(githubToken);
|
|
|
|
assertNotMissingOrEmpty('repoSlug', repoSlug);
|
|
|
|
}
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
// Methods - Public
|
|
|
|
public addComment(pr: number, body: string): Promise<void> {
|
|
|
|
if (!(pr > 0)) {
|
|
|
|
throw new Error(`Invalid PR number: ${pr}`);
|
|
|
|
} else if (!body) {
|
|
|
|
throw new Error(`Invalid or empty comment body: ${body}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.post<void>(`/repos/${this.repoSlug}/issues/${pr}/comments`, null, {body});
|
|
|
|
}
|
|
|
|
|
2017-02-28 14:10:46 -05:00
|
|
|
public fetch(pr: number): Promise<PullRequest> {
|
2017-06-18 18:15:07 -04:00
|
|
|
// Using the `/issues/` URL, because the `/pulls/` one does not provide labels.
|
|
|
|
return this.get<PullRequest>(`/repos/${this.repoSlug}/issues/${pr}`);
|
2017-02-28 14:10:46 -05:00
|
|
|
}
|
|
|
|
|
2017-02-06 13:40:28 -05:00
|
|
|
public fetchAll(state: PullRequestState = 'all'): Promise<PullRequest[]> {
|
2017-02-28 07:01:20 -05:00
|
|
|
console.log(`Fetching ${state} pull requests...`);
|
2017-02-06 13:40:28 -05:00
|
|
|
|
|
|
|
const pathname = `/repos/${this.repoSlug}/pulls`;
|
2017-02-28 07:01:20 -05:00
|
|
|
const params = {state};
|
2017-02-06 13:40:28 -05:00
|
|
|
|
2017-02-28 07:01:20 -05:00
|
|
|
return this.getPaginated<PullRequest>(pathname, params);
|
2017-02-06 13:40:28 -05:00
|
|
|
}
|
|
|
|
}
|