Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates CLI to use '@octokit/rest' instead of 'github'. #154

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 101 additions & 26 deletions packages/cli/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"author": "The Polymer Project Authors",
"license": "BSD-3-Clause",
"dependencies": {
"@octokit/rest": "^15.2.6",
"@types/chalk": "^0.4.31",
"@types/del": "^3.0.0",
"@types/findup-sync": "^0.3.29",
Expand Down Expand Up @@ -54,7 +55,6 @@
"command-line-usage": "^3.0.1",
"del": "^3.0.0",
"findup-sync": "^0.4.2",
"github": "^7.2.1",
"globby": "^8.0.1",
"gunzip-maybe": "^1.3.1",
"inquirer": "^1.0.2",
Expand Down
26 changes: 16 additions & 10 deletions packages/cli/src/github/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import * as semver from 'semver';

import request = require('request');
import rimraf = require('rimraf');
import GitHubApi = require('github');
import Octokit = require('@octokit/rest');

const gunzip = require('gunzip-maybe');
const tar = require('tar-fs');
Expand Down Expand Up @@ -52,14 +52,14 @@ export interface GithubOpts {
owner: string;
repo: string;
githubToken?: string;
githubApi?: GitHubApi;
githubApi?: Octokit;
requestApi?: request
.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
}

export class Github {
private _token: string|null;
private _github: GitHubApi;
private _github: Octokit;
private _request: request
.RequestAPI<request.Request, request.CoreOptions, request.RequiredUriUrl>;
private _owner: string;
Expand All @@ -77,9 +77,7 @@ export class Github {
this._token = opts.githubToken || Github.tokenFromFile('token');
this._owner = opts.owner;
this._repo = opts.repo;
this._github = opts.githubApi || new GitHubApi({
protocol: 'https',
});
this._github = opts.githubApi || new Octokit();
if (this._token != null) {
this._github.authenticate({
type: 'oauth',
Expand Down Expand Up @@ -156,21 +154,29 @@ export class Github {
* Get all Github releases and match their tag names against the given semver
* range. Return the release with the latest possible match.
*/
async getSemverRelease(semverRange: string): Promise<GitHubApi.Release> {
async getSemverRelease(semverRange: string): Promise<any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching from types to any is a pretty big regression. I'd rather not update the library if that's the primary change.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you say to manually adding types here, casting the AnyResponse to a custom type where data is something that we've defined as not any?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's an expectation that the newer version of the library is better then I'm on board

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Note that we only see the 100 most recent releases. If we ever release
// enough versions that this becomes a concern, we'll need to improve this
// call to request multiple pages of results.
const releases: GitHubApi.Release[] = await this._github.repos.getReleases({
const response: Octokit.AnyResponse = await this._github.repos.getReleases({
owner: this._owner,
repo: this._repo,
per_page: 100,
});

const status = response.meta.status;
if (status !== '200 OK') {
throw new Error(`Failed to retrieve releases from GitHub. (${status})`);
}

const releases = response.data;

const validReleaseVersions =
releases.filter((r) => semver.valid(r.tag_name)).map((r) => r.tag_name);
releases.filter((r: any) => semver.valid(r.tag_name)).map((r: any) => r.tag_name);
const maxSatisfyingReleaseVersion =
semver.maxSatisfying(validReleaseVersions, semverRange);
const maxSatisfyingRelease =
releases.find((r) => r.tag_name === maxSatisfyingReleaseVersion);
releases.find((r: any) => r.tag_name === maxSatisfyingReleaseVersion);
if (!maxSatisfyingRelease) {
throw new Error(`${this._owner}/${this._repo} has no releases matching ${
semverRange}.`);
Expand Down
41 changes: 31 additions & 10 deletions packages/cli/src/test/unit/github/github_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,27 @@ suite('github/github', () => {
let getReleasesStub: sinon.SinonStub;
let github: Github;

const basicReleasesResponse = [
{tag_name: 'v1.0.0'},
{tag_name: 'v1.1.0'},
{tag_name: 'v1.2.1'},
{tag_name: 'v2.0.0'},
{tag_name: 'v2.0.0-pre.1'},
{tag_name: 'v2.0.1'},
{tag_name: 'v2.1.0'},
{tag_name: 'TAG_NAME_WITHOUT_VERSION'},
];
const basicReleasesResponse = {
meta: {
status: '200 OK',
},
data: [
{tag_name: 'v1.0.0'},
{tag_name: 'v1.1.0'},
{tag_name: 'v1.2.1'},
{tag_name: 'v2.0.0'},
{tag_name: 'v2.0.0-pre.1'},
{tag_name: 'v2.0.1'},
{tag_name: 'v2.1.0'},
{tag_name: 'TAG_NAME_WITHOUT_VERSION'},
],
};

const failedReleasesResponse = {
meta: {
status: '500 Internal Server Error',
},
};

setup(() => {
getReleasesStub = sinon.stub();
Expand Down Expand Up @@ -200,6 +211,16 @@ suite('github/github', () => {
err.message,
'TEST_OWNER/TEST_REPO has no releases matching ^v3.0.0.');
});

testName = 'rejects with an error if the status is not "200 OK"';
test(testName, async () => {
getReleasesStub.returns(Promise.resolve(failedReleasesResponse));

const err = await invertPromise(github.getSemverRelease('^v3.0.0'));
assert.equal(
err.message,
'Failed to retrieve releases from GitHub. (500 Internal Server Error)');
});
});

});