Skip to content

Commit

Permalink
[#617] feat: integrate frontend with metadata validation service
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Apr 3, 2024
1 parent 40c343b commit b12a3b6
Show file tree
Hide file tree
Showing 22 changed files with 166 additions and 177 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/lighthouse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ jobs:
- name: Run build and lighthouse task
working-directory: ./govtool/frontend
run: |
npm install
VITE_BASE_URL=https://staging.govtool.byron.network/api npm run build
lhci collect
npm install
VITE_BASE_URL=https://staging.govtool.byron.network/ npm run build
lhci collect
- name: Evaluate reports
if: github.repository_owner != 'IntersectMBO'
run: |
lhci assert --preset "lighthouse:recommended"
lhci assert --preset "lighthouse:recommended"
- name: Publish reports
working-directory: ./govtool/frontend
if: github.repository_owner == 'IntersectMBO'
run: |
lhci assert --preset lighthouse:recommended || echo "LightHouse Assertion error ignored ..."
lhci upload --githubAppToken="${{ secrets.LHCI_GITHUB_APP_TOKEN }}" --token="${{ secrets.LHCI_SERVER_TOKEN }}" --serverBaseUrl=https://lighthouse.cardanoapi.io --ignoreDuplicateBuildFailure
curl -X POST https://ligththouse.cardanoapi.io/api/metrics/build-reports \
-d "@./lighthouseci/$(ls ./.lighthouseci |grep 'lhr.*\.json' | head -n 1)" \
-H "commit-hash: $(git rev-parse HEAD)" \
-H "secret-token: ${{ secrets.METRICS_SERVER_SECRET_TOKEN }}" \
-H 'Content-Type: application/json' || echo "Metric Upload error ignored ..."
lhci assert --preset lighthouse:recommended || echo "LightHouse Assertion error ignored ..."
lhci upload --githubAppToken="${{ secrets.LHCI_GITHUB_APP_TOKEN }}" --token="${{ secrets.LHCI_SERVER_TOKEN }}" --serverBaseUrl=https://lighthouse.cardanoapi.io --ignoreDuplicateBuildFailure
curl -X POST https://ligththouse.cardanoapi.io/api/metrics/build-reports \
-d "@./lighthouseci/$(ls ./.lighthouseci |grep 'lhr.*\.json' | head -n 1)" \
-H "commit-hash: $(git rev-parse HEAD)" \
-H "secret-token: ${{ secrets.METRICS_SERVER_SECRET_TOKEN }}" \
-H 'Content-Type: application/json' || echo "Metric Upload error ignored ..."
44 changes: 22 additions & 22 deletions .github/workflows/test_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ name: Backend Test
on:
push:
paths:
- .github/workflows/test_backend.yml
- .github/workflows/test_backend.yml
# - govtool/backend
# - tests/govtool-backend

schedule:
- cron: '0 0 * * *'
- cron: "0 0 * * *"
workflow_dispatch:
inputs:
deployment:
Expand All @@ -24,26 +24,26 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11.4
cache: 'pip'
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11.4
cache: "pip"

- name: Run Backend Test
working-directory: tests/govtool-backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pytest -v --github-report
env:
BASE_URL: https://${{inputs.deployment || 'staging.govtool.byron.network/api' }}
METRICS_URL: https://metrics.cardanoapi.io
METRICS_API_SECRET: "${{ secrets.METRICS_SERVER_SECRET_TOKEN }}"
- name: Run Backend Test
working-directory: tests/govtool-backend
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pytest -v --github-report
env:
BASE_URL: https://${{inputs.deployment || 'staging.govtool.byron.network/' }}
METRICS_URL: https://metrics.cardanoapi.io
METRICS_API_SECRET: "${{ secrets.METRICS_SERVER_SECRET_TOKEN }}"

# - uses: schemathesis/action@v1
# with:
# schema: "http://localhost:8080/swagger.json"
# - uses: schemathesis/action@v1
# with:
# schema: "http://localhost:8080/swagger.json"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ changes.
- Fix endless spinner on a dashboard [Issue 539](https://github.com/IntersectMBO/govtool/issues/539)
- Update frontend package readme to reflect recent changes [Issue 543](https://github.com/IntersectMBO/govtool/issues/543)
- Change input selection strategy to 3 (random) [Issue 575](https://github.com/IntersectMBO/govtool/issues/575)
- Integrate frontend with metadata validation service [Issue 617](https://github.com/IntersectMBO/govtool/issues/617)

### Added

Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build-frontend: docker-login
if [[ "$(cardano_network)" = "mainnet" ]]; then NETWORK_FLAG=1; else NETWORK_FLAG=0; fi; \
$(call check_image_on_ecr,frontend,$(frontend_image_tag)) || \
$(docker) build --tag "$(repo_url)/frontend:$(frontend_image_tag)" \
--build-arg VITE_BASE_URL="https://$(domain)/api" \
--build-arg VITE_BASE_URL="https://$(domain)" \
--build-arg VITE_GTM_ID="$${GTM_ID}" \
--build-arg VITE_NETWORK_FLAG="$$NETWORK_FLAG" \
--build-arg VITE_SENTRY_DSN="$${SENTRY_DSN}" \
Expand Down
10 changes: 5 additions & 5 deletions govtool/frontend/src/consts/externalDataModalConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ModalState } from "@/context";
import I18n from "@/i18n";
import { MetadataValidationStatus } from "@/models";

export enum MetadataHashValidationErrors {
INVALID_URL = "Invalid URL",
Expand Down Expand Up @@ -29,13 +30,12 @@ const urlCannotBeFound = {
};

export const storageInformationErrorModals: Record<
MetadataHashValidationErrors,
MetadataValidationStatus,
ModalState<
typeof externalDataDoesntMatchModal | typeof urlCannotBeFound
>["state"]
> = {
[MetadataHashValidationErrors.INVALID_URL]: urlCannotBeFound,
[MetadataHashValidationErrors.FETCH_ERROR]: urlCannotBeFound,
[MetadataHashValidationErrors.INVALID_JSON]: externalDataDoesntMatchModal,
[MetadataHashValidationErrors.INVALID_HASH]: externalDataDoesntMatchModal,
[MetadataValidationStatus.URL_NOT_FOUND]: urlCannotBeFound,
[MetadataValidationStatus.INVALID_JSONLD]: externalDataDoesntMatchModal,
[MetadataValidationStatus.INVALID_HASH]: externalDataDoesntMatchModal,
};
4 changes: 4 additions & 0 deletions govtool/frontend/src/consts/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export const QUERY_KEYS = {
useGetDRepInfoKey: "useGetDRepInfoKey",
useGetVoteContextFromFile: "useGetVoteContextFromFile",
};

export const MUTATION_KEYS = {
postValidateKey: "postValidateKey",
};
32 changes: 18 additions & 14 deletions govtool/frontend/src/hooks/forms/useCreateGovernanceActionForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ import {
CIP_100,
CIP_108,
GOVERNANCE_ACTION_CONTEXT,
MetadataHashValidationErrors,
PATHS,
storageInformationErrorModals,
} from "@consts";
import { useCardano, useModal } from "@context";
import {
canonizeJSON,
downloadJson,
generateJsonld,
validateMetadataHash,
} from "@utils";
import { canonizeJSON, downloadJson, generateJsonld } from "@utils";
import { MetadataValidationStatus } from "@models";
import {
GovernanceActionFieldSchemas,
GovernanceActionType,
} from "@/types/governanceAction";

import { useValidateMutation } from "../mutations";

export type CreateGovernanceActionValues = {
links?: { link: string }[];
storeData?: boolean;
Expand All @@ -48,6 +45,7 @@ export const useCreateGovernanceActionForm = (
buildTreasuryGovernanceAction,
buildSignSubmitConwayCertTx,
} = useCardano();
const { validateMetadata } = useValidateMutation();
const { t } = useTranslation();
const [isLoading, setIsLoading] = useState<boolean>(false);
const [hash, setHash] = useState<string | null>(null);
Expand Down Expand Up @@ -120,22 +118,27 @@ export const useCreateGovernanceActionForm = (
}, [govActionType, json]);

const validateHash = useCallback(
async (storingUrl: string, localHash: string | null) => {
async (url: string, localHash: string | null) => {
try {
if (!localHash) {
throw new Error(MetadataHashValidationErrors.INVALID_HASH);
throw new Error(MetadataValidationStatus.INVALID_HASH);
}
await validateMetadataHash(storingUrl, localHash);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
const result = await validateMetadata({ url, hash: localHash });

if (result.status) {
throw result.status;
}
} catch (error) {
if (
Object.values(MetadataHashValidationErrors).includes(error.message)
Object.values(MetadataValidationStatus).includes(
error as MetadataValidationStatus,
)
) {
openModal({
type: "statusModal",
state: {
...storageInformationErrorModals[
error.message as MetadataHashValidationErrors
error as MetadataValidationStatus
],
onSubmit: backToForm,
onCancel: backToDashboard,
Expand Down Expand Up @@ -214,6 +217,7 @@ export const useCreateGovernanceActionForm = (
setIsLoading(true);

await validateHash(data.storingURL, hash);

const govActionBuilder = await buildTransaction(data);
await buildSignSubmitConwayCertTx({
govActionBuilder,
Expand Down
25 changes: 13 additions & 12 deletions govtool/frontend/src/hooks/forms/useEditDRepInfoForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import {
storageInformationErrorModals,
} from "@consts";
import { useCardano, useModal } from "@context";
import {
canonizeJSON,
downloadJson,
generateJsonld,
validateMetadataHash,
} from "@utils";
import { canonizeJSON, downloadJson, generateJsonld } from "@utils";
import { MetadataValidationStatus } from "@models";

import { useValidateMutation } from "../mutations";

export type EditDRepInfoValues = {
bio?: string;
Expand All @@ -43,6 +41,7 @@ export const defaultEditDRepInfoValues: EditDRepInfoValues = {
export const useEditDRepInfoForm = (
setStep?: Dispatch<SetStateAction<number>>,
) => {
const { validateMetadata } = useValidateMutation();
const { t } = useTranslation();
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -113,21 +112,23 @@ export const useEditDRepInfoForm = (
};

const validateHash = useCallback(
async (storingUrl: string) => {
async (url: string) => {
try {
if (!hash) throw new Error(MetadataHashValidationErrors.INVALID_HASH);

await validateMetadataHash(storingUrl, hash);
const result = await validateMetadata({ url, hash });

if (result.status) {
throw result.status;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (
Object.values(MetadataHashValidationErrors).includes(error.message)
) {
if (Object.values(MetadataValidationStatus).includes(error)) {
openModal({
type: "statusModal",
state: {
...storageInformationErrorModals[
error.message as MetadataHashValidationErrors
error as MetadataValidationStatus
],
onSubmit: backToForm,
onCancel: backToDashboard,
Expand Down
28 changes: 16 additions & 12 deletions govtool/frontend/src/hooks/forms/useRegisterAsdRepForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ import {
CIP_100,
CIP_QQQ,
DREP_CONTEXT,
MetadataHashValidationErrors,
PATHS,
storageInformationErrorModals,
} from "@consts";
import { useCardano, useModal } from "@context";
import {
canonizeJSON,
downloadJson,
generateJsonld,
validateMetadataHash,
} from "@utils";
import { MetadataValidationStatus } from "@models";
import { canonizeJSON, downloadJson, generateJsonld } from "@utils";

import { useGetVoterInfo } from "..";
import { useValidateMutation } from "../mutations";

export type RegisterAsDRepValues = {
bio?: string;
Expand All @@ -44,6 +41,7 @@ export const defaultRegisterAsDRepValues: RegisterAsDRepValues = {
export const useRegisterAsdRepForm = (
setStep?: Dispatch<SetStateAction<number>>,
) => {
const { validateMetadata } = useValidateMutation();
const { t } = useTranslation();
const navigate = useNavigate();
const [isLoading, setIsLoading] = useState<boolean>(false);
Expand Down Expand Up @@ -116,21 +114,27 @@ export const useRegisterAsdRepForm = (
};

const validateHash = useCallback(
async (storingUrl: string) => {
async (url: string) => {
try {
if (!hash) throw new Error(MetadataHashValidationErrors.INVALID_HASH);
if (!hash) throw new Error(MetadataValidationStatus.INVALID_HASH);

await validateMetadataHash(storingUrl, hash);
const result = await validateMetadata({ url, hash });

if (result.status) {
throw result.status;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
if (
Object.values(MetadataHashValidationErrors).includes(error.message)
Object.values(MetadataValidationStatus).includes(
error as MetadataValidationStatus,
)
) {
openModal({
type: "statusModal",
state: {
...storageInformationErrorModals[
error.message as MetadataHashValidationErrors
error as MetadataValidationStatus
],
onSubmit: backToForm,
onCancel: backToDashboard,
Expand Down
Loading

0 comments on commit b12a3b6

Please sign in to comment.