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

Feat: cms plugin #2393

Merged
merged 4 commits into from
Jan 31, 2024
Merged

Feat: cms plugin #2393

merged 4 commits into from
Jan 31, 2024

Conversation

nmrgt
Copy link
Collaborator

@nmrgt nmrgt commented Jan 30, 2024

  • ajout d'un plugin pour avoir deux boutons
    • deploy via github action
    • flush du cache

Summary by CodeRabbit

  • New Features

    • Introduced a new betagouv plugin with UI components for managing and displaying information about specific flows.
    • Added internationalization support for the plugin's UI.
    • Implemented a navigation bar and main layout for the plugin's application pages.
    • Configured API endpoints for fetching and managing flows.
  • Documentation

    • Provided module declarations for integrating with the Strapi design system and other utilities.
  • Chores

    • Updated Dockerfile configurations to improve build commands with the --workspaces flag.

Copy link
Contributor

coderabbitai bot commented Jan 30, 2024

Walkthrough

A new betagouv plugin has been introduced to a CMS, encompassing both frontend and backend enhancements. This plugin adds functionality to manage and display information about various flows within the admin UI, leveraging React components and custom hooks for fetching and managing data. Additionally, server-side logic for handling flow operations, including running and listing flows, has been implemented. The Docker build process was also refined to better support these changes.

Changes

Files Change Summary
cms/config/plugins.ts Added configuration for betagouv plugin.
cms/src/plugins/betagouv/admin/src/... (multiple files) Introduced React components, hooks, and plugin initialization for managing and displaying flows.
cms/src/plugins/betagouv/server/... (multiple files) Added server-side logic for flow operations and plugin lifecycle.
docker/cms/Dockerfile
docker/cms/prod/Dockerfile
Modified Docker build process to include --workspaces flag and an additional build command.

"In a world of code and data streams,

A rabbit hopped, chasing its dreams.

With every line, a new path unwinds,

In the maze of flows, its way it finds. 🐰✨"

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Datayama38
Datayama38 previously approved these changes Jan 30, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 5f64f35 and 83ae655.
Files ignored due to path filters (8)
  • cms/package-lock.json is excluded by: !**/*.json
  • cms/package.json is excluded by: !**/*.json
  • cms/src/api/row/content-types/row/schema.json is excluded by: !**/*.json
  • cms/src/plugins/betagouv/admin/src/translations/en.json is excluded by: !**/*.json
  • cms/src/plugins/betagouv/admin/src/translations/fr.json is excluded by: !**/*.json
  • cms/src/plugins/betagouv/package.json is excluded by: !**/*.json
  • cms/src/plugins/betagouv/tsconfig.json is excluded by: !**/*.json
  • cms/src/plugins/betagouv/tsconfig.server.json is excluded by: !**/*.json
Files selected for processing (30)
  • cms/config/plugins.ts (1 hunks)
  • cms/src/plugins/betagouv/admin/src/components/FlowCard.tsx (1 hunks)
  • cms/src/plugins/betagouv/admin/src/components/Initializer/index.tsx (1 hunks)
  • cms/src/plugins/betagouv/admin/src/components/PluginIcon/index.tsx (1 hunks)
  • cms/src/plugins/betagouv/admin/src/hooks/useFlowList.ts (1 hunks)
  • cms/src/plugins/betagouv/admin/src/hooks/useFlowRun.ts (1 hunks)
  • cms/src/plugins/betagouv/admin/src/index.tsx (1 hunks)
  • cms/src/plugins/betagouv/admin/src/pages/App/index.tsx (1 hunks)
  • cms/src/plugins/betagouv/admin/src/pages/HomePage/index.tsx (1 hunks)
  • cms/src/plugins/betagouv/admin/src/pluginId.ts (1 hunks)
  • cms/src/plugins/betagouv/admin/src/utils/getTrad.ts (1 hunks)
  • cms/src/plugins/betagouv/custom.d.ts (1 hunks)
  • cms/src/plugins/betagouv/server/bootstrap.ts (1 hunks)
  • cms/src/plugins/betagouv/server/config/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/content-types/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/controllers/flows.ts (1 hunks)
  • cms/src/plugins/betagouv/server/controllers/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/destroy.ts (1 hunks)
  • cms/src/plugins/betagouv/server/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/middlewares/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/policies/has-permission.ts (1 hunks)
  • cms/src/plugins/betagouv/server/policies/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/register.ts (1 hunks)
  • cms/src/plugins/betagouv/server/routes/index.ts (1 hunks)
  • cms/src/plugins/betagouv/server/services/flows.ts (1 hunks)
  • cms/src/plugins/betagouv/server/services/index.ts (1 hunks)
  • cms/src/plugins/betagouv/strapi-admin.js (1 hunks)
  • cms/src/plugins/betagouv/strapi-server.js (1 hunks)
  • docker/cms/Dockerfile (1 hunks)
  • docker/cms/prod/Dockerfile (1 hunks)
