Skip to content

Commit

Permalink
[Tech] Migre le Frontend de CRA vers Vite (#1150)
Browse files Browse the repository at this point in the history
## Taches effectuées

- Migration des imports SVG en tant que composants pour les adapter à
Vite.
- Séparation des envs Frontend des autres via un fichier
`/frontend/.env.example`
(⚠️ **qu'il faut maintenant copier localement dans un
`/frontend/.env`**).
- Injection des variables d'environnement Frontend au runtime (et non au
buildtime) via [`import-meta-env`](https://import-meta-env.org/), comme
sur MonitorFish. [Voir l'ADR
correspondant](https://github.com/MTES-MCT/monitorfish/blob/master/adrs/0001-frontend-runtime-env-var-injection.md).
- Création d'un utilitaire `isPuppeteer()` pour différencier les
conditions que l'on applique pour les tests Puppeteer de celles qu'on
applique aux tests Cypress. Même principe qu'avec `isCypress()`.
- Traduction à la volée des instructions de contribution legacy et
déplacement vers le fichier `CONTRIBUTING.md`.
- Début d'écriture des instructions de contribution mises à jour dans ce
même fichier, en anglais, pour faciliter le partage éventuel avec
d'autres équipes de l'UE.
- Remplacement de la commande `npm run cypress:open` par `npm run
test:e2e:open`.
- Petite refacto qui déplace les constantes de
`frontend/src/features/missions/MissionForm/sse.ts` dans un fichier
séparé pour éviter un import cycle.

~~Je me suis aussi permis de désactiver un des tests des points
d'întérêt parce qu'on est à près de 10% de failure, il y a au moins 4
bugs notables connus sur cette feature et Adeline a prévu de la revoir
entièrement.~~

![image](https://github.com/MTES-MCT/monitorenv/assets/5957876/4ce84534-2351-4bfe-8b17-decb6ea11c29)

## Notes

- GitGuardian râle pour une clé de dév qui était déjà présente mais qui
a été déplacée dans le nouveau ficher `fontend/.env.example`.
- Il y aura un soucis avec monitor-ui concernant `cy.fill()` lorsqu'on
passera à une der versions Rsuite `>5.45.0` qui casse la structure DOM
des fields en assignant l'attribut `id` au wrapper (`div`) au lieu de le
passer à l'input. Cet attribut est ciblé par attributs `for` des labels.
Je ferai le fix.
- Les tests e2e des missions sont localement instables à cause de
`clickOutside()` manquants ou qui ne ferment pas correctement certains
dropdowns. À investiguer ?

## Review

- Ca serait idéal si vous pouviez tester que tout fonctionne bien
localement chez vous. Env est assez léger pour tourner sur Chrome en
mode dev chez moi contrairement à Fish.
- Je vous conseille de cloner cette branche sous un autre dossier car le
switch entre CRA et Vite n'est pas fun d'une branche à l'autre.

## ⚠️ Derniers points à voir ensemble

- Faire le tour des impications staging / prod concernant les variables
d'environnement @louptheron @thoomasbro.
- Il n'y a pas de `FRONTEND_MISSION_FORM_AUTO_SAVE_ENABLED` dans le
docker compose de prod, c'est normal @louptheron ? Rien de problématique
mais juste pour être sûr.

## Related Pull Requests & Issues

- Resolve #964
- #1014

----

- [ ] Tests E2E (Cypress)
  • Loading branch information
ivangabriele authored Feb 9, 2024
2 parents 68db9dc + 51b6649 commit 48a63e5
Show file tree
Hide file tree
Showing 61 changed files with 3,949 additions and 13,458 deletions.
38 changes: 13 additions & 25 deletions .github/workflows/cicd-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
steps:
- name: Get last release version
id: lastrelease
uses: pozetroninc/github-action-get-latest-release@master
uses: rez0n/actions-github-release@main
with:
repository: mtes-mct/monitorenv

Expand Down Expand Up @@ -83,8 +83,7 @@ jobs:
node-version: 20

- name: Install Node.js dependencies
# TODO Remove `--legacy-peer-deps` option as soon as CRA is migrate to Vite.
run: npm ci --legacy-peer-deps
run: npm ci
working-directory: ./frontend

- name: Lint Frontend
Expand Down Expand Up @@ -130,10 +129,9 @@ jobs:
SENTRY_ORG=${{ secrets.SENTRY_ORG }}
SENTRY_PROJECT=${{ secrets.SENTRY_PROJECT }}
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896

# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache-app
Expand Down Expand Up @@ -161,9 +159,11 @@ jobs:
CYPRESS_PROJECT_ID: ${{ secrets.CYPRESS_PROJECT_ID }}
CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
ENV_PROFILE: ${{needs.version.outputs.ENV_PROFILE}}
FRONTEND_GOOGLEMAPS_API_KEY: ${{ secrets.FRONTEND_GOOGLEMAPS_API_KEY }}
FRONTEND_SHOM_KEY: ${{ secrets.FRONTEND_SHOM_KEY }}
FRONTEND_MAPBOX_KEY: ${{ secrets.FRONTEND_MAPBOX_KEY }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MONITORENV_VERSION: ${{ needs.version.outputs.VERSION }}
REACT_APP_CYPRESS_PORT: 8880
VERSION: ${{needs.version.outputs.VERSION}}
steps:
- name: Checkout
Expand All @@ -187,29 +187,19 @@ jobs:
- name: Setup Firefox
uses: browser-actions/setup-firefox@latest
with:
firefox-version: 104.0.2

- name: Check versions
run: |
cat /etc/lsb-release
node -v
npm -v
docker -v
docker compose version
firefox -v
which firefox
firefox-version: 119.0.1

- name: Curl stubbed geoserver check
run: until $(curl --output /dev/null --silent --fail "http://localhost:8081/geoserver/wfs?service=WFS&version=1.1.0&request=GetFeature&typename=monitorenv:regulations&outputFormat=application/json&CQL_FILTER=topic=%27Ouest%20Cotentin%20Bivalves%27%20AND%20zone=%27Praires%20Ouest%20cotentin%27"); do printf '.'; sleep 5; done;

- uses: cypress-io/github-action@v6
with:
browser: firefox
cache-key: cypress-${{ hashFiles('package-lock.json') }}
config-file: config/cypress.config.ts
env: PORT=8880
install: true
# TODO Remove `--legacy-peer-deps` option as soon as CRA is migrate to Vite.
install-command: npm ci --legacy-peer-deps
install-command: npm ci
parallel: true
record: true
wait-on: "http://localhost:8880"
Expand All @@ -224,12 +214,11 @@ jobs:
ENV_PROFILE: ${{needs.version.outputs.ENV_PROFILE}}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MONITORENV_VERSION: ${{ needs.version.outputs.VERSION }}
REACT_APP_CYPRESS_PORT: 8880
VERSION: ${{needs.version.outputs.VERSION}}
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: browser-actions/setup-chrome@v1
with:
chrome-version: 120
Expand All @@ -250,8 +239,7 @@ jobs:
node-version: 20

- name: Install Node.js dependencies
# TODO Remove `--legacy-peer-deps` option as soon as CRA is migrate to Vite.
run: npm ci --legacy-peer-deps
run: npm ci
working-directory: ./frontend

- name: Curl stubbed geoserver check
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cicd-datapipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:

- name: Get last release version
id: lastrelease
uses: pozetroninc/github-action-get-latest-release@master
uses: rez0n/actions-github-release@main
with:
repository: mtes-mct/monitorenv

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/trivy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ name: Trivy
on:
workflow_dispatch:
version:
description: 'Version number'
description: "Version number"
required: true
default: 'v1.0.0'
default: "v1.0.0"

permissions:
contents: read
Expand All @@ -23,7 +23,7 @@ jobs:
steps:
- name: Get last release version
id: lastrelease
uses: pozetroninc/github-action-get-latest-release@master
uses: rez0n/actions-github-release@main
with:
repository: mtes-mct/monitorenv
- id: version
Expand Down Expand Up @@ -61,4 +61,4 @@ jobs:
- name: Upload Trivy scan results to GitHub Security tab
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: "trivy-results.sarif"
sarif_file: "trivy-results.sarif"
175 changes: 175 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Contributing

- [Getting Started: Backend \& Frontend](#getting-started-backend--frontend)
- [Requirements](#requirements)
- [First Setup](#first-setup)
- [Local Development](#local-development)
- [Getting Started: Data Pipeline](#getting-started-data-pipeline)
- [Requirements](#requirements-1)
- [First Setup](#first-setup-1)
- [Local Development](#local-development-1)
- [Technical Stack](#technical-stack)
- [Legacy Instructions](#legacy-instructions)
- [Technical Stack (Main Components)](#technical-stack-main-components)
- [Development Environment Setup](#development-environment-setup)
- [Prerequisites](#prerequisites)
- [Configuration](#configuration)
- [Sentry](#sentry)
- [Frontend](#frontend)
- [Linting](#linting)
- [Environment Variables](#environment-variables)
- [Backend](#backend)

---

## Getting Started: Backend & Frontend

### Requirements

- Debian-based Linux or macOS
- Docker v25 (with Docker Compose v2)
- Java Development Kit (JDK) 19
- Node.js v20 (with npm v10)

### First Setup

```sh
git clone https://github.com/MTES-MCT/monitorenv.git
cd monitorenv
cp infra/.env.example infra/.env
cp frontend/.env.example frontend/.env
cd frontend && npm i
```

### Local Development

In a first CLI tab, for the Backend (in project root):

```sh
make dev-run-back-with-infra
```

In a second CLI tab, for the Frontend:

```sh
cd frontend && npm run dev
```

## Getting Started: Data Pipeline

### Requirements

- Debian-based Linux or macOS
- Docker v25 (with Docker Compose v2)
- Python v3.10 (with Poetry)

### First Setup

__TODO__

### Local Development

__TODO__

## Technical Stack

- Databases
- PostgreSQL
- GeoServer
- Backend
- Language: Kotlin
- Framework: Spring
- ORM: Hibernate
- Testing: JUnit, Mockito
- Documentation: Swagger
- Frontend
- Language: TypeScript
- Framework: React (with Vite)
- UI: Rsuite (mostly customized via [monitor-ui](https://github.com/MTES-MCT/monitor-ui))
- Testing: Jest, Cypress, Puppeteer
- Data Pipeline
- Language: Python (with Poetry)
- Workflow: Prefect
- Testing: Pytest

---
---

## Legacy Instructions

> [!IMPORTANT]
> These instructions must be cleaned, updated and clarified. They are kept here in the meantime.
### Technical Stack (Main Components)

- Infrastructure:
- Docker
- Backend:
- Kotlin
- Spring
- PostgreSQL
- Geoserver
- Frontend:
- React (Create React App)
- OpenLayers
- Rsuite
- Data pipeline:
- Python 3.10
- Poetry
- Prefect

### Development Environment Setup

#### Prerequisites

- npm
- openjdk (osx: `brew install openjdk`)
- postgres (only `psql` is required. osx: `brew install libpq`)
- docker + docker-compose
- python 3.10 + poetry

#### Configuration

Create a `./infra/.env` file based on `./infra/.env.template`
Optionally modify `./infra/configurations/backend/application-dev.properties`

##### Sentry

SENTRY_DSN is an environment variable used by Sentry to identify the application. It is used by both the frontend and backend. It is defined in the `./infra/.env` file and injected into the frontend application via the `./public/env.js` file.

If `SENTRY_DSN` is set, the backend logs will automatically be sent.

#### Frontend

`make dev-install`: install frontend dependencies
`make dev-run-front`: starts the frontend development server
The browser will automatically open at the URL <http://localhost:3000>

##### Linting

Before pushing a commit, check the linting with `npm run test:lint`

##### Environment Variables

React CRA allows the introduction of environment variables at build time.
To allow the use of environment variables at the application's runtime, and to avoid compiling the frontend application for each environment, the variables are injected into the application via the static `public/env.js` file loaded by the client. This file is updated with the environment variables via an `env.sh` script.
In development, the environment variables are injected via CRA. The `src/env.js` file manages the import of variables for the entire application, regardless of the environment (development or production).

#### Backend

Check the configuration: `make dev-check-config`
Start the backend:
`make dev-run-back-with-infra`:

- optionally removes previous docker instances
- creates a docker instance of the database + geoserver
- launches the development backend server

The backend is then accessible at <http://localhost:8880> by default.
A Swagger interface is available at the URL: <http://localhost:8880/swagger-ui.html>

It may be necessary to load context data for frontend development and configure Geoserver to distribute this data.

To restart the backend without recreating the database and geoserver containers, run the command `make dev-run-back`


5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ endif
# Frontend
.PHONY: dev-install dev-run-front
dev-install:
# TODO Remove `--legacy-peer-deps` option as soon as CRA is migrate to Vite.
cd frontend && npm install --legacy-peer-deps
cd frontend && npm install

dev-run-front:
cd frontend && npm start
cd frontend && npm run dev

.PHONY: test-front dev-lint-frontend test-back

Expand Down
Loading

0 comments on commit 48a63e5

Please sign in to comment.