Skip to content

Commit

Permalink
refactor(preview-service): conforms to design patterns, adds tests, a…
Browse files Browse the repository at this point in the history
…nd is typed (#2429)

Co-authored-by: Kristaps Fabians Geikins <[email protected]>
  • Loading branch information
iainsproat and fabis94 authored Sep 4, 2024
1 parent f8878b4 commit 9a28e46
Show file tree
Hide file tree
Showing 83 changed files with 2,774 additions and 956 deletions.
71 changes: 67 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ workflows:
- test-objectsender:
filters: *filters-allow-all

- test-preview-service:
filters: *filters-allow-all

- test-ui-components:
filters: *filters-allow-all

Expand Down Expand Up @@ -176,6 +179,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-frontend:
context: *docker-hub-context
Expand All @@ -190,6 +194,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-frontend-2:
context: *docker-hub-context
Expand All @@ -204,6 +209,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-webhooks:
context: *docker-hub-context
Expand All @@ -218,6 +224,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-file-imports:
context: *docker-hub-context
Expand All @@ -232,6 +239,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-previews:
context: *docker-hub-context
Expand All @@ -246,6 +254,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-test-container:
context: *docker-hub-context
Expand All @@ -260,6 +269,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-monitor-container:
context: *docker-hub-context
Expand All @@ -274,6 +284,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- docker-publish-docker-compose-ingress:
context: *docker-hub-context
Expand All @@ -288,6 +299,7 @@ workflows:
- test-objectsender
- test-server
- test-server-no-ff
- test-preview-service

- publish-helm-chart:
filters: &filters-publish
Expand Down Expand Up @@ -331,6 +343,7 @@ workflows:
- test-frontend-2
- test-viewer
- test-objectsender
- test-preview-service

- publish-viewer-sandbox-cloudflare-pages:
filters: *filters-publish
Expand Down Expand Up @@ -579,8 +592,7 @@ jobs:
working_directory: 'packages/frontend-2'

test-viewer:
docker: &docker-node-browsers-image
- image: cimg/node:18.19.0-browsers
docker: *docker-node-browsers-image
resource_class: large
steps:
- checkout
Expand Down Expand Up @@ -617,9 +629,60 @@ jobs:
command: yarn test
working_directory: 'packages/viewer'

test-preview-service:
docker:
- image: cimg/node:18.19.0
- image: cimg/postgres:14.11
environment:
POSTGRES_DB: preview_service_test
POSTGRES_PASSWORD: preview_service_test
POSTGRES_USER: preview_service_test
resource_class: large
environment: {}
steps:
- checkout
- restore_cache:
name: Restore Yarn Package Cache
keys:
- yarn-packages-server-{{ checksum "yarn.lock" }}
- run:
name: Install Dependencies
command: yarn

- run:
name: Install Dependencies v2 (.node files missing bug)
command: yarn

- save_cache:
name: Save Yarn Package Cache
key: yarn-packages-server-{{ checksum "yarn.lock" }}
paths:
- .yarn/cache
- .yarn/unplugged

- run:
name: Build public packages
command: yarn build:public

- run:
name: Lint everything
command: yarn lint:ci
working_directory: 'packages/preview-service'

- run:
name: Copy .env.example to .env
command: |
#!/usr/bin/env bash
cp packages/preview-service/.env.example packages/preview-service/.env
sed -i~ '/^PG_CONNECTION_STRING=/s/=.*/="postgres:\/\/preview_service_test:[email protected]:5432\/preview_service_test"/' packages/preview-service/.env
- run:
name: Run tests
command: yarn test
working_directory: 'packages/preview-service'

test-objectsender:
docker: &docker-node-browsers-image
- image: cimg/node:18.19.0-browsers
docker: *docker-node-browsers-image
resource_class: large
steps:
- checkout
Expand Down
17 changes: 17 additions & 0 deletions .gitguardian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,21 @@ secret:
name: local test license
- match: 7a4ab6f7bfbcc0a37aa3a0fb00fd5b6edd1d524f393a6054e242eb28f5c06be5
name: 'packages/server/modules/core/tests/graph.spec.js - test secret'
- match: be603148062b367f828a58bdd695149d24f55f7c7f2e2c0bc31abd147cd07e86
name: packages/server/modules/webhooks/tests/cleanup.spec.ts - test password
- match: d1c44da2d7d52afaf219ff9789df7c04a79be80977336d7c87652db736b07538
name: packages/server/.env-example - test password for keycloak
- match: 05b116fa36d25a831d96d5b4ecd45b962ebf9345dcf81ac0950c4adb49e10183
name: packages/server/modules/serverinvites/tests/invites.spec.ts - test password
- match: 22ef4aa9beab564872bb1f15ff7592894ad445a68d6b03364f890cc5c3866b5d
name: packages/server/modules/core/tests/users.spec.js - test password
- match: 05b116fa36d25a831d96d5b4ecd45b962ebf9345dcf81ac0950c4adb49e10183
name: packages/server/modules/core/tests/users.spec.js - test password
- match: d1c44da2d7d52afaf219ff9789df7c04a79be80977336d7c87652db736b07538
name: setup/keycloak/speckle-realm.json - secret for dev keycloak
- match: b92d3b9844a823512dd1831c1eea5d9810c154027e07a36f007232fc26e9f70c
name: setup/keycloak/speckle-realm.json - secret for dev keycloak
- match: 2e1b3675a4049cd39fe6db081735f747730969071528270800f00fa98720d198
name: setup/keycloak/speckle-realm.json - algorithm name

version: 2
94 changes: 94 additions & 0 deletions .github/workflows/preview-service-acceptance.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Preview service acceptance test

on:
workflow_dispatch:
pull_request: # Pushing a new commit to the HEAD ref of a pull request will trigger the “synchronize” event
paths:
- .yarnrc.yml .
- .yarn
- package.json
- packages/frontend-2/type-augmentations/stubs
- packages/preview-service
- packages/viewer
- packages/objectloader
- packages/shared

jobs:
preview-service-acceptance:
name: Preview Service Acceptance test
runs-on: ubuntu-latest
services:
postgres:
# Docker Hub image
image: postgres:14
env:
POSTGRES_DB: preview_service_test
POSTGRES_PASSWORD: preview_service_test
POSTGRES_USER: preview_service_test
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

permissions:
contents: write # to update the screenshot saved in the branch. This is a HACK as GitHub API does not yet support uploading attachments to a comment.
pull-requests: write # to write a comment on the PR

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 9
run_install: false
- name: Install Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'yarn'
- name: Install dependencies
working-directory: utils/preview-service-acceptance
run: yarn install

#TODO load the docker image from a previous job
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and load preview-service Docker image
uses: docker/build-push-action@v6
with:
context: .
file: ./packages/preview-service/Dockerfile
load: true
push: false
tags: speckle/preview-service:local
cache-from: type=gha
cache-to: type=gha,mode=max

- name: Run the acceptance test
working-directory: packages/preview-service
run: yarn test:acceptance
env:
PREVIEW_SERVICE_IMAGE: speckle/preview-service:local
OUTPUT_FILE_PATH: /tmp/preview-service-output.png
NODE_ENV: test
PG_CONNECTION_STRING: postgres://preview_service_test:preview_service_test@localhost:5432/preview_service_test

- uses: actions/upload-artifact@v4
name: Upload the output from the preview-service
id: upload-preview-service-output
with:
name: preview-service-output
path: /tmp/preview-service-output.png
- uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '📸 Preview service has generated the following image:<br/><img src="${{ steps.upload-preview-service-output.outputs.artifact-url }}"/><br/>'
})
19 changes: 12 additions & 7 deletions packages/preview-service/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# NOTE: Docker context should be set to git root directory, to include the viewer
ARG NODE_ENV=production

