Skip to content

Commit

Permalink
Implement quickadds query endpoint (#2747)
Browse files Browse the repository at this point in the history
* Forward graphql requests to netlify function

* Trigger Build

* update lockfile

* Avoid having to introspect schema for every request

* Allow anonymous read only queries

* Allow anonymous read only queries

* Trigger Build

* Add quickadds resolver

* Split resolvers into separate files

* Separate auth in its own file

* Add jest and tests

* Add script to boot local api server for development purposes

* Add quickadd insert field

* Add github workflows

* Trigger build

remove

Trigger build

Trigger build

fsdfsf

Trigger build

Trigger build

bigint patch

use require

* Update codegen tool config

* Reuse mongodb client from context

* Update codege

* Strongly typed environment config

* Increase function timeout

* Update bson version

* Make environment variables available to gatsby functions

* Fix ts error

* Trigger build

* Make errors more verbose

* Add missing variables

* Disable env validation

* Use graphql-http to prevent event emitter leaks

* Enable env validation

* New bson version exports ObjectId instead of ObjectID

* Update lockfile

* Trigger build

* Remove unused query

* bue

* Add support for filters, sorting and pagination

* Remove unused package

* Remove unused file

* allow api tests to be cancelled

* Delete old test file

* Run api tests when pushing staging

* update test utils

* Trigger build

* Force exit to let tests exit

* remove old api endpoint

* Update client side queries

* Udpate docs

* Use vars for api and group ids

* convert to import jic

* Trigger build

* Trigger build

* Update workflows

* Trigger build

* Add missing variables

* Fix error when object id is already serialized

* Update workflow file

* Improve error message

* update readme

* Remove unused vars

* Remove unused packages

* Log errors to rollbar

* Trigger build

* Add rollbar token variable

* Trigger build

* fix variable name

* Trigger build

* Add comment
  • Loading branch information
cesarvarela authored May 31, 2024
1 parent b1238bd commit 03afdc3
Show file tree
Hide file tree
Showing 42 changed files with 32,358 additions and 13,093 deletions.
28 changes: 24 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ jobs:
environment: ${{ inputs.environment }}
runs-on:
labels: ${{ inputs.runner-label || 'ubuntu-latest' }}
defaults:
run:
shell: bash
working-directory: site/gatsby-site
permissions:
pull-requests: write
steps:
- name: Cleanup Disk Space
run: |
echo "Before removing files:"
df -h
sudo rm -rf /usr/share/dotnet
sudo rm -rf /opt/ghc
sudo rm -rf "/usr/local/share/boost"
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
echo "After removing files:"
df -h
- name: Checkout
uses: actions/checkout@v4
with:
Expand All @@ -60,15 +67,18 @@ jobs:
- name: Install dependencies
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
run: npm ci
working-directory: site/gatsby-site

- name: Use deploy specific netlify.toml
run: mv deploy-netlify.toml netlify.toml
working-directory: site/gatsby-site

- name: Install Netlify CLI
run: npm install netlify-cli -g

- name: Build using Netlify
run: netlify build --context ${{ inputs.netlify-context }} --offline
working-directory: site/gatsby-site
env:
NETLIFY_SITE_ID: ${{ vars.NETLIFY_SITE_ID }}
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
Expand Down Expand Up @@ -102,6 +112,16 @@ jobs:
REALM_GRAPHQL_API_KEY: ${{ secrets.REALM_GRAPHQL_API_KEY }}
GATSBY_COMMIT_SHA: ${{ inputs.sha }}
TRANSLATE_DRY_RUN: ${{ vars.TRANSLATE_DRY_RUN }}
REALM_API_APP_ID: ${{ vars.REALM_API_APP_ID }}
REALM_API_GROUP_ID: ${{ vars.REALM_API_GROUP_ID }}
REALM_APP_ID: ${{ vars.GATSBY_REALM_APP_ID }}
REALM_API_PUBLIC_KEY: ${{ secrets.REALM_API_PUBLIC_KEY }}
REALM_API_PRIVATE_KEY: ${{ secrets.REALM_API_PRIVATE_KEY }}
ROLLBAR_POST_SERVER_ITEM_ACCESS_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }}

- name: Build size
run: |
du -ah . 2>/dev/null | sort -rh | head -n 20
- name: Upload to netlify
id: deploy-netlify
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ jobs:
echo "Job originally triggered by ${{ github.actor }}"
exit 1
call-test-api:
if: ${{ !failure() && !cancelled()}}
uses: ./.github/workflows/test-api.yml
needs: permissions-check
secrets: inherit
with:
sha: ${{ github.event.pull_request.head.sha }}
environment: staging

call-test-build:
if: ${{ !failure() && !cancelled() }}
uses: ./.github/workflows/test-build.yml
Expand Down
10 changes: 9 additions & 1 deletion .github/workflows/staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ jobs:
environment: staging
runner-label: ${{ vars.STAGING_RUNNER_LABEL }}

