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
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {graphql} from '@octokit/graphql';
|
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-04-08 15:34:55 -04:00
|
|
|
/** Error for failed Github API requests. */
|
|
|
|
export class GithubGraphqlClientError extends Error {}
|
|
|
|
|
2020-06-15 12:20:05 -04:00
|
|
|
/**
|
|
|
|
* A Github client for interacting with the Github APIs.
|
|
|
|
*
|
2020-07-24 11:59:12 -04:00
|
|
|
* Additionally, provides convenience methods for actions which require multiple requests, or
|
2020-06-15 12:20:05 -04:00
|
|
|
* would provide value from memoized style responses.
|
|
|
|
**/
|
2021-05-27 15:14:36 -04:00
|
|
|
export class GithubClient {
|
2021-04-08 15:34:55 -04:00
|
|
|
/** The graphql instance with authentication set during construction. */
|
|
|
|
private _graphql = graphql.defaults({headers: {authorization: `token ${this.token}`}});
|
2021-05-27 15:14:36 -04:00
|
|
|
/** The Octokit instance actually performing API requests. */
|
2021-06-02 15:48:51 -04:00
|
|
|
private _octokit = new Octokit({auth: this.token});
|
2020-06-15 12:20:05 -04:00
|
|
|
|
2021-04-08 15:34:55 -04:00
|
|
|
/**
|
|
|
|
* @param token The github authentication token for Github Rest and Graphql API requests.
|
|
|
|
*/
|
|
|
|
constructor(private token?: string) {
|
2021-05-27 15:14:36 -04:00
|
|
|
this._octokit.hook.error('request', error => {
|
2020-06-15 12:20:05 -04:00
|
|
|
// Wrap API errors in a known error class. This allows us to
|
|
|
|
// expect Github API errors better and in a non-ambiguous way.
|
|
|
|
throw new GithubApiRequestError(error.status, error.message);
|
|
|
|
});
|
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 = {}) {
|
|
|
|
if (this.token === undefined) {
|
|
|
|
throw new GithubGraphqlClientError(
|
|
|
|
'Cannot query via graphql without an authentication token set, use the authenticated ' +
|
|
|
|
'`GitClient` by calling `GitClient.getAuthenticatedInstance()`.');
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-05-27 15:14:36 -04:00
|
|
|
pulls = this._octokit.pulls;
|
|
|
|
repos = this._octokit.repos;
|
|
|
|
issues = this._octokit.issues;
|
|
|
|
git = this._octokit.git;
|
|
|
|
paginate = this._octokit.paginate;
|
|
|
|
rateLimit = this._octokit.rateLimit;
|
2020-06-15 12:20:05 -04:00
|
|
|
}
|