Files skipped from review due to trivial changes (10)
  • cms/src/plugins/betagouv/admin/src/components/PluginIcon/index.tsx
  • cms/src/plugins/betagouv/admin/src/pluginId.ts
  • cms/src/plugins/betagouv/custom.d.ts
  • cms/src/plugins/betagouv/server/content-types/index.ts
  • cms/src/plugins/betagouv/server/controllers/index.ts
  • cms/src/plugins/betagouv/server/middlewares/index.ts
  • cms/src/plugins/betagouv/server/policies/index.ts
  • cms/src/plugins/betagouv/server/services/index.ts
  • cms/src/plugins/betagouv/strapi-server.js
  • docker/cms/prod/Dockerfile
Additional comments: 17
cms/src/plugins/betagouv/server/config/index.ts (1)
  • 1-4: The configuration file is correctly structured for a Strapi plugin, with placeholders for default configurations and validation logic. This setup allows for future expansion and customization of the plugin's configuration without breaking changes.
cms/src/plugins/betagouv/strapi-admin.js (1)
  • 1-3: The module export structure is correctly implemented for integrating the plugin's admin UI with Strapi. This ensures the plugin's admin UI components are correctly loaded and available within the Strapi admin panel.
cms/src/plugins/betagouv/admin/src/utils/getTrad.ts (1)
  • 1-5: The getTrad utility function is correctly implemented for generating namespaced translation IDs. This approach ensures that translation IDs are unique and scoped to the plugin, avoiding conflicts with other plugins or Strapi itself.
cms/src/plugins/betagouv/server/destroy.ts (1)
  • 1-5: The destroy phase is correctly set up as a placeholder for future cleanup tasks. Including this in the plugin structure from the beginning is a good practice, allowing for easy addition of cleanup logic as the plugin evolves.
cms/src/plugins/betagouv/server/register.ts (1)
  • 1-5: The register phase is correctly set up as a placeholder, indicating no specific registration logic is required at this stage. This approach allows for easy addition of registration logic in the future if needed.
cms/src/plugins/betagouv/server/bootstrap.ts (1)
  • 1-5: The bootstrap phase is correctly set up as a placeholder, suggesting no initialization logic is needed at this point. This setup allows for the straightforward addition of initialization logic as the plugin's requirements evolve.
cms/src/plugins/betagouv/server/routes/index.ts (1)
  • 1-18: The routes configuration is correctly implemented, with HTTP methods, paths, handlers, and policies clearly defined for each route. The use of plugin::betagouv.hasPermission as a policy ensures that access is controlled based on the user's permissions.
cms/src/plugins/betagouv/server/controllers/flows.ts (1)
  • 1-16: The flows controller is well-implemented, with run and index methods correctly using async/await for asynchronous operations. The chaining of plugin service calls is clear and follows best practices for Node.js development.
cms/src/plugins/betagouv/admin/src/components/Initializer/index.tsx (1)
  • 1-24: The Initializer component is correctly implemented, using a useEffect hook to initialize plugin-specific settings upon component mounting. This pattern ensures that the plugin is correctly set up when the admin UI loads.
docker/cms/Dockerfile (1)
  • 17-18: The addition of the --workspaces flag to the npm run build command in the Dockerfile is a good practice for optimizing the build process in projects using workspaces. This change ensures that dependencies are correctly managed and built across the monorepo.
cms/src/plugins/betagouv/server/index.ts (1)
  • 1-23: The server-side export structure of the plugin is correctly implemented, facilitating the integration of various plugin components such as lifecycle methods, configurations, controllers, routes, and services with the Strapi ecosystem.
cms/src/plugins/betagouv/admin/src/hooks/useFlowList.ts (1)
  • 1-20: The useFlowList hook is well-implemented, correctly using React's useState and useEffect hooks for managing the loading state and fetching data. The integration with Strapi's custom fetch client ensures that data fetching is consistent with Strapi's architecture.
