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

PoC: Instance with private, local sheet data #881

Merged
merged 3 commits into from
Nov 22, 2023
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
74 changes: 74 additions & 0 deletions .azure/local-data/download-google-sheets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env node

const { google } = require('googleapis');
const fs = require('fs');

let credentials;

try {
// Required:
// GOOGLE_SHEETS_API_SERVICE_ACCOUNT='{"client_email":"user@example.org","private_key":"secret"}'
credentials = JSON.parse(process.env.GOOGLE_SHEETS_API_SERVICE_ACCOUNT);
} catch (error) {
console.error(
'Error parsing Google Sheets API service account credentials:',
error,
);
}

const auth = new google.auth.JWT(
credentials.client_email,
null,
credentials.private_key,
['https://www.googleapis.com/auth/spreadsheets.readonly'],
);

const sheets = google.sheets({
version: 'v4',
auth,
});

async function downloadSheet(spreadsheetId, range, outputFile) {
try {
const res = await sheets.spreadsheets.values.get({
spreadsheetId,
range,
});
const rows = res.data.values;
if (rows.length) {
fs.writeFileSync(outputFile, JSON.stringify(res.data));
console.log(`Google Sheet downloaded as: ${outputFile}`);
} else {
console.log('No data found.');
}
} catch (err) {
console.error('The API says: ', err);
}
}

//
// --------------------------------------------------------------------------
//

// Get variables from command-line arguments
const spreadsheetId = process.argv[2];

// Create the path(s) required to store the sheets
const path = `./www/data/${spreadsheetId}/values`;

if (!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true });
}

// See "SheetName"-enum in: ../src/app/services/spreadsheet.service.ts
const ranges = [
'Referral Page',
'Categories',
'Sub-Categories',
'Offers',
'Q&As',
];

for (const range of ranges) {
downloadSheet(spreadsheetId, range, `${path}/${range}`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
"route": "/.auth/login/twitter",
"statusCode": 404
},
{
"route": "/.auth/login/github",
"statusCode": 404
},
{
"route": "/logout",
"redirect": "/.auth/logout"
Expand All @@ -50,11 +46,12 @@
"trailingSlash": "auto",
"navigationFallback": {
"rewrite": "index.html",
"exclude": ["/assets/*", "/data/*"]
"exclude": ["/assets/*", "/data/*", "/login", "/logout", "/.auth/*"]
},
"responseOverrides": {
"404": {
"redirect": "/"
"401": {
"statusCode": 302,
"redirect": "/login"
}
},
"globalHeaders": {
Expand Down
53 changes: 27 additions & 26 deletions .github/workflows/poc-deploy-private-faq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ name: 'POC: Private FAQ [staging]'

on:
workflow_dispatch:
issue_comment:
types: [created]

jobs:
build_and_deploy_job:
if: github.event_name == 'workflow_dispatch' || github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
build_and_deploy:
if: >
github.event_name == 'workflow_dispatch' ||
(
github.event_name == 'issue_comment' &&
github.event.issue.number == vars.GHA_DEPLOY_TRIGGER_ISSUE &&
contains(github.event.issue.assignees.*.login, github.event.comment.user.login) &&
github.event.comment.body == '/update private-faq'
)
runs-on: ubuntu-latest
name: Build and Deploy Job
name: Build and Deploy
environment: 'staging-faq'
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -40,12 +49,16 @@ jobs:
NG_USE_Q_AND_AS: 'true'
NG_USE_Q_AND_A_SEARCH: 'true'
TXT_APP_NAME: 'Private FAQ'
TXT_APP_LOGO_URL: 'https://rodekruis.github.io/510-ICON-LIBRARY/Beeldmerk_van_het_Nederlandse_Rode_Kruis.svg'
TXT_APP_LOGO_URL: ''
TXT_MAIN_PAGE_HEADER: 'Private PoC'
TXT_MAIN_PAGE_INTRO: >-
Private FAQ only:\n
Private FAQ only!\n
\n
<a href="/.auth/login/github" class="action"><strong>🔐 Login (with GitHub)</strong></a>\n
<a href="/.auth/login/aad" class="action"><strong>🔐 Login (Microsoft-account)</strong></a>\n
\n
<a href="/logout" class="action"><strong>🔓 Logout</strong></a>\n
\n
`[ 🚧 = development ]`\n
TXT_ERROR_HEADER: 'Data Unavailable'
TXT_ERROR_MESSAGE: 'Reach out to us at: '
TXT_ERROR_CONTACT_URL: 'https://www.rodekruis.nl/'
Expand All @@ -57,20 +70,21 @@ jobs:
REGIONS_SHEET_IDS: >-
1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII,
GOOGLE_SHEETS_API_KEY: ${{ secrets.GOOGLE_SHEETS_API_KEY }}
GOOGLE_SHEETS_API_URL: '/data/'
GOOGLE_SHEETS_API_URL: '/data'
AI_CONNECTION_STRING: ''

- name: Set/override robots.txt
run: 'cp src/robots.private.txt www/robots.txt'

- name: Install Google Sheets API SDK
run: 'npm install googleapis@^128.0.0 --no-save'

- name: Download sheet data
env:
GOOGLE_SHEETS_API_SERVICE_ACCOUNT: ${{ secrets.GOOGLE_SHEETS_API_SERVICE_ACCOUNT }}
run: |
echo "Downloading sheet data..."
cp './data/test-sheet-id-1/values/Referral Page.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Referral Page'
cp './data/test-sheet-id-1/values/Categories.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Categories'
cp './data/test-sheet-id-1/values/Sub-Categories.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Sub-Categories'
cp './data/test-sheet-id-1/values/Offers.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Offers'
cp './data/test-sheet-id-1/values/Q&As.json' './www/data/1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII/Q&As'
node ./.azure/local-data/download-google-sheets.js 1QsmCBZ7j1sgOwo7sXjI8Jln_jw68oL1gyRvvzj3dHII
echo "Downloading sheet data: done ✅"

- name: Deploy to Private-Staging
Expand All @@ -80,22 +94,9 @@ jobs:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_WHITE_ROCK_069A8B403 }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: 'upload'
config_file_location: '/staticwebapp.config_private.json'
config_file_location: '.azure/local-data/'
app_location: 'www'
output_location: ''
skip_app_build: true
skip_api_build: true
deployment_environment: 'private'

close_pull_request_job:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
name: Close Pull Request Job
environment: 'staging-faq'
steps:
- name: Close Pull Request for Staging
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_WHITE_ROCK_069A8B403 }}
action: 'close'
deployment_environment: 'private'
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ coverage

# Third party code
node_modules

# Sensitive data
.credentials.json
16 changes: 14 additions & 2 deletions src/ngsw-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@
"maxAge": "7d",
"timeout": "8s"
},
"urls": ["https://sheets.googleapis.com/v4/spreadsheets/*"]
"urls": ["https://sheets.googleapis.com/v4/spreadsheets/**"]
},
{
"name": "localSheetsData",
"version": 1,
"cacheConfig": {
"strategy": "freshness",
"maxSize": 0,
"maxAge": "1u",
"timeout": "1u"
},
"urls": ["/data/**"]
}
]
],
"navigationUrls": ["/**", "!/**/*.*", "!/login", "!/logout", "!/.auth/**"]
}
Loading