Skip to content

Commit

Permalink
feat(publisher-github): add debug support for Octokit (#2499)
Browse files Browse the repository at this point in the history
* chore(publisher-github): use recommended userAgent config option
  • Loading branch information
malept authored Sep 8, 2021
1 parent 0a4fed7 commit 73252c3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
6 changes: 5 additions & 1 deletion packages/publisher/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"license": "MIT",
"main": "dist/PublisherGithub.js",
"typings": "dist/PublisherGithub.d.ts",
"scripts": {
"test": "yarn test:base test/**/*_spec.ts",
"test:base": "cross-env TS_NODE_FILES=1 mocha --config ../../../.mocharc.js"
},
"devDependencies": {
"chai": "^4.3.3",
"mocha": "^9.0.1",
Expand All @@ -23,8 +27,8 @@
"@octokit/core": "^3.2.4",
"@octokit/rest": "^18.0.11",
"@octokit/types": "^6.1.2",
"debug": "^4.3.1",
"fs-extra": "^10.0.0",
"lodash": "^4.17.20",
"mime-types": "^2.1.25"
}
}
22 changes: 17 additions & 5 deletions packages/publisher/github/src/util/github.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import debug from 'debug';
import { Octokit } from '@octokit/rest';
import { OctokitOptions } from '@octokit/core/dist-types/types.d';
import { merge } from 'lodash';

const logInfo = debug('electron-forge:publisher:github:info');
const logDebug = debug('electron-forge:publisher:github:debug');

export default class GitHub {
private options: OctokitOptions;
Expand All @@ -12,10 +15,19 @@ export default class GitHub {
requireAuth: boolean = false,
options: OctokitOptions = {},
) {
this.options = merge(
options,
{ headers: { 'user-agent': 'Electron Forge' } },
);
const noOp = () => {};

this.options = {
...options,
log: {
debug: logDebug.enabled ? logDebug : noOp,
error: console.error,
info: logInfo.enabled ? logInfo : noOp,
warn: console.warn,
},
userAgent: 'Electron Forge',
};

if (authToken) {
this.token = authToken;
} else if (process.env.GITHUB_TOKEN) {
Expand Down
30 changes: 16 additions & 14 deletions packages/publisher/github/test/github_spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { expect } from 'chai';
import { Octokit } from '@octokit/rest';
import { OctokitOptions } from '@octokit/core/dist-types/types.d';
import proxyquire from 'proxyquire';
import sinon, { SinonSpy } from 'sinon';

Expand Down Expand Up @@ -37,6 +39,12 @@ describe('GitHub', () => {
});

describe('getGitHub', () => {
function getOptions(api: Octokit): OctokitOptions {
const { options } = api as any;
delete options.log;
return options;
}

it('should create a new GitHubAPI', () => {
const gh = new GitHub();
expect(gitHubSpy.callCount).to.equal(0);
Expand All @@ -50,42 +58,36 @@ describe('GitHub', () => {
});
const api = gh.getGitHub();

expect((api as any).options).to.deep.equal({
expect(getOptions(api)).to.deep.equal({
auth: '1234',
baseUrl: 'https://github.example.com:8443/enterprise',
headers: {
'user-agent': 'Electron Forge',
},
userAgent: 'Electron Forge',
});
});

it('should not override the user agent', () => {
const gh = new GitHub('1234', true, { headers: { 'user-agent': 'Something' } });
const gh = new GitHub('1234', true, { userAgent: 'Something' });
const api = gh.getGitHub();

expect((api as any).options.headers['user-agent']).to.equal('Electron Forge');
expect(getOptions(api).userAgent).to.equal('Electron Forge');
});

it('should authenticate if a token is present', () => {
const gh = new GitHub('token');
const api = gh.getGitHub();
gh.getGitHub();
expect((api as any).options).to.deep.equal({
expect(getOptions(api)).to.deep.equal({
auth: 'token',
headers: {
'user-agent': 'Electron Forge',
},
userAgent: 'Electron Forge',
});
});

it('should not authenticate if a token is not present', () => {
const gh = new GitHub();
const api = gh.getGitHub();
gh.getGitHub();
expect((api as any).options).to.deep.equal({
headers: {
'user-agent': 'Electron Forge',
},
expect(getOptions(api)).to.deep.equal({
userAgent: 'Electron Forge',
});
});

Expand Down

0 comments on commit 73252c3

Please sign in to comment.