This commit converts the Images Table to Typescript and converts all API calls to image-builder to use RTK Query hooks. This should increase the performance of the app significantly. Previously our calls to the image-builder API were made in series. They are now made in parallel. We may want to investigate the possibility of hitting rate limiting now that we will be issuing requests in much more rapid succession. In the tests, moving to RTK Query hooks has allowed us to remove virtually all Jest mocking. However, this means that some of our previous tests which tested against implementation details were broken. Most notably, we no longer check the Redux store to verify that clones have been added correctly and we no longer check that compose requests were issued successfully. Test coverage will be restored in a follow-up PR where the dev-dependency @msw/data is added. Adding a persistent data layer to the tests using @msw/data will allow us to verify that our POST requests (creating composes and cloning them) are working by testing that the Images Table has been updated. |
||
|---|---|---|
| .github | ||
| .travis | ||
| api | ||
| config | ||
| deploy | ||
| devel | ||
| distribution | ||
| schutzbot | ||
| src | ||
| .eslintignore | ||
| .eslintrc-typescript.yml | ||
| .eslintrc.yml | ||
| .gitignore | ||
| .gitlab-ci.yml | ||
| .stylelintrc.json | ||
| .travis.yml | ||
| api.sh | ||
| babel.config.js | ||
| build_deploy.sh | ||
| codecov.yml | ||
| jest.setup.js | ||
| LICENSE | ||
| package-lock.json | ||
| package.json | ||
| pr_check.sh | ||
| README.md | ||
| tsconfig.json | ||
image-builder-frontend
Table of Contents
How to build and run image-builder-frontend
Frontend Development
To develop the frontend you can use a proxy to run image-builder-frontend locally against the chrome and backend at console.redhat.com.
Working against the production environment is preferred, as any work can be released without worrying if a feature from stage has been released yet.
Nodejs and npm version
Make sure you have npm@7 and node 15+ installed. If you need multiple versions of nodejs check out nvm.
Webpack proxy
-
run
npm ci -
run
npm run prod-beta. This command uses a prod-beta env by default. Configure your environment by theenvattribute indev.webpack.config.js. -
Secondly redirect a few
prod.foo.redhat.comto localhost, if this has not been done already.
echo "127.0.0.1 prod.foo.redhat.com" >> /etc/hosts
- open browser at
https://prod.foo.redhat.com:1337/beta/insights/image-builder
Webpack proxy (staging) -- Runs with image-builder's stage deployment
-
run
npm ci -
run
npm run stage-beta. This command uses a stage-beta env by default. Configure your environment by theenvattribute indev.webpack.config.js. -
Secondly redirect a few
stage.foo.redhat.comto localhost, if this has not been done already.
echo "127.0.0.1 stage.foo.redhat.com" >> /etc/hosts
- open browser at
https://stage.foo.redhat.com:1337/beta/insights/image-builder
Insights proxy (deprecated)
-
Clone the insights proxy: https://github.com/RedHatInsights/insights-proxy
-
Setting up the proxy
Choose a runner (podman or docker), and point the SPANDX_CONFIG variable to
profile/local-frontend.jsincluded in image-builder-frontend.sudo insights-proxy/scripts/patch-etc-hosts.sh export RUNNER="podman" export SPANDX_CONFIG=$PATH_TO/image-builder-frontend/profiles/local-frontend.js sudo -E insights-proxy/scripts/run.sh -
Starting up image-builder-frontend
In the image-builder-frontend checkout directory
npm install npm start
The UI should be running on https://prod.foo.redhat.com:1337/beta/insights/image-builder/landing. Note that this requires you to have access to either production or stage (plus VPN and proxy config) of insights.
API endpoints
API endpoints are programmatically generated with the RTKQ library. This sections overview the steps to add new APIs and endpoints.
Add a new API
For an hypothetical API called foobar
-
Download the foobar api openapi json or yaml representation under
api/schema/foobar.json -
Create a new "empty" api file under
src/store/emptyFoobarApi.tsthat has for content:
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { FOOBAR_API } from '../constants';
// initialize an empty api service that we'll inject endpoints into later as needed
export const emptyFoobarApi = createApi({
reducerPath: 'foobarApi',
baseQuery: fetchBaseQuery({ baseUrl: FOO_BAR }),
endpoints: () => ({}),
});
- Declare the new constat
FOOBAR_APIto the API url insrc/constants.js
export const FOOBAR_API = 'api/foobar/v1'
- Create the config file for code generation in
api/config/foobar.tscontaining:
import type { ConfigFile } from '@rtk-query/codegen-openapi';
const config: ConfigFile = {
schemaFile: '../schema/foobar.json',
apiFile: '../../src/store/emptyFoobarApi.ts',
apiImport: 'emptyEdgeApi',
outputFile: '../../src/store/foobarApi.ts',
exportName: 'foobarApi',
hooks: true,
filterEndpoints: ['getFoo', 'getBar', 'getFoobar'],
};
- Update the
api.shscript by adding a new line for npx to generate the code:
npx @rtk-query/codegen-openapi ./api/config/foobar.ts &
- Update the
.eslintignorefile by adding a new line for the generated code:
foobarApi.ts
- run api generation
rpn run api
And voilà!
Add a new endpoint
To add a new endpoint, simply update the api/config/foobar.ts file with new
endpoints in the filterEndpoints table.
Backend Development
To develop both the frontend and the backend you can again use the proxy to run both the frontend and backend locally against the chrome at cloud.redhat.com. For instructions see devel/README.md.
File Structure
Quick Reference
| Directory | Description |
|---|---|
/api |
API schema and config files |
/config |
webpack configuration |
/devel |
tools for local development |
/src |
source code |
/src/Components |
source code split by individual components |
/src/test |
test utilities |
/src/test/mocks |
mock handlers and server config for MSW |
/src/store |
Redux store |
/src/api.js |
API calls |
Style Guidelines
This project uses eslint's recommended styling guidelines. These rules can be found here: https://eslint.org/docs/rules/
To run the linter, use:
npm run lint
Any errors that can be fixed automatically, can be corrected by running:
npm run lint --fix
All the linting rules and configuration of eslint can be found in .eslintrc.yml.
Additional eslint rules
There are also additional rules added to enforce code style. Those being:
import/order-> enforces the order in import statements and separates them into groups based on their typeprefer-const-> enforces use ofconstdeclaration for variables that are never reassignedno-console-> throws an error for any calls ofconsolemethods leftover after debugging
Test Guidelines
This project is tested using the Jest framework, React Testing Library, and the Mock Service Worker library.
All UI contributions must also include a new test or update an existing test in order to maintain code coverage.
Running the tests
To run the unit tests, the linter, and the code coverage check run:
npm run test
These tests will also be run in our Travis CI when a PR is opened.
API endpoints
API slice definitions are generated using the @rtk-query/codegen-openapi package.
OpenAPI schema for the endpoints are stored in /api/schema. Their
corresponding configuration files are stored in /api/config. Each endpoint
has a corresponding empty API slice and generated API slice which are stored in
/src/store.
To generate or update API slice definitions, run:
npm run api