From 05eb1758cc6681c996d57a3861696bd3c5a2b087 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 20 May 2022 13:30:54 -0700 Subject: [PATCH 1/3] fix: delete only excess data fixes #1091 --- src/exportToSheets.ts | 18 +++++++++++------- src/fetchServices.ts | 19 +++++++++++-------- test/test.exportToSheets.ts | 8 ++++++-- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/exportToSheets.ts b/src/exportToSheets.ts index 54df4083..f16426aa 100644 --- a/src/exportToSheets.ts +++ b/src/exportToSheets.ts @@ -75,14 +75,8 @@ export async function exportToSheets() { version: 'v4', auth: await fixtures.getClient(), }); - // clear the current text in the sheet except for the field labels - // on the first row. - await sheets.spreadsheets.values.clear({ - spreadsheetId, - range: 'A2:Z10000', - }); - // insert it into the sheet + // first update the data into the sheet await sheets.spreadsheets.values.batchUpdate({ spreadsheetId, requestBody: { @@ -95,4 +89,14 @@ export async function exportToSheets() { ], }, }); + + // then clear the excess data + const start = values.length + 1; + const end = start + 10000; + + await sheets.spreadsheets.values.clear({ + spreadsheetId, + range: `A${start}:Z${end}`, + }); + } diff --git a/src/fetchServices.ts b/src/fetchServices.ts index caf82967..d65885d4 100644 --- a/src/fetchServices.ts +++ b/src/fetchServices.ts @@ -167,14 +167,7 @@ export async function exportApisToSheets() { const values = await getResults(); values.unshift(['Service', 'Title', 'Group', 'HasSurface', 'InScope', 'ToS']); - // clear the current text in the sheet except for the field labels - // on the first row. - await sheets.spreadsheets.values.clear({ - spreadsheetId, - range: 'all_apis!A2:Z10000', - }); - - // insert it into the sheet + // first update the data into the sheet await sheets.spreadsheets.values.batchUpdate({ spreadsheetId: spreadsheetId, requestBody: { @@ -187,6 +180,16 @@ export async function exportApisToSheets() { ], }, }); + + // then clear the excess data + const start = values.length + 1; + const end = start + 10000; + + await sheets.spreadsheets.values.clear({ + spreadsheetId, + range: `all_apis!A${start}:Z${end}`, + }); + } /** diff --git a/test/test.exportToSheets.ts b/test/test.exportToSheets.ts index 8a809da4..27c0acdb 100644 --- a/test/test.exportToSheets.ts +++ b/test/test.exportToSheets.ts @@ -70,10 +70,14 @@ describe('exportToSheets', () => { sandbox.stub(fixtures, 'getClient').resolves(jwt); const sheetPath = '/v4/spreadsheets/1VV5Clqstgoeu1qVwpbKkYOxwEgjvhMhSkVCBLMqg24M'; + + // The test data has one row plus the file name row. + const start = 3; + const end = start + 10000; const scope = nock('https://sheets.googleapis.com') - .post(`${sheetPath}/values/A2%3AZ10000:clear`) - .reply(200) .post(`${sheetPath}/values:batchUpdate`) + .reply(200) + .post(`${sheetPath}/values/A${start}%3AZ${end}:clear`) .reply(200); await exportToSheets(); scope.done(); From eeb6282ba72f8f4e5b2f21175b5d70114c17bc56 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 20 May 2022 13:33:40 -0700 Subject: [PATCH 2/3] lint fix --- src/exportToSheets.ts | 1 - src/fetchServices.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/src/exportToSheets.ts b/src/exportToSheets.ts index f16426aa..862527d7 100644 --- a/src/exportToSheets.ts +++ b/src/exportToSheets.ts @@ -98,5 +98,4 @@ export async function exportToSheets() { spreadsheetId, range: `A${start}:Z${end}`, }); - } diff --git a/src/fetchServices.ts b/src/fetchServices.ts index d65885d4..9c230071 100644 --- a/src/fetchServices.ts +++ b/src/fetchServices.ts @@ -189,7 +189,6 @@ export async function exportApisToSheets() { spreadsheetId, range: `all_apis!A${start}:Z${end}`, }); - } /** From 4cee415b3c340a1c4b2031b73ab3edd212f92e1b Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Fri, 20 May 2022 14:58:39 -0700 Subject: [PATCH 3/3] address review comments --- src/exportToSheets.ts | 3 ++- src/fetchServices.ts | 3 ++- src/util.ts | 2 ++ test/test.exportToSheets.ts | 3 ++- 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/exportToSheets.ts b/src/exportToSheets.ts index 862527d7..8ce77367 100644 --- a/src/exportToSheets.ts +++ b/src/exportToSheets.ts @@ -15,6 +15,7 @@ import * as google from '@googleapis/sheets'; import {getIssues} from './issue'; import {Issue} from './types'; +import {NUMBER_TO_DELETE} from './util'; const spreadsheetId = '1VV5Clqstgoeu1qVwpbKkYOxwEgjvhMhSkVCBLMqg24M'; export const fixtures = { @@ -92,7 +93,7 @@ export async function exportToSheets() { // then clear the excess data const start = values.length + 1; - const end = start + 10000; + const end = start + NUMBER_TO_DELETE; await sheets.spreadsheets.values.clear({ spreadsheetId, diff --git a/src/fetchServices.ts b/src/fetchServices.ts index 9c230071..5948516d 100644 --- a/src/fetchServices.ts +++ b/src/fetchServices.ts @@ -21,6 +21,7 @@ import * as meow from 'meow'; import Table = require('cli-table'); import {allow, deny} from './services.json'; import * as CSV from 'csv-string'; +import {NUMBER_TO_DELETE} from './util'; const auth = new google.auth.GoogleAuth({ scopes: [ @@ -183,7 +184,7 @@ export async function exportApisToSheets() { // then clear the excess data const start = values.length + 1; - const end = start + 10000; + const end = start + NUMBER_TO_DELETE; await sheets.spreadsheets.values.clear({ spreadsheetId, diff --git a/src/util.ts b/src/util.ts index 6cf3439f..902e5858 100644 --- a/src/util.ts +++ b/src/util.ts @@ -27,3 +27,5 @@ export async function gethub(options: GaxiosOptions) { }; return request(options); } + +export const NUMBER_TO_DELETE = 10000; diff --git a/test/test.exportToSheets.ts b/test/test.exportToSheets.ts index 27c0acdb..228672fb 100644 --- a/test/test.exportToSheets.ts +++ b/test/test.exportToSheets.ts @@ -24,6 +24,7 @@ process.env.DRIFT_API_KEY = 'not-a-key'; import {exportToSheets, fixtures} from '../src/exportToSheets'; import * as issues from '../src/issue'; +import {NUMBER_TO_DELETE} from '../src/util'; nock.disableNetConnect(); const sandbox = sinon.createSandbox(); @@ -73,7 +74,7 @@ describe('exportToSheets', () => { // The test data has one row plus the file name row. const start = 3; - const end = start + 10000; + const end = start + NUMBER_TO_DELETE; const scope = nock('https://sheets.googleapis.com') .post(`${sheetPath}/values:batchUpdate`) .reply(200)