Skip to content

Commit

Permalink
Issue mozilla#16 - Use API key to fetch sheet with release status
Browse files Browse the repository at this point in the history
We have changed the Quantum spreadsheet to be public and we now only
need an API key to make it work.
  • Loading branch information
armenzg committed Feb 16, 2018
1 parent 825eb66 commit 2917d88
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@ For the frontend code visit the [Firefox health dashboard](https://github.com/mo

[![Build Status](https://api.travis-ci.org/mozilla/firefox-health-backend.svg?branch=master)](https://travis-ci.org/mozilla/firefox-health-backend)

## Requirements

* Node
* Yarn (recommended)

## Setting project up

In your console:
```
yarn // To get the dependencies installed
yarn start // To start the server
```

### Providing a Google API key

The [notes](http://localhost:3000/api/perf/notes) API requires a `GOOGLE_API_KEY`
in order to access a Google Spreadsheet. In order for this API to work locally
you need to create an API key for it.

Follow these instructions

* Visit [Google's cloud dashboard](https://console.cloud.google.com/apis/dashboard)
* Create a new project
* Select "Enable APIs and services" and enable "Google Sheets API"
* Back on the dashboard, go to credentials and create credentials
* Do not use the wizard but select "API Key" from the drop down
* Name it something recognizable like "fx health local server"
* Start the backend like this:

```
GOOGLE_API_KEY=<created API key> yarn start
```
* Visit http://localhost:3000/api/perf/notes to verify it works

## Attributions

- heartbeat icon by Creative Stall from the Noun Project
2 changes: 1 addition & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
},
"env": {
"GAUTH_JSON": {
"GOOGLE_API_KEY": {
"required": true
},
"NPM_CONFIG_PRODUCTION": {
Expand Down
44 changes: 12 additions & 32 deletions src/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,11 @@ import getCalendar from './release/calendar';
// Project Dawn
// channels.splice(2, 1);

let jwtClient = null;
const authKey = process.env.GOOGLE_API_KEY;
const sheetsAPI = 'https://sheets.googleapis.com/v4/spreadsheets';
const getSpreadsheetValues = async ({ id, range }) => {
if (!process.env.GAUTH_JSON) {
console.log('You need to set GAUTH_JSON');
}
if (!jwtClient) {
const jwtKey = JSON.parse(process.env.GAUTH_JSON);
jwtClient = new google.auth.JWT(
jwtKey.client_email,
null,
jwtKey.private_key,
'https://spreadsheets.google.com/feeds',
null,
);
await new Promise(resolve => jwtClient.authorize(resolve));
}
const { values } = await new Promise((resolve) => {
const sheets = google.sheets('v4');
sheets.spreadsheets.values.get(
{
auth: jwtClient,
spreadsheetId: id,
range: range,
},
(err, response) => resolve(response),
);
});
const url = `${sheetsAPI}/${id}/values/${range}?key=${authKey}`;
const { values } = await fetchJson(url);
const headers = values.splice(0, 1).pop();
return values.reduce((criteria, entry) => {
const obj = {};
Expand Down Expand Up @@ -124,8 +102,9 @@ let notesCache = null;

router
.get('/notes', async (ctx) => {
if (process.env.GAUTH_JSON) {
if (process.env.GOOGLE_API_KEY) {
if (!notesCache) {
console.log('Fetching notes since it is not in the cache.');
notesCache = (await getSpreadsheetValues({
id: '1UMsy_sZkdgtElr2buwRtABuyA3GY6wNK_pfF01c890A',
range: 'Status!A1:F30',
Expand All @@ -134,16 +113,17 @@ router
return hash;
}, {});

// const result = await gsjson({
// spreadsheetId: '1UMsy_sZkdgtElr2buwRtABuyA3GY6wNK_pfF01c890A',
// worksheet: ['Status'],
// credentials: process.env.GAUTH_JSON,
// });
setTimeout(() => {
notesCache = null;
}, process.env.NODE_ENV === 'production' ? 1000 * 60 * 5 : 1000 * 60);
}
ctx.body = notesCache;
} else {
ctx.throw(
400,
'You need to set the GOOGLE_API_KEY for this endpoint to work. More info in ' +
'https://github.com/mozilla/firefox-health-backend/blob/master/README.md',
);
}
})
.get('/benchmark/startup', async (ctx) => {
Expand Down

0 comments on commit 2917d88

Please sign in to comment.