cms/src/plugins/betagouv/admin/src/pages/App/index.tsx (1)
  • 1-26: The App component is correctly set up with routing for the plugin's pages within the Strapi admin UI. The use of Switch and Route for defining page routes and the fallback to AnErrorOccurred for unmatched routes are best practices for React applications.
cms/src/plugins/betagouv/admin/src/components/FlowCard.tsx (1)
  • 1-20: The FlowCard component is well-implemented, correctly using the useFlowRun hook for executing flows and managing state, and useIntl for internationalization. This ensures that the component is functional, maintainable, and supports multiple languages.
cms/config/plugins.ts (1)
  • 36-39: The addition of the betagouv plugin configuration block is correctly implemented, with enabled and resolve properties specified. This configuration pattern is consistent with Strapi's approach to plugin management, allowing for easy enabling and integration of the plugin within the project.
cms/src/plugins/betagouv/admin/src/pages/HomePage/index.tsx (1)
  • 1-37: The HomePage component is well-implemented, correctly using the useFlowList hook for fetching flow data and displaying it using GridLayout and FlowCard components. The modular and responsive UI design, combined with internationalization support through useIntl, follows best practices for Strapi plugins.
cms/src/plugins/betagouv/admin/src/index.tsx (1)
  • 1-67: The plugin registration process is correctly implemented, including adding a menu link, defining the plugin icon, and setting up internationalization. The use of registerPlugin and registerTrads methods, along with the async loading of the App component, follows Strapi's best practices for plugin development.

cms/src/plugins/betagouv/server/policies/has-permission.ts Outdated Show resolved Hide resolved
Comment on lines +1 to +23
import { useState } from 'react';
import { useFetchClient } from '@strapi/helper-plugin';
import pluginId from '../pluginId';

export function useFlowRun(id: string) {
const { post } = useFetchClient();
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);

async function run() {
setIsLoading(true);
try {
await post(`${pluginId}/flows/${id}`);
setIsError(false);
} catch(e) {
console.error(e)
setIsError(true);
} finally {
setIsLoading(false);
}
}
return [run, isLoading, isError];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useFlowRun hook is correctly implemented for executing flows and managing loading and error states. Consider replacing console.error with a more robust error handling strategy, such as reporting errors to an error tracking service, to improve maintainability and debuggability in production environments.

-      console.error(e)
+      // Consider replacing console.error with a more robust error handling strategy

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import { useState } from 'react';
import { useFetchClient } from '@strapi/helper-plugin';
import pluginId from '../pluginId';
export function useFlowRun(id: string) {
const { post } = useFetchClient();
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
async function run() {
setIsLoading(true);
try {
await post(`${pluginId}/flows/${id}`);
setIsError(false);
} catch(e) {
console.error(e)
setIsError(true);
} finally {
setIsLoading(false);
}
}
return [run, isLoading, isError];
}
import { useState } from 'react';
import { useFetchClient } from '@strapi/helper-plugin';
import pluginId from '../pluginId';
export function useFlowRun(id: string) {
const { post } = useFetchClient();
const [isLoading, setIsLoading] = useState(false);
const [isError, setIsError] = useState(false);
async function run() {
setIsLoading(true);
try {
await post(`${pluginId}/flows/${id}`);
setIsError(false);
} catch(e) {
// Consider replacing console.error with a more robust error handling strategy
setIsError(true);
} finally {
setIsLoading(false);
}
}
return [run, isLoading, isError];
}

Comment on lines +1 to +71
import { Strapi } from '@strapi/strapi';

type FlowFn = () => Promise<boolean>;