call-test-api:
uses: ./.github/workflows/test-api.yml
needs: cache-modifier
secrets: inherit
with:
sha: ${{ github.event.pull_request.head.sha }}
environment: staging

call-test-build:
uses: ./.github/workflows/test-build.yml
secrets: inherit
Expand All @@ -56,7 +64,7 @@ jobs:
call-deploy:
if: inputs.force-deploy == true || success()
uses: ./.github/workflows/deploy.yml
needs: [cache-modifier, call-test]
needs: [cache-modifier, call-test, call-test-api]
secrets: inherit
permissions:
pull-requests: write
Expand Down
76 changes: 76 additions & 0 deletions .github/workflows/test-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: Run API tests
on:
workflow_call:
inputs:
environment:
description: The Github environment to load secrets from
type: string
required: true
sha:
description: The commit SHA to run the tests against
type: string
required: true
runner-label:
description: The label of the runner to use
type: string
cache-modifier:
description: A modifier for the cache key used to bypass existing cache
type: string
required: false
default: ""

jobs:
test:
name: Run tests
environment: ${{ inputs.environment }}
runs-on:
labels: ${{ inputs.runner-label || 'ubuntu-latest' }}
defaults:
run:
shell: bash
working-directory: site/gatsby-site
steps:

- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ inputs.sha }}

- name: Read node modules from cache
id: cache-nodemodules
uses: actions/cache/restore@v4
env:
cache-name: cache-install-folder
with:
path: |
site/gatsby-site/node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}-${{ inputs.cache-modifier }}

- name: Install NPM dependencies
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
run: npm ci
working-directory: site/gatsby-site

- name: Extract branch name
shell: bash
run: echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_ENV
id: extract_branch

- name: Jest run
run: npm run test:api:ci
env:
REALM_APP_ID: ${{ vars.GATSBY_REALM_APP_ID }}
REALM_API_APP_ID: ${{ vars.REALM_API_APP_ID }}
REALM_API_GROUP_ID: ${{ vars.REALM_API_GROUP_ID }}
REALM_API_PUBLIC_KEY: ${{ secrets.REALM_API_PUBLIC_KEY }}
REALM_API_PRIVATE_KEY: ${{ secrets.REALM_API_PRIVATE_KEY }}
REALM_GRAPHQL_API_KEY: ${{ secrets.REALM_GRAPHQL_API_KEY }}
MONGODB_CONNECTION_STRING: ${{ secrets.MONGODB_CONNECTION_STRING }}
E2E_ADMIN_USERNAME: ${{ secrets.E2E_ADMIN_USERNAME }}
E2E_ADMIN_PASSWORD: ${{ secrets.E2E_ADMIN_PASSWORD }}
ROLLBAR_POST_SERVER_ITEM_ACCESS_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
6 changes: 6 additions & 0 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ jobs:
CLOUDFLARE_R2_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
REALM_GRAPHQL_API_KEY: ${{ secrets.REALM_GRAPHQL_API_KEY }}
GATSBY_COMMIT_SHA: ${{ inputs.sha }}
REALM_API_APP_ID: ${{ vars.REALM_API_APP_ID }}
REALM_API_GROUP_ID: ${{ vars.REALM_API_GROUP_ID }}
REALM_APP_ID: ${{ vars.GATSBY_REALM_APP_ID }}
REALM_API_PUBLIC_KEY: ${{ secrets.REALM_API_PUBLIC_KEY }}
REALM_API_PRIVATE_KEY: ${{ secrets.REALM_API_PRIVATE_KEY }}
ROLLBAR_POST_SERVER_ITEM_ACCESS_TOKEN: ${{ secrets.GATSBY_ROLLBAR_TOKEN }}

- name: Cache build
uses: actions/cache/save@v4
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ jobs:
CLOUDFLARE_R2_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_R2_ACCESS_KEY_ID }}
CLOUDFLARE_R2_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_R2_SECRET_ACCESS_KEY }}
REALM_GRAPHQL_API_KEY: ${{ secrets.REALM_GRAPHQL_API_KEY }}
REALM_APP_ID: ${{ secrets.REALM_APP_ID }}
REALM_API_APP_ID: ${{ vars.REALM_API_APP_ID }}
REALM_API_GROUP_ID: ${{ vars.REALM_API_GROUP_ID }}
REALM_API_PUBLIC_KEY: ${{ secrets.REALM_API_PUBLIC_KEY }}
REALM_API_PRIVATE_KEY: ${{ secrets.REALM_API_PRIVATE_KEY }}

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
env:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,8 @@ All three workflows share a common set of environment variables, which need to b
- `GATSBY_PRISMIC_REPO_NAME`
- `GATSBY_REALM_APP_ID`
- `NETLIFY_SITE_ID`
- `REALM_API_APP_ID`
- `REALM_API_GROUP_ID`

### Testing

