Skip to content
Open
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
7 changes: 4 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"presets": ["@babel/preset-env"],
"plugins": [
["module-resolver", {
"alias": {
"test": "./test"
},
"root": ["./src"]
}]
],
"presets": ["es2015-node", "stage-1"]
}],
["lodash"]
]
}
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"lint-staged"
],
"dependencies": {
"@octokit/rest": "^15.9.2",
"@octokit/rest": "^19.0.5",
"bluebird": "^3.3.1",
"fs": "^0.0.2",
"inquirer": "^6.0.0",
Expand All @@ -54,19 +54,19 @@
"yargs": "^11.0.0"
},
"devDependencies": {
"babel-cli": "^6.4.0",
"babel-jest": "^23.2.0",
"babel-plugin-module-resolver": "^3.1.1",
"babel-preset-es2015-node": "^6.1.1",
"babel-preset-stage-1": "^6.24.1",
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/node": "^7.20.7",
"@babel/preset-env": "^7.20.2",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-module-resolver": "^4.1.0",
"coveralls": "^3.0.1",
"eslint": "5.0.1",
"eslint-config-seegno": "10.0.0",
"faker": "^4.1.0",
"jest": "^23.2.0",
"jest": "^27.0.6",
"lint-staged": "7.2.0",
"pre-commit": "^1.2.2",
"standard-http-error": "^2.0.0"
"pre-commit": "^1.2.2"
},
"engines": {
"node": "^6.14.0 || ^8.10.0 || >=9.10.0"
Expand Down
Empty file modified src/bin/labels.js
100644 → 100755
Empty file.
22 changes: 10 additions & 12 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
* Module dependencies.
*/

import { Octokit } from '@octokit/rest';
import { differenceBy, get, has } from 'lodash';
import Github from '@octokit/rest';

/**
* GitHub configuration.
* GitHub timeout default value.
*/

const config = {
debug: false,
timeout: 5000,
version: '3.0.0'
};
const defaultTimeout = 5000;

/**
* Get repository owner and name and include a validation.
Expand All @@ -41,9 +37,11 @@ export default class Client {
*/

constructor({ token, ...options }) {
this.github = new Github({ ...config, ...options });

this.github.authenticate({ token, type: 'oauth' });
this.github = new Octokit({
auth: token,
request: { timeout: defaultTimeout },
...options
});
}

/**
Expand Down Expand Up @@ -71,7 +69,7 @@ export default class Client {
try {
label = await this.getLabel(repository, name);
} catch (err) {
if (!has(err, 'code') || get(err, 'code') !== 404) {
if (!has(err, 'status') || get(err, 'status') !== 404) {
throw err;
}
}
Expand Down Expand Up @@ -104,7 +102,7 @@ export default class Client {
async getLabels(repository) {
const { owner, repo } = getRepositoryOptions(repository);

const result = await this.github.issues.getLabels({
const result = await this.github.issues.listLabelsForRepo({
owner,
repo
});
Expand Down
39 changes: 21 additions & 18 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
* Module dependencies.
*/

import { Octokit } from '@octokit/rest';
import Client from 'client';
import Github from '@octokit/rest';
import HttpError from 'standard-http-error';

/**
* Jest mocks.
Expand All @@ -21,7 +20,6 @@ describe('Client', () => {
const defaultOptions = { token: 'foobiz' };
const repository = 'corge/waldo';
const repositoryOptions = { owner: 'corge', repo: 'waldo' };
let authenticate;
let client;
let createLabel;
let deleteLabel;
Expand All @@ -31,7 +29,6 @@ describe('Client', () => {
let updateLabels;

beforeEach(() => {
authenticate = jest.fn();
createLabel = jest.fn();
deleteLabel = jest.fn();
getLabel = jest.fn();
Expand All @@ -46,19 +43,14 @@ describe('Client', () => {
updateLabels
};

Github.mockImplementation(() => ({ authenticate, issues }));
Octokit.mockImplementation(() => ({ issues }));

client = new Client(defaultOptions);
});

describe('constructor', () => {
it('should create an instance of `client` with given arguments', () => {
expect(client.github).toEqual({ authenticate, issues });
});

it('should call `github.authenticate` with given token and type as `oauth`', () => {
expect(client.github.authenticate).toHaveBeenCalledTimes(1);
expect(client.github.authenticate).toHaveBeenCalledWith({ token: 'foobiz', type: 'oauth' });
expect(client.github).toEqual({ issues });
});
});

Expand Down Expand Up @@ -96,19 +88,30 @@ describe('Client', () => {
});

it('should throw if `getLabel` throws an error with code different than 404', async () => {
client.getLabel = jest.fn(() => { throw new HttpError(100); });
client.getLabel = jest.fn(() => {
const err = new Error('');

err.status = 100;
throw err;
});

try {
await client.createOrUpdateLabel(repository);

fail();
} catch (e) {
expect(e.code).toBe(100);
expect(e.status).toBe(100);
}
});

it('should call `createLabel` with given `repository`, `name` and `color` if `getLabel` throws an error with code 404', async () => {
client.getLabel = jest.fn(() => { throw new HttpError(404); });
client.getLabel = jest.fn(() => {
const err = new Error('404 not found');

err.status = 404;
throw err;
});

Choose a reason for hiding this comment

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

Add a break line here.


client.updateLabel = jest.fn();
client.createLabel = jest.fn(() => Promise.resolve('foobiz'));

Expand Down Expand Up @@ -179,15 +182,15 @@ describe('Client', () => {
});

it('should call `github.issues.getLabels` with repository options', () => {
client.github.issues.getLabels = jest.fn(() => true);
client.github.issues.listLabelsForRepo = jest.fn(() => true);
client.getLabels(repository, 'foobar');

expect(client.github.issues.getLabels).toHaveBeenCalledTimes(1);
expect(client.github.issues.getLabels).toHaveBeenCalledWith({ ...repositoryOptions });
expect(client.github.issues.listLabelsForRepo).toHaveBeenCalledTimes(1);
expect(client.github.issues.listLabelsForRepo).toHaveBeenCalledWith({ ...repositoryOptions });
});

it('should return the data property from the result of the call `github.issues.getLabels`', () => {
client.github.issues.getLabels = jest.fn(() => Promise.resolve({ data: 'foobar' }));
client.github.issues.listLabelsForRepo = jest.fn(() => Promise.resolve({ data: 'foobar' }));

return client.getLabels(repository).then(label => {
expect(label).toBe('foobar');
Expand Down
Loading