-
Notifications
You must be signed in to change notification settings - Fork 12
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
Feat: cms plugin #2393
Conversation
WalkthroughA new Changes
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this 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
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, withrun
andindex
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 auseEffect
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 thenpm 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'suseState
anduseEffect
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 ofSwitch
andRoute
for defining page routes and the fallback toAnErrorOccurred
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 theuseFlowRun
hook for executing flows and managing state, anduseIntl
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, withenabled
andresolve
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 theuseFlowList
hook for fetching flow data and displaying it usingGridLayout
andFlowCard
components. The modular and responsive UI design, combined with internationalization support throughuseIntl
, 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
andregisterTrads
methods, along with the async loading of theApp
component, follows Strapi's best practices for plugin development.
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]; | ||
} |
There was a problem hiding this comment.
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.
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]; | |
} |
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, | ||
}; | ||
} | ||
}); |
There was a problem hiding this comment.
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.
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>
There was a problem hiding this 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
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
Summary by CodeRabbit
New Features
betagouv
plugin with UI components for managing and displaying information about specific flows.Documentation
Chores
--workspaces
flag.