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

Shared module v2 #1233

Merged
merged 43 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d16785e
Add shared module v2
nygrenh Sep 6, 2023
d8487f1
Merge remote-tracking branch 'origin/master' into shared-module-v2
nygrenh Nov 1, 2023
3cb4cab
Fixes to the sync-tmc-users-cronjob
nygrenh Nov 1, 2023
60802e7
Finish new syncing
nygrenh Nov 2, 2023
9abadc6
Small improvements
nygrenh Nov 3, 2023
3a4a628
WIP
nygrenh Nov 3, 2023
a2fcdec
Merge remote-tracking branch 'origin/master' into shared-module-v2
nygrenh Jan 15, 2024
77789cf
Restart syncing automatically
nygrenh Jan 15, 2024
6bdb354
Add bin/shared-module-sync-watch
nygrenh Jan 15, 2024
06203df
Merge remote-tracking branch 'origin/master' into shared-module-v2
nygrenh Jan 22, 2024
435d9da
Move shared module contents
nygrenh Jan 22, 2024
2cf6e4c
WIP
nygrenh Jan 24, 2024
57e80df
Replace old shared module
nygrenh Jan 25, 2024
70daabb
Automatically clean up old shared module folders
nygrenh Jan 25, 2024
c56ca19
WIP
nygrenh Jan 25, 2024
2a7e2d1
Remove extra files
nygrenh Jan 25, 2024
b4dfd82
Gitignore
nygrenh Jan 25, 2024
b169ec7
Script fix
nygrenh Jan 25, 2024
9513327
Fixes
nygrenh Feb 5, 2024
f4b25e7
Merge remote-tracking branch 'origin/master' into shared-module-v2
nygrenh Feb 15, 2024
d1c1d68
WIP
nygrenh Mar 6, 2024
e6b5370
Merge remote-tracking branch 'origin/master' into shared-module-v2
nygrenh May 17, 2024
7aa4c22
Update to storybook 8
nygrenh May 17, 2024
c9f2ea7
Updates
nygrenh May 17, 2024
838a338
Fix
nygrenh May 17, 2024
960da45
Restore services folder
nygrenh May 17, 2024
bfe9a5f
Restore system tests
nygrenh May 17, 2024
c5b2dcf
Restore shared module common
nygrenh May 17, 2024
03e4cf0
Update imports for cms
nygrenh May 17, 2024
bfc9171
Update imports for course-material
nygrenh May 17, 2024
04cec2a
Update imports for example-exercise
nygrenh May 17, 2024
527e24d
Update imports for main-frontend
nygrenh May 17, 2024
c6b7727
Update imports for quizzes
nygrenh May 17, 2024
c96f55d
Fixes
nygrenh May 17, 2024
53145ac
Update workspace to include the storybook and sort alphabetically
nygrenh May 17, 2024
1da7ed4
Ci fixes
nygrenh May 17, 2024
112313a
More import updates
nygrenh May 17, 2024
164323c
Fixes
nygrenh May 17, 2024
d63c450
next.config.js import fixes
nygrenh May 21, 2024
d2a2b34
Re-add shared module tests
nygrenh May 21, 2024
24c7d87
Test fixes
nygrenh May 21, 2024
e81771a
Test stability fix
nygrenh May 21, 2024
796cd9a
Test stability fix
nygrenh May 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 7 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ module.exports = {
},
overrides: [
{
files: ["system-tests/**/*", "**.test.tsx", "**.test.ts"],
files: [
"system-tests/**/*",
"**.test.tsx",
"**.test.ts",
"storybook/**/*",
"shared-module/packages/create-exercise-service/**/*",
],
rules: {
"i18next/no-literal-string": "off",
},
Expand Down
20 changes: 12 additions & 8 deletions .github/workflows/storybook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,29 @@ jobs:
STORYBOOK_DEPLOY_KEY: ${{ secrets.STORYBOOK_DEPLOY_KEY }}
if: ${{ github.ref == 'refs/heads/master' }}
- name: Check out storybook repository
run: git clone --branch gh-pages [email protected]:rage/secret-project-storybook.git ./shared-module/storybook-static-repo
run: git clone --branch gh-pages [email protected]:rage/secret-project-storybook.git ./storybook/storybook-static-repo
env:
GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes"
if: ${{ github.ref == 'refs/heads/master' }}

- name: npm ci
- name: npm ci for the repo root
run: npm ci
working-directory: ./shared-module

- name: npm ci for the storybook
run: npm ci
working-directory: ./storybook


- name: npm run build-storybook
run: npm run build-storybook -- --quiet
working-directory: ./shared-module
working-directory: ./storybook

- name: Remove old files from repo
run: rm -rf ./shared-module/storybook-static-repo/*
run: rm -rf ./storybook/storybook-static-repo/*
if: ${{ github.ref == 'refs/heads/master' }}

- name: Copy new files to repo
run: cp -r ./shared-module/storybook-static/* ./shared-module/storybook-static-repo/
run: cp -r ./storybook/storybook-static/* ./storybook/storybook-static-repo/
if: ${{ github.ref == 'refs/heads/master' }}

- name: Commit files
Expand All @@ -58,12 +62,12 @@ jobs:
git config --local user.name "github-actions[bot]"
git add .
git commit -m "Update: $GITHUB_SHA" --allow-empty
working-directory: ./shared-module/storybook-static-repo
working-directory: ./storybook/storybook-static-repo
if: ${{ github.ref == 'refs/heads/master' }}

- name: Push changes
run: git push
env:
GIT_SSH_COMMAND: "ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes"
working-directory: ./shared-module/storybook-static-repo
working-directory: ./storybook/storybook-static-repo
if: ${{ github.ref == 'refs/heads/master' }}
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ jobs:
run: npm run test
working-directory: ./services/cms

shared-module:
shared-module-common:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: npm ci for the root of the repo
run: npm ci
- name: npm ci
run: npm ci
working-directory: ./shared-module
working-directory: ./shared-module/packages/common
- name: Run tests
run: npm run test
working-directory: ./shared-module
working-directory: ./shared-module/packages/common

quizzes:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion bin/generate-bindings
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ run_command cd "$RELATIVE_PATH" || exit
run_command env SQLX_OFFLINE=1 cargo test --features ts_rs --lib ts_binding_generator

# Fix formatting
run_command sed -i '1s;^;// ###########################################\n// ## This file is autogenerated by running ## \n// ## "bin/generate-bindings" in the root ##\n// ## folder of this repo ##\n// ## ##\n// ## Do not edit this file by hand. ##\n// ###########################################\n\n;' ../../shared-module/src/bindings.ts
run_command sed -i '1s;^;// ###########################################\n// ## This file is autogenerated by running ## \n// ## "bin/generate-bindings" in the root ##\n// ## folder of this repo ##\n// ## ##\n// ## Do not edit this file by hand. ##\n// ###########################################\n\n;' ../../shared-module/common/src/bindings.ts

run_command npm run eslint:format-bindings

Expand Down
28 changes: 28 additions & 0 deletions bin/run-cronjob
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
set -euo pipefail
source "$(dirname "$0")/.common"

echo "This command creates a new job from a cronjob. Give it a cronjob name to run the job it once."
echo -e "${BLUE}Tip: if you changed some code you'll need to restart bin/dev or bin/test to see your changes.${RESET_EVERYTHING}"

POD_NAME=$(kubectl get pods -l app=headless-lms -o name --sort-by=.metadata.creationTimestamp | tac | head -n 1)
[ -z "$POD_NAME" ] && { echo "Failed to find the headless-lms pod. Note that you'll need to have either bin/dev or bin/test running for this to work."; exit 1; }

# check if first argument is a cronjob name
if [ -z "${1:-}" ]; then
echo -e "${RED}Error: you need to give a cronjob name as the first argument.${RESET_EVERYTHING}"
echo ""
echo "List of available cronjobs:"
run_command kubectl get cronjobs
exit 1
fi

CRONJOB_NAME="$1"
JOB_NAME="$CRONJOB_NAME-$(date --iso-8601=ns | sha256sum | cut -d ' ' -f 1 | cut -c -10)"

run_command kubectl create job --from=cronjob/"$CRONJOB_NAME" "$JOB_NAME"

echo ""
echo -e "${GREEN}✅ The job $JOB_NAME has been created. You should see its output in the bin/dev or bin/test terminal.${RESET_EVERYTHING}"

echo -e "${GREEN}Alternatively, you should be able to see the logs with ${BOLD}kubectl logs -f job.batch/$JOB_NAME${RESET_EVERYTHING}"
14 changes: 14 additions & 0 deletions bin/shared-module-sync-watch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash
# Only meant to be ran against the database in minikube. You can start it with
# either `bin/dev` or `bin/dev-only-db`.
set -euo pipefail
source "$(dirname "$0")/.common"


BASEDIR="$(dirname "${BASH_SOURCE[0]}")"
FOLDER_PATH="$BASEDIR/../shared-module"
RELATIVE_PATH=$(realpath --relative-to="$(pwd)" "$FOLDER_PATH")

cd "$RELATIVE_PATH"

run_command npm run sync
9 changes: 5 additions & 4 deletions bin/translations-add-new-ones-to-non-en
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3

import os
import subprocess
import glob
import json
BASEDIR="$(dirname "${BASH_SOURCE[0]}")"
TRANSLATIONS_EN_PATH="$BASEDIR/../shared-module/common/src/locales/en"
TRANSLATIONS_EN_RELATIVE_PATH=$(realpath --relative-to="$(pwd)" "$TRANSLATIONS_EN_PATH")
FOLDERS_PATH="$BASEDIR/../shared-module/common/src/locales"
FOLDERS_RELATIVE_PATH=$(realpath --relative-to="$(pwd)" "$FOLDERS_PATH")

BASEDIR = os.path.dirname(os.path.realpath(__file__))
TRANSLATIONS_EN_PATH = os.path.join(BASEDIR, "../shared-module/src/locales/en")
Expand Down
2 changes: 1 addition & 1 deletion bin/translations-sort
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -euo pipefail
source "$(dirname "$0")/.common"

BASEDIR="$(dirname "${BASH_SOURCE[0]}")"
FOLDER_PATH="$BASEDIR/../shared-module/src/locales"
FOLDER_PATH="$BASEDIR/../shared-module/common/src/locales"
RELATIVE_PATH=$(realpath --relative-to="$(pwd)" "$FOLDER_PATH")

ensure_program_in_path jq
Expand Down
6 changes: 3 additions & 3 deletions docs/frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export const mainFrontendClient = axios.create({ baseURL: "/api/v0/main-frontend
`shared-module` contains types (in `bindings.ts`) and guards (in `bindings.guard`) generated from the backend types as well as other helper functions (in `utils`) which should be used when interacting with the backend. For example, `main-frontend` fetches `/api/v0/main-frontend/organizations` with

```ts
import { Organization } from "../../shared-module/bindings"
import { isOrganization } from "../../shared-module/bindings.guard"
import { isArray, validateResponse } from "../../shared-module/utils/fetching"
import { Organization } from "@/shared-module/common/bindings"
import { isOrganization } from "@/shared-module/common/bindings.guard"
import { isArray, validateResponse } from "@/shared-module/common/utils/fetching"

export const fetchOrganizations = async (): Promise<Array<Organization>> => {
// first, we get a response from the API using mainFrontendClient
Expand Down
60 changes: 60 additions & 0 deletions kubernetes/base/headless-lms/sync-tmc-users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: sync-tmc-users
labels:
app: sync-tmc-users
deploymentType: with-init-container-cronjob
needs-db: "job"
spec:
schedule: "13 3 * * *"
startingDeadlineSeconds: 900
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 1
successfulJobsHistoryLimit: 3
jobTemplate:
spec:
activeDeadlineSeconds: 10800
template:
spec:
restartPolicy: OnFailure
containers:
- name: sync-tmc-users
image: headless-lms
command:
[
"cargo",
"run",
"--",
"sync-tmc-users",
]
resources:
requests:
memory: 100Mi
cpu: 20m
limits:
memory: 300Mi
cpu: 200m
envFrom:
- secretRef:
name: headless-lms-secrets
initContainers:
- name: headless-lms-wait-for-db
image: headless-lms
command:
- bash
- "-c"
- |
echo Waiting for postgres to be available
timeout 120 ./wait-for-db.sh
./wait-for-db-migrations.sh
resources:
requests:
memory: 100Mi
cpu: 20m
limits:
memory: 300Mi
cpu: 200m
envFrom:
- secretRef:
name: headless-lms-secrets
1 change: 1 addition & 0 deletions kubernetes/base/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ resources:
- headless-lms/service-info-fetcher.yml
- headless-lms/regrader.yml
- headless-lms/peer-review-updater.yml
- headless-lms/sync-tmc-users.yml
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"eslint:ci": "eslint .",
"prepare": "husky install",
"lint:css": "stylelint '**/*.tsx'",
"postinstall": "ts-node --transpile-only -O \"{\\\"module\\\":\\\"commonjs\\\"}\" shared-module/setup-shared-module.ts"
"postinstall": "cd shared-module && npm ci && npm run sync-once"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions services/cms/next.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable i18next/no-literal-string */
/* eslint-disable @typescript-eslint/no-var-requires */
const generateNormalResponseHeaders =
require("./src/shared-module/utils/responseHeaders").generateNormalResponseHeaders
const svgoConfig = require("./src/shared-module/utils/svgoConfig")
require("./src/shared-module/common/utils/responseHeaders").generateNormalResponseHeaders
const svgoConfig = require("./src/shared-module/common/utils/svgoConfig")

const normalResponseHeaders = generateNormalResponseHeaders()

Expand Down
3 changes: 2 additions & 1 deletion services/cms/src/blocks/Author/AuthorEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { BlockEditProps, Template } from "@wordpress/blocks"
import React from "react"
import { useTranslation } from "react-i18next"

import { baseTheme, headingFont } from "../../shared-module/styles"
import BlockWrapper from "../BlockWrapper"

import { baseTheme, headingFont } from "@/shared-module/common/styles"

const ALLOWED_NESTED_BLOCKS = ["moocfi/author-inner-block"]
const AUTHOR_BLOCK_TEMPLATE: Template[] = [["moocfi/author-inner-block"]]
const AuthorEditor: React.FC<React.PropsWithChildren<BlockEditProps<Record<string, never>>>> = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { useTranslation } from "react-i18next"
import PageContext from "../../contexts/PageContext"
import { fetchCourseInstances } from "../../services/backend/course-instances"
import { fetchCourseModulesByCourseId } from "../../services/backend/courses"
import CheckBox from "../../shared-module/components/InputFields/CheckBox"
import { assertNotNullOrUndefined } from "../../shared-module/utils/nullability"
import BlockPlaceholderWrapper from "../BlockPlaceholderWrapper"

import { ConditionAttributes } from "."

import CheckBox from "@/shared-module/common/components/InputFields/CheckBox"
import { assertNotNullOrUndefined } from "@/shared-module/common/utils/nullability"

const ALLOWED_NESTED_BLOCKS = [
"core/heading",
"core/buttons",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { InnerBlocks, RichText } from "@wordpress/block-editor"
import { BlockEditProps, Template } from "@wordpress/blocks"
import React from "react"

import BreakFromCentered from "../../shared-module/components/Centering/BreakFromCentered"
import { baseTheme } from "../../shared-module/styles"
import {
CMS_EDITOR_SIDEBAR_THRESHOLD,
CMS_EDITOR_SIDEBAR_WIDTH,
} from "../../shared-module/utils/constants"
import BlockWrapper from "../BlockWrapper"

import { CourseObjectiveSectionAttributes } from "."

import BreakFromCentered from "@/shared-module/common/components/Centering/BreakFromCentered"
import { baseTheme } from "@/shared-module/common/styles"
import {
CMS_EDITOR_SIDEBAR_THRESHOLD,
CMS_EDITOR_SIDEBAR_WIDTH,
} from "@/shared-module/common/utils/constants"

const ALLOWED_NESTED_BLOCKS = ["core/heading", "core/buttons", "core/button", "core/paragraph"]
const COURSE_OBJECTIVE_SECTION_TEMPLATE: Template[] = [
[
Expand Down
9 changes: 5 additions & 4 deletions services/cms/src/blocks/Exercise/ExerciseEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { useTranslation } from "react-i18next"

import { EditorContentDispatch } from "../../contexts/EditorContentContext"
import ExerciseBlockContext from "../../contexts/ExerciseBlockContext"
import Button from "../../shared-module/components/Button"
import BreakFromCentered from "../../shared-module/components/Centering/BreakFromCentered"
import Centered from "../../shared-module/components/Centering/Centered"
import { baseTheme, primaryFont, typography } from "../../shared-module/styles"
import breakFromCenteredProps from "../../utils/breakfromCenteredProps"

import { ExerciseAttributes } from "."

import Button from "@/shared-module/common/components/Button"
import BreakFromCentered from "@/shared-module/common/components/Centering/BreakFromCentered"
import Centered from "@/shared-module/common/components/Centering/Centered"
import { baseTheme, primaryFont, typography } from "@/shared-module/common/styles"

const ALLOWED_NESTED_BLOCKS = ["moocfi/exercise-slide"]

const ExerciseEditorCard = styled.div`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { useTranslation } from "react-i18next"
import PeerReviewEditor from "../../../components/PeerReviewEditor"
import ExerciseBlockContext from "../../../contexts/ExerciseBlockContext"
import PageContext from "../../../contexts/PageContext"
import Accordion from "../../../shared-module/components/Accordion"
import CheckBox from "../../../shared-module/components/InputFields/CheckBox"
import TextField from "../../../shared-module/components/InputFields/TextField"
import { baseTheme } from "../../../shared-module/styles"
import { respondToOrLarger } from "../../../shared-module/styles/respond"

import Accordion from "@/shared-module/common/components/Accordion"
import CheckBox from "@/shared-module/common/components/InputFields/CheckBox"
import TextField from "@/shared-module/common/components/InputFields/TextField"
import { baseTheme } from "@/shared-module/common/styles"
import { respondToOrLarger } from "@/shared-module/common/styles/respond"

const ALLOWED_NESTED_BLOCKS = ["core/image", "core/paragraph", "core/list", "moocfi/latex"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import React, { useContext } from "react"
import { useTranslation } from "react-i18next"

import { EditorContentDispatch } from "../../../contexts/EditorContentContext"
import Button from "../../../shared-module/components/Button"
import { primaryFont, typography } from "../../../shared-module/styles"
import { gutenbergControlsHidden } from "../../../styles/EditorStyles"

import Button from "@/shared-module/common/components/Button"
import { primaryFont, typography } from "@/shared-module/common/styles"

const ALLOWED_NESTED_BLOCKS = ["moocfi/exercise-task"]
const INNER_BLOCKS_TEMPLATE: TemplateArray = [["moocfi/exercise-task", {}]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { css } from "@emotion/css"
import { useTranslation } from "react-i18next"

import useAllExerciseServices from "../../../../hooks/useAllExerciseServices"
import { ExerciseServiceIframeRenderingInfo } from "../../../../shared-module/bindings"
import Button from "../../../../shared-module/components/Button"
import ErrorBanner from "../../../../shared-module/components/ErrorBanner"
import Spinner from "../../../../shared-module/components/Spinner"

import { ExerciseServiceIframeRenderingInfo } from "@/shared-module/common/bindings"
import Button from "@/shared-module/common/components/Button"
import ErrorBanner from "@/shared-module/common/components/ErrorBanner"
import Spinner from "@/shared-module/common/components/Spinner"

interface Props {
onChooseItem: (task: ExerciseServiceIframeRenderingInfo) => void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ExerciseServiceIframeRenderingInfo } from "../../../../shared-module/bindings"

import ExerciseServiceList from "./ExerciseServiceList"

import { ExerciseServiceIframeRenderingInfo } from "@/shared-module/common/bindings"

interface Props {
onChooseItem: (task: ExerciseServiceIframeRenderingInfo) => void
}
Expand Down
Loading
Loading