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';
|
|
|
|
import * as Octokit from '@octokit/rest';
|
|
|
|
import {RequestParameters} from '@octokit/types';
|
|
|
|
import {query, types} from 'typed-graphqlify';
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
**/
|
2020-07-24 11:59:12 -04:00
|
|
|
export class GithubClient extends Octokit {
|
2020-06-15 12:20:05 -04:00
|
|
|
/** The Github GraphQL (v4) API. */
|
2020-08-14 19:49:07 -04:00
|
|
|
graphql: GithubGraphqlClient;
|
2020-06-15 12:20:05 -04:00
|
|
|
|
|
|
|
/** The current user based on checking against the Github API. */
|
|
|
|
private _currentUser: string|null = null;
|
|
|
|
|
|
|
|
constructor(token?: string) {
|
|
|
|
// Pass in authentication token to base Octokit class.
|
|
|
|
super({auth: token});
|
|
|
|
|
|
|
|
this.hook.error('request', error => {
|
|
|
|
// 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);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create authenticated graphql client.
|
2020-08-14 19:49:07 -04:00
|
|
|
this.graphql = new GithubGraphqlClient(token);
|
2020-06-15 12:20:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Retrieve the login of the current user from Github. */
|
|
|
|
async getCurrentUser() {
|
|
|
|
// If the current user has already been retrieved return the current user value again.
|
|
|
|
if (this._currentUser !== null) {
|
|
|
|
return this._currentUser;
|
|
|
|
}
|
2020-08-14 19:49:07 -04:00
|
|
|
const result = await this.graphql.query({
|
2020-06-15 12:20:05 -04:00
|
|
|
viewer: {
|
|
|
|
login: types.string,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this._currentUser = result.viewer.login;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-09 08:44:14 -04:00
|
|
|
* An object representation of a GraphQL Query to be used as a response type and
|
|
|
|
* to generate a GraphQL query string.
|
2020-06-15 12:20:05 -04:00
|
|
|
*/
|
2020-09-09 08:44:14 -04:00
|
|
|
export type GraphQLQueryObject = Parameters<typeof query>[1];
|
2020-06-15 12:20:05 -04:00
|
|
|
|
2020-09-09 08:44:14 -04:00
|
|
|
/** A client for interacting with Github's GraphQL API. */
|
|
|
|
export class GithubGraphqlClient {
|
2020-06-15 12:20:05 -04:00
|
|
|
/** The Github GraphQL (v4) API. */
|
|
|
|
private graqhql = graphql;
|
|
|
|
|
|
|
|
constructor(token?: string) {
|
|
|
|
// Set the default headers to include authorization with the provided token for all
|
|
|
|
// graphQL calls.
|
|
|
|
if (token) {
|
2020-08-14 19:49:07 -04:00
|
|
|
this.graqhql = this.graqhql.defaults({headers: {authorization: `token ${token}`}});
|
2020-06-15 12:20:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Perform a query using Github's GraphQL API. */
|
|
|
|
async query<T extends GraphQLQueryObject>(queryObject: T, params: RequestParameters = {}) {
|
|
|
|
const queryString = query(queryObject);
|
|
|
|
return (await this.graqhql(queryString, params)) as T;
|
|
|
|
}
|
|
|
|
}
|