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

Update google-auth-library (and others) #229

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
5 changes: 2 additions & 3 deletions lib/oauth/google-oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as fs from 'fs';
import * as path from 'path';

const GoogleAuth = require('google-auth-library');
const { OAuth2Client } = require('google-auth-library');
const readlineSync = require('readline-sync');

const { getMessage } = require('../utils/messages');
Expand Down Expand Up @@ -47,8 +47,7 @@ class GoogleOauth {
const clientSecret = credentials.installed.client_secret;
const clientId = credentials.installed.client_id;
const redirectUrl = credentials.installed.redirect_uris[0];
const auth = new GoogleAuth();
const oauth2Client: Oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
const oauth2Client: Oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);

try {
const token = this.getToken();
Expand Down
78 changes: 44 additions & 34 deletions lib/sheets/gsheets.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
// Copyright 2016 Google Inc. All Rights Reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE

const google = require('googleapis');
const promisify = require('micro-promisify');
const {google} = require('googleapis');

const { getMessage } = require('../utils/messages');

import { Oauth2Client, GSheetsAppendResultsOptions, GSheetsValuesToAppend } from '../../types/types';
import { Logger } from '../utils/logger';
const logger = Logger.getInstance();

async function getRange(auth: Oauth2Client, range: number, spreadsheetId: string): Promise<Array<GSheetsValuesToAppend>> {
try {
const sheets = google.sheets('v4');
const response = await promisify(sheets.spreadsheets.values.get)({
auth: auth,
spreadsheetId: spreadsheetId,
range: range
});
return response.values;
} catch(error) {
logger.error(getMessage('G_SHEETS_API_ERROR', error));
throw new Error(error);
}
}

const formatValues = (values: Array<GSheetsValuesToAppend>) => {
let newValues = values.slice();
Expand All @@ -32,30 +17,55 @@ const formatValues = (values: Array<GSheetsValuesToAppend>) => {
}, []).join('');
};