const flows: Record<string, FlowFn> = {
flush_cache: async() => {
const url = `https://api.covoiturage.beta.gouv.fr/cache`;
const params = {
method: 'DELETE',
headers: {
'X-Route-Cache-Auth': `${process.env.FLOW_FLUSH_CACHE_TOKEN}`,
},
};

if(!process.env.FLOW_FLUSH_CACHE_TOKEN) {
console.debug('flush_cache', url, params);
return true;
}

const response = await fetch(url, params);
if (!response.ok) {
return false;
}
return true;
},
deploy: async() => {
const url = `https://api.github.com/repos/betagouv/preuve-covoiturage/actions/workflows/deploy-frontends-public.yml/dispatches`
const params = {
method: 'POST',
headers: {
'Authorization': `token ${process.env.FLOW_DEPLOY_GH_TOKEN}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
ref: 'main'
}),
};

if(!process.env.FLOW_DEPLOY_GH_TOKEN) {
console.debug('deploy', url, params);
return true;
}

const response = await fetch(url, params);

if (!response.ok) {
const errorMsg = await response.text();
console.error(errorMsg);
return false;
}

return true;
},
};

export default ({ strapi }: { strapi: Strapi }) => ({
list() {
return Object.keys(flows);
},
async run(id: string) {
if (!(id in flows)) {
return;
}
const success = await flows[id]();
return {
success,
flow: id,
};
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The flows service is correctly implemented, with secure and asynchronous operations for flush_cache and deploy flows. Consider replacing console.debug and console.error with a more robust logging and error handling strategy to improve maintainability and debuggability in production environments.

-      console.debug('flush_cache', url, params);
-      console.error(errorMsg);
+      // Consider replacing console.debug and console.error with a more robust logging strategy

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import { Strapi } from '@strapi/strapi';
type FlowFn = () => Promise<boolean>;
const flows: Record<string, FlowFn> = {
flush_cache: async() => {
const url = `https://api.covoiturage.beta.gouv.fr/cache`;
const params = {
method: 'DELETE',
headers: {
'X-Route-Cache-Auth': `${process.env.FLOW_FLUSH_CACHE_TOKEN}`,
},
};
if(!process.env.FLOW_FLUSH_CACHE_TOKEN) {
console.debug('flush_cache', url, params);
return true;
}
const response = await fetch(url, params);
if (!response.ok) {
return false;
}
return true;
},
deploy: async() => {
const url = `https://api.github.com/repos/betagouv/preuve-covoiturage/actions/workflows/deploy-frontends-public.yml/dispatches`
const params = {
method: 'POST',
headers: {
'Authorization': `token ${process.env.FLOW_DEPLOY_GH_TOKEN}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
ref: 'main'
}),
};
if(!process.env.FLOW_DEPLOY_GH_TOKEN) {
console.debug('deploy', url, params);
return true;
}
const response = await fetch(url, params);
if (!response.ok) {
const errorMsg = await response.text();
console.error(errorMsg);
return false;
}
return true;
},
};
export default ({ strapi }: { strapi: Strapi }) => ({
list() {
return Object.keys(flows);
},
async run(id: string) {
if (!(id in flows)) {
return;
}
const success = await flows[id]();
return {
success,
flow: id,
};
}
});
import { Strapi } from '@strapi/strapi';
type FlowFn = () => Promise<boolean>;
const flows: Record<string, FlowFn> = {
flush_cache: async() => {
const url = `https://api.covoiturage.beta.gouv.fr/cache`;
const params = {
method: 'DELETE',
headers: {
'X-Route-Cache-Auth': `${process.env.FLOW_FLUSH_CACHE_TOKEN}`,
},
};
if(!process.env.FLOW_FLUSH_CACHE_TOKEN) {
// Consider replacing console.debug and console.error with a more robust logging strategy
return true;
}
const response = await fetch(url, params);
if (!response.ok) {
return false;
}
return true;
},
deploy: async() => {
const url = `https://api.github.com/repos/betagouv/preuve-covoiturage/actions/workflows/deploy-frontends-public.yml/dispatches`
const params = {
method: 'POST',
headers: {
'Authorization': `token ${process.env.FLOW_DEPLOY_GH_TOKEN}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
ref: 'main'
}),
};
if(!process.env.FLOW_DEPLOY_GH_TOKEN) {
// Consider replacing console.debug and console.error with a more robust logging strategy
return true;
}
const response = await fetch(url, params);
if (!response.ok) {
// Consider replacing console.debug and console.error with a more robust logging strategy
return false;
}
return true;
},
};
export default ({ strapi }: { strapi: Strapi }) => ({
list() {
return Object.keys(flows);
},
async run(id: string) {
if (!(id in flows)) {
return;
}
const success = await flows[id]();
return {
success,
flow: id,
};
}
});

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 83ae655 and b6e9ad8.
Files selected for processing (1)
  • cms/src/plugins/betagouv/server/policies/has-permission.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • cms/src/plugins/betagouv/server/policies/has-permission.ts

@Datayama38 Datayama38 merged commit 9fe7c18 into main Jan 31, 2024
5 checks passed
@Datayama38 Datayama38 deleted the feat/cms_plugin branch January 31, 2024 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants