2020-06-15 12:20:05 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2021-06-25 18:45:48 -04:00
|
|
|
import {OctokitOptions} from '@octokit/core/dist-types/types';
|
2020-06-15 12:20:05 -04:00
|
|
|
import {graphql} from '@octokit/graphql';
|
2021-06-25 18:45:48 -04:00
|
|
|
import {PaginateInterface} from '@octokit/plugin-paginate-rest';
|
|
|
|
import {RestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types';
|
2021-05-27 15:14:36 -04:00
|
|
|
import {Octokit} from '@octokit/rest';
|
2020-06-15 12:20:05 -04:00
|
|
|
import {RequestParameters} from '@octokit/types';
|
2021-05-27 15:14:36 -04:00
|
|
|
import {query} from 'typed-graphqlify';
|
2020-06-15 12:20:05 -04:00
|
|
|
|
2021-04-08 15:34:55 -04:00
|
|
|
/**
|
|
|
|
* An object representation of a Graphql Query to be used as a response type and
|
|
|
|
* to generate a Graphql query string.
|
|
|
|
*/
|
|
|
|
export type GraphqlQueryObject = Parameters<typeof query>[1];
|
|
|
|
|
2020-09-01 06:32:09 -04:00
|
|
|
/** Interface describing a Github repository. */
|
|
|
|
export interface GithubRepo {
|
|
|
|
/** Owner login of the repository. */
|
|
|
|
owner: string;
|
|
|
|
/** Name of the repository. */
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:20:05 -04:00
|
|
|
/** Error for failed Github API requests. */
|
|
|
|
export class GithubApiRequestError extends Error {
|
|
|
|
constructor(public status: number, message: string) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 09:59:20 -04:00
|
|
|
/** A Github client for interacting with the Github APIs. */
|
|
|
|
export class GithubClient {
|
|
|
|
/** The octokit instance actually performing API requests. */
|
|
|
|
private _octokit = new Octokit(this._octokitOptions);
|
|
|
|
|
|
|
|
readonly pulls = this._octokit.pulls;
|
|
|
|
readonly repos = this._octokit.repos;
|
|
|
|
readonly issues = this._octokit.issues;
|
|
|
|
readonly git = this._octokit.git;
|
|
|
|
readonly rateLimit = this._octokit.rateLimit;
|
2021-07-14 15:43:28 -04:00
|
|
|
readonly teams = this._octokit.teams;
|
2021-06-03 09:59:20 -04:00
|
|
|
|
2021-06-25 18:45:48 -04:00
|
|
|
// Note: These are properties from `Octokit` that are brought in by optional plugins.
|
|
|
|
// TypeScript requires us to provide an explicit type for these.
|
|
|
|
readonly rest: RestEndpointMethods = this._octokit.rest;
|
|
|
|
readonly paginate: PaginateInterface = this._octokit.paginate;
|
|
|
|
|
|
|
|
constructor(private _octokitOptions?: OctokitOptions) {}
|
2021-06-03 09:59:20 -04:00
|
|
|
}
|
2021-04-08 15:34:55 -04:00
|
|
|
|
2020-06-15 12:20:05 -04:00
|
|
|
/**
|
2021-06-03 09:59:20 -04:00
|
|
|
* Extension of the `GithubClient` that provides utilities which are specific
|
|
|
|
* to authenticated instances.
|
|
|
|
*/
|
|
|
|
export class AuthenticatedGithubClient extends GithubClient {
|
2021-04-08 15:34:55 -04:00
|
|
|
/** The graphql instance with authentication set during construction. */
|
2021-06-03 09:59:20 -04:00
|
|
|
private _graphql = graphql.defaults({headers: {authorization: `token ${this._token}`}});
|
2020-06-15 12:20:05 -04:00
|
|
|
|
2021-06-03 09:59:20 -04:00
|
|
|
constructor(private _token: string) {
|
|
|
|
// Set the token for the octokit instance.
|
|
|
|
super({auth: _token});
|
2021-04-08 15:34:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Perform a query using Github's Graphql API. */
|
|
|
|
async graphql<T extends GraphqlQueryObject>(queryObject: T, params: RequestParameters = {}) {
|
2021-04-21 08:23:57 -04:00
|
|
|
return (await this._graphql(query(queryObject).toString(), params)) as T;
|
2020-06-15 12:20:05 -04:00
|
|
|
}
|
|
|
|
}
|