async function appendResults(auth: Oauth2Client, valuesToAppend: Array<GSheetsValuesToAppend>, options:GSheetsAppendResultsOptions):Promise<any> {
try {
const sheets = google.sheets('v4');
// clone values to append
const values = Object.assign([], valuesToAppend);
logger.log(getMessage('G_SHEETS_APPENDING', formatValues(valuesToAppend)));
export class GSheets {
private sheets: any;
private auth: Oauth2Client;

constructor() {
this.sheets = google.sheets('v4');
}

public async getRange(range: number, spreadsheetId: string): Promise<Array<GSheetsValuesToAppend>> {
try {
const response = await this.sheets.spreadsheets.values.get({
auth: this.auth,
spreadsheetId: spreadsheetId,
range: range
});
return response.data.values;
} catch(error) {
logger.error(getMessage('G_SHEETS_API_ERROR', error));
throw new Error(error);
}
}

const response = await promisify(sheets.spreadsheets.values.append)({
auth: auth,
async sendResultsToGoogle(valuesToAppend: Array<GSheetsValuesToAppend>, options:GSheetsAppendResultsOptions):Promise<any> {
logger.log(getMessage('G_SHEETS_APPENDING', formatValues(valuesToAppend)));
const response = await this.sheets.spreadsheets.values.append({
auth: this.auth,
spreadsheetId: options.spreadsheetId,
range: `${options.tableName}!A1:C1`,
valueInputOption: 'USER_ENTERED',
resource: {
values: values,
values: valuesToAppend,
},
});
const rangeValues: Array<GSheetsValuesToAppend> = await getRange(auth, response.updates.updatedRange, options.spreadsheetId);
logger.log(getMessage('G_SHEETS_APPENDED', formatValues(rangeValues)));
} catch(error) {
logger.error(getMessage('G_SHEETS_API_ERROR', error));
throw new Error(error);

return response;
}
}

export {
appendResults
};
public async appendResults (auth: Oauth2Client, valuesToAppend: Array<GSheetsValuesToAppend>, options:GSheetsAppendResultsOptions):Promise<any> {
this.auth = auth;
try {
// clone values to append
const values = Object.assign([], valuesToAppend);
const response = await this.sendResultsToGoogle(values, options);

const rangeValues: Array<GSheetsValuesToAppend> = await this.getRange(response.data.updates.updatedRange, options.spreadsheetId);
logger.log(getMessage('G_SHEETS_APPENDED', formatValues(rangeValues)));
} catch(error) {
logger.error(getMessage('G_SHEETS_API_ERROR', error));
throw new Error(error);
}
}
}
12 changes: 9 additions & 3 deletions lib/sheets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { SheetsConfig, MetricsResults, Oauth2Client, AuthorizeCredentials, GSheetsValuesToAppend } from '../../types/types';

const GoogleOauth = require('../oauth/google-oauth');
import * as gsheets from './gsheets';
const GSheets = require('./gsheets').GSheets;

// @todo add 'import' after moving all stuff to typescript
const { getMessage } = require('../utils/messages');
Expand Down Expand Up @@ -45,6 +45,12 @@ export class Sheets {
}
}

async getOauth() {
const googleOauth = new GoogleOauth();
const oauth: Oauth2Client = await googleOauth.authenticate(this.clientSecret);
return oauth;
}

async appendResultsToGSheets(results: Array<MetricsResults>) {
let valuesToAppend: Array<GSheetsValuesToAppend> = [];
results.forEach(data => {
Expand All @@ -64,8 +70,8 @@ export class Sheets {
});

try {
const googleOauth = new GoogleOauth();
const oauth: Oauth2Client = await googleOauth.authenticate(this.clientSecret);
const oauth = await this.getOauth();
const gsheets = new GSheets();
await gsheets.appendResults(oauth, valuesToAppend, this.config.options);
} catch(error) {
throw error;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@gribnoysup/wunderbar": "^2.2.1",
"@types/node": "*",
"chrome-launcher": "^0.10.2",
"google-auth-library": "^0.10.0",
"google-auth-library": "^5.10.0",
"googleapis": "^47.0.0",
"lighthouse": "^5.2.0",
"micro-promisify": "^0.1.1",
Expand All @@ -37,7 +37,7 @@
"mocha": "^3.2.0",
"sinon": "^1.17.7",
"sinon-chai": "^2.8.0",
"typescript": "3.0.1"
"typescript": "^3.8.0"
},
"repository": {
"type": "git",
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,17 @@ module.exports.normalizedExpectations = {
error: 6000,
},
};

module.exports.sheetsResponse = {
data: {
updates: {
updatedRange: 'A1:C1'
}
}
};

module.exports.valuesToAppend = [
['3.0.0-beta.0', 'https://www.paulirish.com/', '6/8/2018 3:36:07 PM', 1059.9194, 1575.1898999999999, 4329, 4180, 4180],
['3.0.0-beta.0', 'https://www.paulirish.com/', '6/8/2018 3:36:07 PM', 1059.9194, 1575.1898999999999, 4329, 4180, 4180],
['3.0.0-beta.0', 'https://www.paulirish.com/', '6/8/2018 3:36:07 PM', 1059.9194, 1575.1898999999999, 4329, 4180, 4180]
];
28 changes: 26 additions & 2 deletions test/sheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the Apache License, Version 2.0. See LICENSE
'use strict';

const GSheets = require('../lib/sheets/gsheets').GSheets;
const Sheets = require('../lib/sheets').Sheets;
const {getMessage} = require('../lib/utils/messages');

const {getMessage} = require('../lib/utils/messages');
const runOptions = require('./fixtures/run-options');
const {metricsResults, sheetsResponse, valuesToAppend} = require('./fixtures/mocks');

/* eslint-env mocha */
describe('Sheets', () => {
Expand Down Expand Up @@ -74,6 +76,28 @@ describe('Sheets', () => {
});

describe('appendResultsToGSheets', () => {
// @todo after refactoring lib/gsheets/gsheets

});

describe('gSheets', () => {
let oauthStub;
let spy;

beforeEach(() => {
oauthStub = sinon.stub(sheets, 'getOauth', () => {
return 'oauth';
});
sinon.stub(GSheets.prototype, 'sendResultsToGoogle', () => sheetsResponse);
spy = sinon.stub(GSheets.prototype, 'getRange', () => valuesToAppend);
});

afterEach(() => {
oauthStub.restore();
});

it('should at least consider running', async() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
it('should at least consider running', async() => {
it('should at least consider running', () => {

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems to work on my end but I'm not totally sure about it..

sheets.appendResults(metricsResults);
sinon.assert.calledOnce(spy);
});
});
});
Loading