Skip to content

Commit ffc9700

Browse files
Merge pull request #1 from codeflash-ai/github-actions-setup
setup github actions
2 parents cada1c9 + 3f0fd3f commit ffc9700

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1502
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Codeflash Docs Publish to Azure Static Web Apps
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- 'docs/**'
9+
- '.github/workflows/deploy-docs-to-azure.yaml'
10+
workflow_dispatch:
11+
12+
jobs:
13+
build_and_deploy_job:
14+
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
15+
runs-on: ubuntu-latest
16+
name: Build and Deploy Job
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
submodules: true
21+
- name: Build And Deploy
22+
id: builddeploy
23+
uses: Azure/static-web-apps-deploy@v1
24+
with:
25+
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
26+
repo_token: ${{ secrets.GITHUB_TOKEN }} # Used for GitHub integrations (i.e. PR comments)
27+
action: "upload"
28+
###### Repository/Build Configurations ######
29+
app_location: "docs" # App source code path relative to repository root
30+
output_location: "build" # Built app content directory, relative to app_location - optional
31+
###### End of Repository/Build Configurations ######

.github/workflows/unit-tests.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: unit-tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
workflow_dispatch:
8+
9+
jobs:
10+
unit-tests:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
python-version: [3.9.18, 3.10.13, 3.11.6, 3.12.1, 3.13.0]
15+
continue-on-error: true
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
23+
- name: Install uv
24+
uses: astral-sh/setup-uv@v5
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
version: "0.5.30"
28+
- name: install poetry as a tool
29+
run: uv tool install poetry
30+
31+
- name: install dependencies
32+
run: uvx poetry install --with dev
33+
34+
- name: Unit tests
35+
run: uvx poetry run pytest tests/ --cov --cov-report=xml
36+
37+
- name: Upload coverage reports to Codecov
38+
uses: codecov/codecov-action@v5
39+
if: matrix.python-version == '3.12.1'
40+
with:
41+
token: ${{ secrets.CODECOV_TOKEN }}

docs/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Dependencies
2+
/node_modules
3+
4+
# Production
5+
/build
6+
7+
# Generated files
8+
.docusaurus
9+
.cache-loader
10+
11+
# Misc
12+
.DS_Store
13+
.env
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*

docs/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Website
2+
3+
This website is built using [Docusaurus](https://docusaurus.io/), a modern static website generator.
4+
5+
### Installation
6+
7+
```
8+
$ npm install
9+
```
10+
11+
### Local Development
12+
13+
```
14+
$ npm run start
15+
```
16+
17+
This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.
18+
19+
### Build
20+
21+
```
22+
$ npm run build
23+
```
24+
25+
This command generates static content into the `build` directory and can be served using any static contents hosting service.
26+
27+
### Deployment
28+
29+
Deployment is done automatically via GitHub Actions when the Pull Request changes are merged to the `main` branch.

docs/babel.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
3+
};