FROM node:18-bookworm-slim@sha256:408f8cbbb7b33a5bb94bdb8862795a94d2b64c2d516856824fd86c4a5594a443 as build-stage
FROM node:18-bookworm-slim@sha256:408f8cbbb7b33a5bb94bdb8862795a94d2b64c2d516856824fd86c4a5594a443 AS build-stage

ARG NODE_ENV
ENV NODE_ENV=${NODE_ENV}
Expand Down Expand Up @@ -36,7 +36,9 @@ COPY packages/preview-service ./packages/preview-service/
# This way the foreach only builds the frontend and its deps
RUN yarn workspaces foreach -W run build

FROM node:18-bookworm-slim@sha256:408f8cbbb7b33a5bb94bdb8862795a94d2b64c2d516856824fd86c4a5594a443 as node
# google-chrome-stable is only available for amd64 so we have to fix the platform
# hadolint ignore=DL3029
FROM --platform=linux/amd64 node:18-bookworm-slim@sha256:408f8cbbb7b33a5bb94bdb8862795a94d2b64c2d516856824fd86c4a5594a443 AS node

SHELL ["/bin/bash", "-o", "pipefail", "-c"]
# hadolint ignore=DL3008,DL3015
Expand All @@ -47,13 +49,16 @@ RUN apt-get update && \
# wget has different versions for different architectures
wget \
gnupg=2.2.40-1.1 && \
# Clean up
apt-get clean && \
rm -rf /var/lib/apt/lists/* && \
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg && \
sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] https://dl-ssl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' && \
sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] https://dl-ssl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
# Install Chrome and fonts
apt-get update && \
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y \
--no-install-recommends \
google-chrome-stable \
google-chrome-stable=128.0.6613.119-1 \
fonts-ipafont-gothic=00303-23 \
fonts-wqy-zenhei=0.9.45-8 \
fonts-thai-tlwg=1:0.7.3-1 \
Expand All @@ -74,7 +79,7 @@ COPY .yarnrc.yml .
COPY .yarn ./.yarn
COPY package.json yarn.lock ./

# Onyl copy in the relevant package.json files for the dependencies
# Only copy in the relevant package.json files for the dependencies
COPY packages/frontend-2/type-augmentations/stubs ./packages/frontend-2/type-augmentations/stubs/
COPY packages/preview-service/package.json ./packages/preview-service/

Expand All @@ -99,4 +104,4 @@ RUN groupadd -g 30000 -r pptruser && \
# Run everything after as non-privileged user.
USER pptruser

ENTRYPOINT [ "tini", "--", "node", "bin/www" ]
ENTRYPOINT [ "tini", "--", "node", "--loader=./dist/src/aliasLoader.js", "bin/www" ]
19 changes: 0 additions & 19 deletions packages/preview-service/Makefile

This file was deleted.

43 changes: 0 additions & 43 deletions packages/preview-service/app.js

This file was deleted.

Loading

0 comments on commit 9a28e46

Please sign in to comment.