Skip to content

Commit

Permalink
refactor(dev-infra): update to later version of @octokit/rest and rem…
Browse files Browse the repository at this point in the history
…ove class extenstion of Octokit (#42395)

Update @octokit/rest and remove the usage of a class extension of Octokit as the
class does not have a class define constructor.

PR Close #42395
  • Loading branch information
josephperrott authored and AndrewKushnir committed Jun 1, 2021
1 parent af762fa commit aeb4072
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 143 deletions.
56 changes: 14 additions & 42 deletions dev-infra/build-worker.js
Expand Up @@ -8,7 +8,7 @@ require('inquirer');
var child_process = require('child_process');
var semver = require('semver');
var graphql = require('@octokit/graphql');
var Octokit = require('@octokit/rest');
var rest = require('@octokit/rest');
var typedGraphqlify = require('typed-graphqlify');
var url = require('url');

Expand Down Expand Up @@ -68,32 +68,27 @@ var GithubGraphqlClientError = /** @class */ (function (_super) {
* Additionally, provides convenience methods for actions which require multiple requests, or
* would provide value from memoized style responses.
**/
var GithubClient = /** @class */ (function (_super) {
tslib.__extends(GithubClient, _super);
var GithubClient = /** @class */ (function () {
/**
* @param token The github authentication token for Github Rest and Graphql API requests.
*/
function GithubClient(token) {
var _this =
// Pass in authentication token to base Octokit class.
_super.call(this, { auth: token }) || this;
_this.token = token;
/** The current user based on checking against the Github API. */
_this._currentUser = null;
this.token = token;
/** The graphql instance with authentication set during construction. */
_this._graphql = graphql.graphql.defaults({ headers: { authorization: "token " + _this.token } });
_this.hook.error('request', function (error) {
this._graphql = graphql.graphql.defaults({ headers: { authorization: "token " + this.token } });
/** The Octokit instance actually performing API requests. */
this._octokit = new rest.Octokit({ token: this.token });
this.pulls = this._octokit.pulls;
this.repos = this._octokit.repos;
this.issues = this._octokit.issues;
this.git = this._octokit.git;
this.paginate = this._octokit.paginate;
this.rateLimit = this._octokit.rateLimit;
this._octokit.hook.error('request', function (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);
});
// Note: The prototype must be set explictly as Github's Octokit class is a non-standard class
// definition which adjusts the prototype chain.
// See:
// https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work
// https://github.com/octokit/rest.js/blob/7b51cee4a22b6e52adcdca011f93efdffa5df998/lib/constructor.js
Object.setPrototypeOf(_this, GithubClient.prototype);
return _this;
}
/** Perform a query using Github's Graphql API. */
GithubClient.prototype.graphql = function (queryObject, params) {
Expand All @@ -112,31 +107,8 @@ var GithubClient = /** @class */ (function (_super) {
});
});
};
/** Retrieve the login of the current user from Github. */
GithubClient.prototype.getCurrentUser = function () {
return tslib.__awaiter(this, void 0, void 0, function () {
var result;
return tslib.__generator(this, function (_a) {
switch (_a.label) {
case 0:
// If the current user has already been retrieved return the current user value again.
if (this._currentUser !== null) {
return [2 /*return*/, this._currentUser];
}
return [4 /*yield*/, this.graphql({
viewer: {
login: typedGraphqlify.types.string,
}
})];
case 1:
result = _a.sent();
return [2 /*return*/, this._currentUser = result.viewer.login];
}
});
});
};
return GithubClient;
}(Octokit));
}());

/**
* @license
Expand Down
59 changes: 16 additions & 43 deletions dev-infra/ng-dev.js
Expand Up @@ -12,7 +12,7 @@ var path = require('path');
var child_process = require('child_process');
var semver = require('semver');
var graphql = require('@octokit/graphql');
var Octokit = require('@octokit/rest');
var rest = require('@octokit/rest');
var typedGraphqlify = require('typed-graphqlify');
var url = require('url');
var fetch = _interopDefault(require('node-fetch'));
Expand Down Expand Up @@ -231,32 +231,27 @@ var GithubGraphqlClientError = /** @class */ (function (_super) {
* Additionally, provides convenience methods for actions which require multiple requests, or
* would provide value from memoized style responses.
**/
var GithubClient = /** @class */ (function (_super) {
tslib.__extends(GithubClient, _super);
var GithubClient = /** @class */ (function () {
/**
* @param token The github authentication token for Github Rest and Graphql API requests.
*/
function GithubClient(token) {
var _this =
// Pass in authentication token to base Octokit class.
_super.call(this, { auth: token }) || this;
_this.token = token;
/** The current user based on checking against the Github API. */
_this._currentUser = null;
this.token = token;
/** The graphql instance with authentication set during construction. */
_this._graphql = graphql.graphql.defaults({ headers: { authorization: "token " + _this.token } });
_this.hook.error('request', function (error) {
this._graphql = graphql.graphql.defaults({ headers: { authorization: "token " + this.token } });
/** The Octokit instance actually performing API requests. */
this._octokit = new rest.Octokit({ token: this.token });
this.pulls = this._octokit.pulls;
this.repos = this._octokit.repos;
this.issues = this._octokit.issues;
this.git = this._octokit.git;
this.paginate = this._octokit.paginate;
this.rateLimit = this._octokit.rateLimit;
this._octokit.hook.error('request', function (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);
});
// Note: The prototype must be set explictly as Github's Octokit class is a non-standard class
// definition which adjusts the prototype chain.
// See:
// https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work
// https://github.com/octokit/rest.js/blob/7b51cee4a22b6e52adcdca011f93efdffa5df998/lib/constructor.js
Object.setPrototypeOf(_this, GithubClient.prototype);
return _this;
}
/** Perform a query using Github's Graphql API. */
GithubClient.prototype.graphql = function (queryObject, params) {
Expand All @@ -275,31 +270,8 @@ var GithubClient = /** @class */ (function (_super) {
});
});
};
/** Retrieve the login of the current user from Github. */
GithubClient.prototype.getCurrentUser = function () {
return tslib.__awaiter(this, void 0, void 0, function () {
var result;
return tslib.__generator(this, function (_a) {
switch (_a.label) {
case 0:
// If the current user has already been retrieved return the current user value again.
if (this._currentUser !== null) {
return [2 /*return*/, this._currentUser];
}
return [4 /*yield*/, this.graphql({
viewer: {
login: typedGraphqlify.types.string,
}
})];
case 1:
result = _a.sent();
return [2 /*return*/, this._currentUser = result.viewer.login];
}
});
});
};
return GithubClient;
}(Octokit));
}());

/**
* @license
Expand Down Expand Up @@ -882,7 +854,8 @@ const versionBranchNameRegex = /^(\d+)\.(\d+)\.x$/;
function getVersionOfBranch(repo, branchName) {
return tslib.__awaiter(this, void 0, void 0, function* () {
const { data } = yield repo.api.repos.getContents({ owner: repo.owner, repo: repo.name, path: '/package.json', ref: branchName });
const { version } = JSON.parse(Buffer.from(data.content, 'base64').toString());
const content = Array.isArray(data) ? '' : data.content || '';
const { version } = JSON.parse(Buffer.from(content, 'base64').toString());
const parsedVersion = semver.parse(version);
if (parsedVersion === null) {
throw Error(`Invalid version detected in following branch: ${branchName}.`);
Expand Down
9 changes: 5 additions & 4 deletions dev-infra/pr/merge/strategies/api-merge.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {PullsListCommitsResponse, PullsMergeParams} from '@octokit/rest';
import {Octokit} from '@octokit/rest';
import {prompt} from 'inquirer';

import {parseCommitMessage} from '../../../commit-message/parse';
Expand Down Expand Up @@ -74,7 +74,7 @@ export class GithubApiMergeStrategy extends MergeStrategy {
return failure;
}

const mergeOptions: PullsMergeParams = {
const mergeOptions: Octokit.PullsMergeParams = {
pull_number: prNumber,
merge_method: method,
...this.git.remoteParams,
Expand Down Expand Up @@ -159,7 +159,8 @@ export class GithubApiMergeStrategy extends MergeStrategy {
* strategy, we cannot start an interactive rebase because we merge using the Github API.
* The Github API only allows modifications to PR title and body for squash merges.
*/
private async _promptCommitMessageEdit(pullRequest: PullRequest, mergeOptions: PullsMergeParams) {
private async _promptCommitMessageEdit(
pullRequest: PullRequest, mergeOptions: Octokit.PullsMergeParams) {
const commitMessage = await this._getDefaultSquashCommitMessage(pullRequest);
const {result} = await prompt<{result: string}>({
type: 'editor',
Expand Down Expand Up @@ -197,7 +198,7 @@ export class GithubApiMergeStrategy extends MergeStrategy {
private async _getPullRequestCommitMessages({prNumber}: PullRequest) {
const request = this.git.github.pulls.listCommits.endpoint.merge(
{...this.git.remoteParams, pull_number: prNumber});
const allCommits: PullsListCommitsResponse = await this.git.github.paginate(request);
const allCommits: Octokit.PullsListCommitsResponse = await this.git.github.paginate(request);
return allCommits.map(({commit}) => commit.message);
}

Expand Down
2 changes: 1 addition & 1 deletion dev-infra/release/publish/pull-request-state.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import * as Octokit from '@octokit/rest';
import {Octokit} from '@octokit/rest';
import {GitClient} from '../../utils/git/index';

/** Thirty seconds in milliseconds. */
Expand Down
3 changes: 2 additions & 1 deletion dev-infra/release/versioning/version-branches.ts
Expand Up @@ -35,7 +35,8 @@ export async function getVersionOfBranch(
repo: GithubRepoWithApi, branchName: string): Promise<semver.SemVer> {
const {data} = await repo.api.repos.getContents(
{owner: repo.owner, repo: repo.name, path: '/package.json', ref: branchName});
const {version} = JSON.parse(Buffer.from(data.content, 'base64').toString()) as
const content = Array.isArray(data) ? '' : data.content || '';
const {version} = JSON.parse(Buffer.from(content, 'base64').toString()) as
{version: string, [key: string]: any};
const parsedVersion = semver.parse(version);
if (parsedVersion === null) {
Expand Down
41 changes: 12 additions & 29 deletions dev-infra/utils/git/github.ts
Expand Up @@ -7,9 +7,9 @@
*/

import {graphql} from '@octokit/graphql';
import * as Octokit from '@octokit/rest';
import {Octokit} from '@octokit/rest';
import {RequestParameters} from '@octokit/types';
import {query, types} from 'typed-graphqlify';
import {query} from 'typed-graphqlify';

/**
* An object representation of a Graphql Query to be used as a response type and
Expand Down Expand Up @@ -41,31 +41,21 @@ export class GithubGraphqlClientError extends Error {}
* Additionally, provides convenience methods for actions which require multiple requests, or
* would provide value from memoized style responses.
**/
export class GithubClient extends Octokit {
/** The current user based on checking against the Github API. */
private _currentUser: string|null = null;
export class GithubClient {
/** The graphql instance with authentication set during construction. */
private _graphql = graphql.defaults({headers: {authorization: `token ${this.token}`}});
/** The Octokit instance actually performing API requests. */
private _octokit = new Octokit({token: this.token});

/**
* @param token The github authentication token for Github Rest and Graphql API requests.
*/
constructor(private token?: string) {
// Pass in authentication token to base Octokit class.
super({auth: token});

this.hook.error('request', error => {
this._octokit.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);
});

// Note: The prototype must be set explictly as Github's Octokit class is a non-standard class
// definition which adjusts the prototype chain.
// See:
// https://github.com/Microsoft/TypeScript/wiki/FAQ#why-doesnt-extending-built-ins-like-error-array-and-map-work
// https://github.com/octokit/rest.js/blob/7b51cee4a22b6e52adcdca011f93efdffa5df998/lib/constructor.js
Object.setPrototypeOf(this, GithubClient.prototype);
}

/** Perform a query using Github's Graphql API. */
Expand All @@ -78,17 +68,10 @@ export class GithubClient extends Octokit {
return (await this._graphql(query(queryObject).toString(), params)) as T;
}

/** 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;
}
const result = await this.graphql({
viewer: {
login: types.string,
}
});
return this._currentUser = result.viewer.login;
}
pulls = this._octokit.pulls;
repos = this._octokit.repos;
issues = this._octokit.issues;
git = this._octokit.git;
paginate = this._octokit.paginate;
rateLimit = this._octokit.rateLimit;
}
2 changes: 1 addition & 1 deletion dev-infra/utils/git/index.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import * as Octokit from '@octokit/rest';
import {Octokit} from '@octokit/rest';
import {spawnSync, SpawnSyncOptions, SpawnSyncReturns} from 'child_process';
import {Options as SemVerOptions, parse, SemVer} from 'semver';

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -62,7 +62,7 @@
"@bazel/terser": "3.5.0",
"@bazel/typescript": "3.5.0",
"@microsoft/api-extractor": "7.7.11",
"@octokit/rest": "16.28.7",
"@octokit/rest": "16.43.2",
"@octokit/types": "^6.0.0",
"@schematics/angular": "12.0.1",
"@types/angular": "^1.6.47",
Expand Down

0 comments on commit aeb4072

Please sign in to comment.