Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

fix: delete only excess data #1092

Merged
merged 3 commits into from
May 23, 2022
Merged
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
18 changes: 11 additions & 7 deletions src/exportToSheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -75,14 +76,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: {
Expand All @@ -95,4 +90,13 @@ export async function exportToSheets() {
],
},
});

// then clear the excess data
const start = values.length + 1;
const end = start + NUMBER_TO_DELETE;

await sheets.spreadsheets.values.clear({
spreadsheetId,
range: `A${start}:Z${end}`,
tmatsuo marked this conversation as resolved.
Show resolved Hide resolved
});
}
19 changes: 11 additions & 8 deletions src/fetchServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -167,14 +168,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: {
Expand All @@ -187,6 +181,15 @@ export async function exportApisToSheets() {
],
},
});

// then clear the excess data
const start = values.length + 1;
const end = start + NUMBER_TO_DELETE;

await sheets.spreadsheets.values.clear({
spreadsheetId,
range: `all_apis!A${start}:Z${end}`,
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ export async function gethub<T = unknown>(options: GaxiosOptions) {
};
return request<T>(options);
}

export const NUMBER_TO_DELETE = 10000;
9 changes: 7 additions & 2 deletions test/test.exportToSheets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -70,10 +71,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 + NUMBER_TO_DELETE;
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();
Expand Down