---
sidebar_position: 2
---

# Automate Optimization of Pull Requests
<!--- TODO: Add more pictures to guide better --->

Codeflash can automatically optimize your code when new pull requests are opened.

To be able to scan new code for performance optimizations, Codeflash requires a GitHub action workflow to 
be installed which runs the Codeflash optimization logic on every new pull request.
If the action workflow finds an optimization, it communicates with the Codeflash GitHub 
App through our secure servers and asks it to suggest new changes to the pull request.

This is the most useful way of using Codeflash, where you set it up once and all your new code gets optimized.
So setting this up is highly recommended.

## Prerequisites
- 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.
- 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.

## Install the Codeflash GitHub App

The Codeflash GitHub App allows access to your repository to the codeflash-ai bot to open PRs, review code, and provide optimization suggestions.

![Codeflash GitHub App](/img/codeflash_github_app_bot.png)

Please [install the Codeflash GitHub
app](https://github.com/apps/codeflash-ai/installations/select_target) by choosing the repository you want to install
Codeflash on.

## Add the Codeflash GitHub Actions workflow

### Guided setup

To add the Codeflash GitHub Actions workflow to your repository, you can run the following command in your project directory:

```bash
codeflash init-actions
```

This will walk you through the process of adding the Codeflash GitHub Actions workflow to your repository.

### All Set up!

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,
codeflash-ai bot will comment on your repo with the optimization suggestions.

### Manual Installation (optional)
If you prefer to install the GitHub actions manually, follow the steps below -

#### Add the workflow file
Create a new file in your repository at `.github/workflows/codeflash-optimize.yaml` with the following contents:


```yaml title=".github/workflows/codeflash-optimize.yaml"
name: Codeflash

on:
  pull_request:
  workflow_dispatch:

jobs:
  optimize:
    name: Optimize new code in this PR
    runs-on: ubuntu-latest
    env:
      CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
      CODEFLASH_PR_NUMBER: ${{ github.event.number }}
    steps:
      - name: Check if PR is from Codeflash bot
        id: bot_check
        working-directory: .
        run: |
          echo "Checking if this PR is created by Codeflash bot..."
          if [ "${{ github.event.pull_request.user.login }}" == "codeflash-ai[bot]" ]; then
            echo "PR created by Codeflash bot. Skipping optimization."
            echo "skip_remaining_steps=yes" >> $GITHUB_OUTPUT
          else
            echo "skip_remaining_steps=no" >> $GITHUB_OUTPUT
            echo "It's not. Proceeding with the optimization."
          fi
      - uses: actions/checkout@v3
        if: steps.bot_check.outputs.skip_remaining_steps == 'no'
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
      # TODO: Replace the following with your project's Python installation method
      - name: Set up Python
        if: steps.bot_check.outputs.skip_remaining_steps == 'no'
        uses: actions/setup-python@v4
        with:
          python-version: {{ python_version }}
      # TODO: Replace the following with your project's dependency installation method
      - name: Install Project Dependencies
        if: steps.bot_check.outputs.skip_remaining_steps == 'no'
        run: |
          python -m pip install --upgrade pip
        # TODO: Replace the following with your project setup method
          pip install -r requirements.txt
          pip install codeflash
      - name: Run Codeflash to optimize code
        if: steps.bot_check.outputs.skip_remaining_steps == 'no'
        id: optimize_code
        run: |
          codeflash
```
You would need to fill in the `#TODO`s in the file above to make it work. Please commit this file to your repository.

#### Add your API key to your repository secrets

Go to your GitHub repository, click **Settings**, and click on **Secrets and
Variables** -> **Actions** on the left sidebar.

Add the following secret:

- `CODEFLASH_API_KEY`: The API key you got from https://app.codeflash.ai/app/apikeys

