|
| 1 | +--- |
| 2 | +id: AI-assisted code migration |
| 3 | +section: PatternFly-AI |
| 4 | +--- |
| 5 | + |
| 6 | +This guide explores a workflow that enables developers to leverage AI to accelerate code migrations, with best practices and recommendations to follow when replicating the process. |
| 7 | + |
| 8 | +## What is AI-assisted code migration? |
| 9 | + |
| 10 | +For the purposes of this guide, a code migration refers to moving your project from a legacy or dated framework to PatternFly React components. An **AI-assisted code migration** heavily utilizes AI tools and models to help speed up the process of modernizing your UI, while adhering to best practices. |
| 11 | + |
| 12 | +## What are the benefits of AI-assisted migrations? |
| 13 | + |
| 14 | +Even when its initial output is imperfect, using AI in combination with engineering judgment and domain expertise can significantly speed up the development process. |
| 15 | + |
| 16 | +While AI is *not* a replacement for human developers, it offers key benefits: |
| 17 | +- **Faster migrations:** Migrate simple pages up to 4 times faster, compared to a fully manual migration. |
| 18 | +- **Learning support:** Quickly develop an understanding of the project's unique context by prompting AI to explain topics. |
| 19 | +- **Planning support:** Identify and scope all requirements and steps needed for the migration. |
| 20 | + |
| 21 | +## Multi-phased workflow example |
| 22 | + |
| 23 | +AI serves as a powerful accelerator throughout all steps of the migration process, including learning, planning, development, and testing phases. This example outlines what an AI-assisted workflow could look like throughout a migration project. While your specific workflow will look different and be in context of your own codebase, this example is meant to serve as a reference point, outlining ideas that you can use in your own scenario. |
| 24 | + |
| 25 | +### 1. Learning phase |
| 26 | + |
| 27 | +When you're first starting a migration project, AI can help you build project-specific context by bridging knowledge gaps, setting up development environments, and identifying relevant code sections. |
| 28 | + |
| 29 | +In this workflow example, the original code is written with Angular. Here, AI can help to quickly explain Angular-specific patterns (like directives or services) and point to their equivalents in React and PatternFly. |
| 30 | + |
| 31 | +**Prompt:** |
| 32 | +_"Can you explain the Angular directives to me?"_ |
| 33 | + |
| 34 | +**Response:** |
| 35 | + |
| 36 | +  |
| 37 | + |
| 38 | +### 2. Planning phase |
| 39 | + |
| 40 | +Once you've gathered enough context in the learning phase, you can create a migration plan by asking AI to estimate scope and map features. To make the plan more concrete and comprehensive, you might find it useful to have AI: |
| 41 | +- Generate a list of components that need migration. |
| 42 | +- Identify potentially difficult areas, such as state management and routing. |
| 43 | + |
| 44 | +**Prompt:** |
| 45 | +_"I want to convert all the Katello pages that are written in Angular to React. Can you list the Katello pages that would need to be converted. Give results in a table format."_ |
| 46 | + |
| 47 | +**Response:** |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | +### 3. Development phase |
| 52 | + |
| 53 | +After establishing a migration plan, you can use AI to build new features iteratively. This can be especially useful for generating forms, validating logic, and creating reusable components. |
| 54 | + |
| 55 | +For example, you could use AI to migrate a simple Angular feature like this table: |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | +During the development phase, you could take a couple of different approaches to migrate code. You might be able to simply change the code within the same file or you might want to start fresh, using the patternfly-react-seed and following [our rapid prototyping guide.](https://www.patternfly.org/patternfly-ai/rapid-prototyping/new-prototypes) |
| 60 | + |
| 61 | +In this example, we created a placeholder page (PF6placeholder.js) and asked AI to replicate the Angular component on the new page, using PatternFly. |
| 62 | + |
| 63 | +**Prompt:** |
| 64 | +_"Copy the Angular Products page content into PF6placeholder.js using React and PatternFly6, using best practices with bias towards how other React and PatternFly files in this project are doing things."_ |
| 65 | + |
| 66 | +This simple and direct prompt quickly and successfully recreated the table using PatternFly components: |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +### 4. Testing phase |
| 71 | + |
| 72 | +Use AI to accelerate test coverage by generating boilerplate unit tests, mocks for API integrations or dependencies, and end-to-end test outlines using tools like Cypress. |
| 73 | + |
| 74 | +**Prompt:** |
| 75 | +_"Generate tests for PF6placeholder.js page following best practices and what is done in this project."_ |
| 76 | + |
| 77 | +**Response:** |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +## Best practices |
| 82 | + |
| 83 | +Because AI can make inaccurate assumptions, providing it with careful direction helps ensure that it will perform more reliably. Follow these best practices to help ensure consistency in AI behavior. |
| 84 | + |
| 85 | +- Use AI as a helper for planning and managing tasks. For example, use AI-generated to-do lists for easier and more streamlined task management. |
| 86 | +- Avoid using AI for common tasks that can be done more easily manually and explicitly. Instead ask it to focus on more complex tasks that will have more impact. |
| 87 | +- Leverage different language models throughout your migration and swap to more efficient models for different tasks. |
| 88 | +- Avoid overloading a model with a long or time consuming task. If a request is taking longer than you'd like, or is going beyond what you intended, use the "Stop" button and refine your prompt. |
| 89 | +- Ensure that you understand the product architecture and are familiar with any technologies used. Otherwise, using AI can actually be more time consuming than a manual migration. |
| 90 | +- Follow our [prompting tips](#prompting-tips) when requesting something from an AI model. |
| 91 | + |
| 92 | +### Prompting tips |
| 93 | + |
| 94 | +When interacting with AI, especially with tools like Cursor, specificity is key. In order to efficiently and successfully request something from an AI tool, there are a few "do's and don'ts" to follow: |
| 95 | + |
| 96 | +| **Don't** | **Do** | |
| 97 | +| --- | --- | |
| 98 | +| Expect AI to assume your needs. | Be specific and clearly state what you want to achieve. | |
| 99 | +| Let AI follow its own rules. | Instruct AI to follow best practices for React and PatternFly, providing clear definitions and examples. | |
| 100 | +| Restrict yourself to text-based prompts. | When text prompts are insufficient, share screenshots with AI to provide valuable context. | |
| 101 | +| Reinvent the wheel. | Use previously completed features and functionalities as models for new requests. | |
| 102 | + |
| 103 | +## Experimentation and feedback |
| 104 | + |
| 105 | +AI-assisted code migrations are still an evolving experiment, and we'd love to learn from your experiences: |
| 106 | +- Share your feedback and experiences on our [GitHub discussions board](https://github.com/orgs/patternfly/discussions). |
| 107 | +- Connect with the community on our [PatternFly Slack workspace](https://join.slack.com/t/patternfly/shared_invite/zt-1npmqswgk-bF2R1E2rglV8jz5DNTezMQ). |
| 108 | + |
| 109 | +As this process evolves, we'll continue to refine our recommendations based on community feedback and real-world usage. |
| 110 | + |
0 commit comments