|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# Automate Optimization of Pull Requests |
| 6 | +<!--- TODO: Add more pictures to guide better ---> |
| 7 | + |
| 8 | +Codeflash can automatically optimize your code when new pull requests are opened. |
| 9 | + |
| 10 | +To be able to scan new code for performance optimizations, Codeflash requires a GitHub action workflow to |
| 11 | +be installed which runs the Codeflash optimization logic on every new pull request. |
| 12 | +If the action workflow finds an optimization, it communicates with the Codeflash GitHub |
| 13 | +App through our secure servers and asks it to suggest new changes to the pull request. |
| 14 | + |
| 15 | +This is the most useful way of using Codeflash, where you set it up once and all your new code gets optimized. |
| 16 | +So setting this up is highly recommended. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | +- You have a Codeflash API key. If you don't have one, you can generate one from the [Codeflash Webapp](https://app.codeflash.ai/). Make sure you generate the API key with the right GitHub account that has access to the repository you want to optimize. |
| 20 | +- You have completed [local installation](/getting-started/local-installation) steps and have a Python project with a `pyproject.toml` file that is configured with Codeflash. If you haven't configured Codeflash for your project yet, you can do so by running `codeflash init` in the root directory of your project. |
| 21 | + |
| 22 | +## Add the Codeflash GitHub Actions workflow |
| 23 | + |
| 24 | +### Guided setup |
| 25 | + |
| 26 | +To add the Codeflash GitHub Actions workflow to your repository, you can run the following command in your project directory: |
| 27 | + |
| 28 | +```bash |
| 29 | +codeflash init-actions |
| 30 | +``` |
| 31 | + |
| 32 | +This will walk you through the process of adding the Codeflash GitHub Actions workflow to your repository. |
| 33 | + |
| 34 | +### All Set up! |
| 35 | + |
| 36 | +Open a new PR to your GitHub project, and you will now see a new actions workflow for Codeflash run. If it finds an optimization, |
| 37 | +codeflash-ai bot will comment on your repo with the optimization suggestions. |
| 38 | + |
| 39 | +### Manual Installation (optional) |
| 40 | +If you prefer to install the GitHub actions manually, follow the steps below - |
| 41 | + |
| 42 | +#### Add the workflow file |
| 43 | +Create a new file in your repository at `.github/workflows/codeflash-optimize.yaml` with the following contents: |
| 44 | + |
| 45 | + |
| 46 | +```yaml title=".github/workflows/codeflash-optimize.yaml" |
| 47 | +name: Codeflash |
| 48 | + |
| 49 | +on: |
| 50 | + pull_request: |
| 51 | + workflow_dispatch: |
| 52 | + |
| 53 | +jobs: |
| 54 | + optimize: |
| 55 | + name: Optimize new code in this PR |
| 56 | + if: ${{ github.actor != 'codeflash-ai[bot]' }} |
| 57 | + runs-on: ubuntu-latest |
| 58 | + env: |
| 59 | + CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }} |
| 60 | + CODEFLASH_PR_NUMBER: ${{ github.event.number }} |
| 61 | + steps: |
| 62 | + - uses: actions/checkout@v4 |
| 63 | + with: |
| 64 | + fetch-depth: 0 |
| 65 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 66 | + # TODO: Replace the following with your project's Python installation method |
| 67 | + - name: Set up Python |
| 68 | + uses: actions/setup-python@v5 |
| 69 | + with: |
| 70 | + python-version: '3.11' |
| 71 | + # TODO: Replace the following with your project's dependency installation method |
| 72 | + - name: Install Project Dependencies |
| 73 | + run: | |
| 74 | + python -m pip install --upgrade pip |
| 75 | + # TODO: Replace the following with your project setup method |
| 76 | + pip install -r requirements.txt |
| 77 | + pip install codeflash |
| 78 | + - name: Run Codeflash to optimize code |
| 79 | + id: optimize_code |
| 80 | + run: | |
| 81 | + codeflash |
| 82 | +``` |
| 83 | +You would need to fill in the `#TODO`s in the file above to make it work. Please commit this file to your repository. |
| 84 | +If you use a particular Python package manager like Poetry or uv, some helpful configurations are provided below. |
| 85 | + |
| 86 | +#### Config with different Python package managers |
| 87 | + |
| 88 | +The yaml config above is a basic template. Here is how you can run Codeflash with the different Python package managers: |
| 89 | + |
| 90 | +1. Poetry |
| 91 | + |
| 92 | +```yaml |
| 93 | + - name: Install Project Dependencies |
| 94 | + run: | |
| 95 | + python -m pip install --upgrade pip |
| 96 | + pip install poetry |
| 97 | + poetry install --with dev |
| 98 | + - name: Run Codeflash to optimize code |
| 99 | + id: optimize_code |
| 100 | + run: | |
| 101 | + poetry env use python |
| 102 | + poetry run codeflash |
| 103 | +``` |
| 104 | +This assumes that you install poetry with pip and have Codeflash dependency in the `dev` section of your `pyproject.toml` file. |
| 105 | + |
| 106 | +2. uv |
| 107 | + |
| 108 | +```yaml |
| 109 | + - uses: astral-sh/setup-uv@v4 |
| 110 | + with: |
| 111 | + enable-cache: true |
| 112 | + - run: uv sync --group=dev |
| 113 | + - name: Run Codeflash to optimize code |
| 114 | + run: uv run codeflash |
| 115 | +``` |
| 116 | + |
| 117 | +#### Add your API key to your repository secrets |
| 118 | + |
| 119 | +Go to your GitHub repository, click **Settings**, and click on **Secrets and |
| 120 | +Variables** -> **Actions** on the left sidebar. |
| 121 | + |
| 122 | +Add the following secret: |
| 123 | + |
| 124 | +- `CODEFLASH_API_KEY`: The API key you got from https://app.codeflash.ai/app/apikeys |
| 125 | + |
0 commit comments