From 77e771f783f4470bb1fd0a6aad637a9361e11c50 Mon Sep 17 00:00:00 2001 From: Elwin Schmitz Date: Tue, 21 Nov 2023 16:05:47 +0100 Subject: [PATCH 1/3] feat: Use GitHub for login (to test now) --- .../local-data/staticwebapp.config.json | 11 ++++------- .github/workflows/poc-deploy-private-faq.yml | 14 +++++++++----- src/ngsw-config.json | 3 ++- 3 files changed, 15 insertions(+), 13 deletions(-) rename staticwebapp.config_private.json => .azure/local-data/staticwebapp.config.json (90%) diff --git a/staticwebapp.config_private.json b/.azure/local-data/staticwebapp.config.json similarity index 90% rename from staticwebapp.config_private.json rename to .azure/local-data/staticwebapp.config.json index 9a3775ea..0a819a64 100644 --- a/staticwebapp.config_private.json +++ b/.azure/local-data/staticwebapp.config.json @@ -38,10 +38,6 @@ "route": "/.auth/login/twitter", "statusCode": 404 }, - { - "route": "/.auth/login/github", - "statusCode": 404 - }, { "route": "/logout", "redirect": "/.auth/logout" @@ -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": { diff --git a/.github/workflows/poc-deploy-private-faq.yml b/.github/workflows/poc-deploy-private-faq.yml index b99892d1..17ad9916 100644 --- a/.github/workflows/poc-deploy-private-faq.yml +++ b/.github/workflows/poc-deploy-private-faq.yml @@ -40,12 +40,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 + 🔐 Login (with GitHub)\n + 🔐 Login (Microsoft-account)\n + \n + 🔓 Logout\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/' @@ -57,7 +61,7 @@ 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 @@ -80,7 +84,7 @@ 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 diff --git a/src/ngsw-config.json b/src/ngsw-config.json index 62a87d55..63a412b2 100644 --- a/src/ngsw-config.json +++ b/src/ngsw-config.json @@ -38,5 +38,6 @@ }, "urls": ["https://sheets.googleapis.com/v4/spreadsheets/*"] } - ] + ], + "navigationUrls": ["/**", "!/**/*.*", "!/login", "!/logout", "!/.auth/**"] } From af0a52127d27de5bc2c531313d4efc415f7625a2 Mon Sep 17 00:00:00 2001 From: Elwin Schmitz Date: Wed, 22 Nov 2023 10:22:48 +0100 Subject: [PATCH 2/3] feat: Add download-google-sheets build-step --- .azure/local-data/download-google-sheets.js | 74 ++++++++++++++++++++ .github/workflows/poc-deploy-private-faq.yml | 11 +-- .gitignore | 3 + src/ngsw-config.json | 13 +++- 4 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 .azure/local-data/download-google-sheets.js diff --git a/.azure/local-data/download-google-sheets.js b/.azure/local-data/download-google-sheets.js new file mode 100644 index 00000000..14cb02bd --- /dev/null +++ b/.azure/local-data/download-google-sheets.js @@ -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}`); +} diff --git a/.github/workflows/poc-deploy-private-faq.yml b/.github/workflows/poc-deploy-private-faq.yml index 17ad9916..2c867659 100644 --- a/.github/workflows/poc-deploy-private-faq.yml +++ b/.github/workflows/poc-deploy-private-faq.yml @@ -67,14 +67,15 @@ jobs: - 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 diff --git a/.gitignore b/.gitignore index 13afce37..c2e2f624 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ coverage # Third party code node_modules + +# Sensitive data +.credentials.json diff --git a/src/ngsw-config.json b/src/ngsw-config.json index 63a412b2..d26e9723 100644 --- a/src/ngsw-config.json +++ b/src/ngsw-config.json @@ -36,7 +36,18 @@ "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/**"] From 85b8abb75e57402551122025113069ef67f2bb88 Mon Sep 17 00:00:00 2001 From: Elwin Schmitz Date: Wed, 22 Nov 2023 12:06:21 +0100 Subject: [PATCH 3/3] feat: Make update-workflow trigger from Issue Comment --- .github/workflows/poc-deploy-private-faq.yml | 28 +++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/.github/workflows/poc-deploy-private-faq.yml b/.github/workflows/poc-deploy-private-faq.yml index 2c867659..26e3423e 100644 --- a/.github/workflows/poc-deploy-private-faq.yml +++ b/.github/workflows/poc-deploy-private-faq.yml @@ -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 @@ -91,16 +100,3 @@ jobs: 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'