docs/docs/configuration.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
sidebar_position: 5
3+
---
4+
5+
# Manual Configuration
6+
7+
Codeflash is installed and configured on a per-project basis.
8+
`codeflash init` should guide you through the configuration process, but if you need to manually configure Codeflash or set advanced settings, you can do so by editing the `pyproject.toml` file in the root directory of your project.
9+
10+
## Configuration Options
11+
Codeflash config looks like the following
12+
```toml
13+
[tool.codeflash]
14+
module-root = "my_module"
15+
tests-root = "tests"
16+
test-framework = "pytest"
17+
formatter-cmds = ["black $file"]
18+
# optional configuration
19+
ignore-paths = ["my_module/build/"]
20+
pytest-cmd = "pytest"
21+
disable-imports-sorting = false
22+
disable-telemetry = false
23+
```
24+
All file paths are relative to the directory of the `pyproject.toml` file.
25+
26+
Required Options:
27+
- `module-root`: The Python module you want Codeflash to optimize going forward. Only code under this directory will be optimized. It should also have an `__init__.py` file to make the module importable.
28+
- `tests-root`: The directory where your tests are located. Codeflash will use this directory to discover existing tests as well as generate new tests.
29+
- `test-framework`: The test framework you use for your project. Codeflash supports `pytest` and `unittest`.
30+
31+
Optional Configuration:
32+
- `ignore-paths`: A list of paths withing the `module-root` to ignore when optimizing code. Codeflash will not optimize code in these paths. Useful for ignoring build directories or other generated code. You can also leave this empty if not needed.
33+
- `pytest-cmd`: The command to run your tests. Defaults to `pytest`. You can specify extra commandline arguments here for pytest.
34+
- `formatter-cmds`: The command line to run your code formatter or linter. Defaults to `["black $file"]`. In the command line `$file` refers to the current file being optimized. The assumption with using tools here is that they overwrite the same file and returns a zero exit code. You can also specify multiple tools here that run in a chain as a toml array. You can also disable code formatting by setting this to `["disabled"]`.
35+
- `ruff` - A recommended way to run ruff linting and formatting is `["ruff check --exit-zero --fix $file", "ruff format $file"]`. To make `ruff check --fix` return a 0 exit code please add a `--exit-zero` argument.
36+
- `disable-imports-sorting`: By default, codeflash uses isort to organize your imports before creating suggestions. You can disable this by setting this field to `true`. This could be useful if you don't sort your imports or while using linters like ruff that sort imports too.
37+
- `disable-telemetry`: Disable telemetry data collection. Defaults to `false`. Set this to `true` to disable telemetry data collection. Codeflash collects anonymized telemetry data to understand how users are using Codeflash and to improve the product. Telemetry does not collect any code data.
38+
39+
## Example Configuration
40+
Here's an example project with the following structure:
41+
```text
42+
acme-project/
43+
|- foo_module/
44+
| |- __init__.py
45+
| |- foo.py
46+
| |- main.py
47+
|- tests/
48+
| |- __init__.py
49+
| |- test_script.py
50+
|- pyproject.toml
51+
```
52+
53+
Here's a sample `pyproject.toml` file for the above project:
54+
```toml
55+
[tool.codeflash]
56+
module-root = "foo_module"
57+
tests-root = "tests"
58+
test-framework = "pytest" # or "unittest"
59+
ignore-paths = []
60+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"label": "Getting Started",
3+
"position": 2,
4+
"collapsed": false
5+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
sidebar_position: 1
3+
---
4+
5+
# Local Installation
6+
7+
Codeflash is installed and configured on a per-project basis.
8+
9+
You can install Codeflash locally for a project by running the following command in the project's virtual environment:
10+
11+
```bash
12+
pip install codeflash
13+
```
14+
15+
:::tip[Codeflash is a Development Dependency]
16+
We recommend installing Codeflash as a development dependency.
17+
It doesn't need to be installed as part of your package requirements.
18+
Codeflash is intended to be used locally and as part of development workflows such as CI.
19+
:::
20+
21+
## Generate a Codeflash API Key
22+
23+
Since Codeflash uses advanced Large Language Models (LLMs) that are hosted in the cloud, you will need to generate an API key to use Codeflash.
24+
25+
To generate an API key, visit the [Codeflash Web App](https://app.codeflash.ai/) and sign up for an account with GitHub login.
26+
<!--- TODO: Do we ask for access to specific repositories here? --->
27+
Once you have signed up, you will be able to generate an API key from the [API Key](https://app.codeflash.ai/app/apikeys) page.
28+
You will need the API key in the next step.
29+
30+
## Automatic Configuration
31+
32+
To configure Codeflash for a project, in the root directory of your project where your `pyproject.toml` file is located, run the following command :
33+
34+
```bash
35+
codeflash init
36+
```
37+
38+
If you don't have a pyproject.toml file yet, the codeflash init command will ask you to create one
39+
40+
:::tip[What's pyproject.toml?]
41+
`pyproject.toml` is a configuration file that is used to specify build tool settings for Python projects.
42+
pyproject.toml is the modern replacement for setup.py and requirements.txt files.
43+
It's the new standard for Python package metadata.
44+
:::
45+
46+
When running `codeflash init`, you will see the following prompts:
47+
48+
```text
49+
1. Enter your Codeflash API key:
50+
2. Which Python module do you want me to optimize going forward? (e.g. my_module)
51+
3. Where are your tests located? (e.g. tests/)
52+
4. Which test framework do you use? (pytest/unittest)
53+
```
54+
55+
After you have answered these questions, Codeflash will be configured for your project.
56+
The configuration will be saved in the `pyproject.toml` file in the root directory of your project.
57+
To understand the configuration options, and set more advanced options, see the [Configuration](/configuration) page.
58+
59+
## Install the Codeflash GitHub App
60+
61+
Finally, if you have not done so already, Codeflash will ask you to install the Github App in your repository. The Codeflash GitHub App allows access to your repository to the codeflash-ai bot to open PRs, review code, and provide optimization suggestions.
62+
63+
Please [install the Codeflash GitHub
64+
app](https://github.com/apps/codeflash-ai/installations/select_target) by choosing the repository you want to install
65+
Codeflash on.
66+
##

0 commit comments

Comments
 (0)