Expand Down
15 changes: 15 additions & 0 deletions site/gatsby-site/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
overwrite: true,
schema: ["./server/schema.ts"],
require: ['ts-node/register'],
generates: {
"server/generated/graphql.ts": {
plugins: ["typescript", "typescript-resolvers", "typescript-mongodb"]
}
}
};

export default config;
8 changes: 4 additions & 4 deletions site/gatsby-site/cypress/e2e/unit/AlgoliaUpdater.cy.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ObjectID } = require('bson');
const { ObjectId } = require('bson');

const AlgoliaUpdater = require('../../../src/utils/AlgoliaUpdater');

Expand All @@ -12,7 +12,7 @@ const incidents = [

const reports = [
{
_id: new ObjectID('60dd465f80935bc89e6f9b01'),
_id: new ObjectId('60dd465f80935bc89e6f9b01'),
authors: ['Alistair Barr'],
date_downloaded: '2019-04-13',
date_modified: '2020-06-14',
Expand All @@ -36,7 +36,7 @@ const reports = [
url: 'https://url.com/stuff',
},
{
_id: new ObjectID('60dd465f80935bc89e6f9b02'),
_id: new ObjectId('60dd465f80935bc89e6f9b02'),
authors: ['Alistair Barr'],
date_downloaded: '2019-04-13',
date_modified: '2020-06-14',
Expand All @@ -62,7 +62,7 @@ const reports = [

// this report hast no parent incidents
{
_id: new ObjectID('60dd465f80935bc89e6f9b07'),
_id: new ObjectId('60dd465f80935bc89e6f9b07'),
authors: ['Test User'],
date_downloaded: '2019-04-13',
date_modified: '2020-06-14',
Expand Down
6 changes: 3 additions & 3 deletions site/gatsby-site/cypress/e2e/unit/Translator.cy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const { ObjectID } = require('bson');
const { ObjectId } = require('bson');

const Translator = require('../../../src/utils/Translator');

const reports = [
{
_id: new ObjectID('60dd465f80935bc89e6f9b01'),
_id: new ObjectId('60dd465f80935bc89e6f9b01'),
authors: ['Alistair Barr'],
date_downloaded: '2019-04-13',
date_modified: '2020-06-14',
Expand All @@ -27,7 +27,7 @@ const reports = [
url: 'https://url.com/stuff',
},
{
_id: new ObjectID('60dd465f80935bc89e6f9b02'),
_id: new ObjectId('60dd465f80935bc89e6f9b02'),
authors: ['Alistair Barr'],
date_downloaded: '2019-04-13',
date_modified: '2020-06-14',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ObjectID } = require('bson');
const { ObjectId } = require('bson');

const {
up,
Expand All @@ -16,8 +16,8 @@ describe('Migration Script - Remove Duplicated Subscriptions', () => {
incident_id: 600,
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a3'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a4'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a3'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a4'),
],
},
{
Expand All @@ -27,9 +27,9 @@ describe('Migration Script - Remove Duplicated Subscriptions', () => {
incident_id: 600,
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a0'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a1'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a2'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a0'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a1'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a2'),
],
},
],
Expand All @@ -41,8 +41,8 @@ describe('Migration Script - Remove Duplicated Subscriptions', () => {
entity_id: 'trans-women',
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a5'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a6'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a5'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a6'),
],
},
],
Expand All @@ -53,8 +53,8 @@ describe('Migration Script - Remove Duplicated Subscriptions', () => {
userId: '642188372947d07020c1319d',
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a7'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a8'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a7'),
new ObjectId('5f9f6b9b5f9c4c0001a3b3a8'),
],
},
],
Expand Down
15 changes: 15 additions & 0 deletions site/gatsby-site/cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": true,
// be explicit about types included
// to avoid clashing with Jest types
"types": [
"cypress"
]
},
"include": [
"../node_modules/cypress",
"./**/*.ts"
]
}
11 changes: 11 additions & 0 deletions site/gatsby-site/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MongoMemoryServer } from 'mongodb-memory-server';

export = async function globalSetup() {

const instance = await MongoMemoryServer.create();
const uri = instance.getUri();

(global as any).__MONGOINSTANCE = instance;

process.env.MONGODB_CONNECTION_STRING = uri.slice(0, uri.lastIndexOf('/'));
};
6 changes: 6 additions & 0 deletions site/gatsby-site/globalTeardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MongoMemoryServer } from 'mongodb-memory-server';

export = async function globalTeardown() {
const instance: MongoMemoryServer = (global as any).__MONGOINSTANCE;
await instance.stop();
};
20 changes: 20 additions & 0 deletions site/gatsby-site/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/

import type { Config } from 'jest';

const config: Config = {
preset: "ts-jest",
clearMocks: true,
collectCoverage: true,
coverageDirectory: "coverage",
coverageProvider: "v8",
testEnvironment: "node",
globalSetup: "./globalSetup.ts",
globalTeardown: "./globalTeardown.ts",
verbose: true,
};

export default config;
Loading

0 comments on commit 03afdc3

Please sign in to comment.