Skip to content

Commit d3b7b0d

Browse files
Add Claude Code GitHub Workflow (#462)
* Claude PR Assistant workflow * Claude Code Review workflow * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add CLAUDE.mc * add time limit and filtering conditions for claude code GH actions --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 00e879d commit d3b7b0d

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Claude Code Review
2+
3+
on:
4+
pull_request:
5+
types: [opened]
6+
# Optional: Only run on specific file changes
7+
# paths:
8+
# - "src/**/*.ts"
9+
# - "src/**/*.tsx"
10+
# - "src/**/*.js"
11+
# - "src/**/*.jsx"
12+
13+
jobs:
14+
claude-review:
15+
# Skip review for draft PRs, WIP, or explicitly skipped PRs
16+
if: |
17+
!contains(github.event.pull_request.title, '[skip-review]') &&
18+
!contains(github.event.pull_request.title, '[WIP]') &&
19+
!github.event.pull_request.draft
20+
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 30
23+
permissions:
24+
contents: read
25+
pull-requests: read
26+
issues: read
27+
id-token: write
28+
29+
steps:
30+
- name: Checkout repository
31+
uses: actions/checkout@v4
32+
with:
33+
fetch-depth: 1
34+
35+
- name: Validate required secrets
36+
run: |
37+
if [ -z "${{ secrets.ANTHROPIC_API_KEY }}" ]; then
38+
echo "Error: Missing required secret ANTHROPIC_API_KEY"
39+
echo "Please add the ANTHROPIC_API_KEY secret to your repository settings"
40+
exit 1
41+
fi
42+
43+
- name: Run Claude Code Review
44+
id: claude-review
45+
uses: anthropics/claude-code-action@beta
46+
with:
47+
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
48+
49+
# Optional: Specify model (defaults to Claude Sonnet 4, uncomment for Claude Opus 4)
50+
# model: "claude-opus-4-20250514"
51+
52+
# Direct prompt for automated review (no @claude mention needed)
53+
direct_prompt: |
54+
Please review this pull request for the linopy optimization library. Focus on:
55+
- Python best practices and type safety (we use mypy for type checking)
56+
- Proper xarray integration patterns and dimension handling
57+
- Performance implications for large-scale optimization problems
58+
- Mathematical correctness in solver interfaces and constraint formulations
59+
- Memory efficiency considerations for handling large arrays
60+
- Test coverage and edge cases
61+
- Consistency with the existing codebase patterns, avoiding redundant code
62+
63+
Linopy is built on xarray and provides N-dimensional labeled arrays for variables and constraints.
64+
Be constructive and specific in your feedback.
65+
66+
# Optional: Customize review based on file types
67+
# direct_prompt: |
68+
# Review this PR focusing on:
69+
# - For TypeScript files: Type safety and proper interface usage
70+
# - For API endpoints: Security, input validation, and error handling
71+
# - For React components: Performance, accessibility, and best practices
72+
# - For tests: Coverage, edge cases, and test quality
73+
74+
# Optional: Different prompts for different authors
75+
# direct_prompt: |
76+
# ${{ github.event.pull_request.author_association == 'FIRST_TIME_CONTRIBUTOR' &&
77+
# 'Welcome! Please review this PR from a first-time contributor. Be encouraging and provide detailed explanations for any suggestions.' ||
78+
# 'Please provide a thorough code review focusing on our coding standards and best practices.' }}
79+
80+
# Project-specific tools for Python development
81+
allowed_tools: "Bash(pytest),Bash(ruff check .),Bash(ruff format .),Bash(mypy .),Bash(uv pip install -e .[dev,solvers])"
82+
83+
# Optional: Skip review for certain conditions
84+
# if: |
85+
# !contains(github.event.pull_request.title, '[skip-review]') &&
86+
# !contains(github.event.pull_request.title, '[WIP]')

.github/workflows/claude.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Claude Code
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
pull_request_review_comment:
7+
types: [created]
8+
issues:
9+
types: [opened, assigned]
10+
pull_request_review:
11+
types: [submitted]
12+
13+
jobs:
14+
claude:
15+
# This workflow triggers when @claude is mentioned in:
16+
# - Issue comments (on issues or PRs)
17+
# - Pull request review comments (inline code comments)
18+
# - Pull request reviews (general review comments)
19+
# - New issues (in title or body)
20+
if: |
21+
(github.event_name == 'issue_comment' && contains(github.event.comment.body, '@claude')) ||
22+
(github.event_name == 'pull_request_review_comment' && contains(github.event.comment.body, '@claude')) ||
23+
(github.event_name == 'pull_request_review' && contains(github.event.review.body, '@claude')) ||
24+
(github.event_name == 'issues' && (contains(github.event.issue.body, '@claude') || contains(github.event.issue.title, '@claude')))
25+
runs-on: ubuntu-latest
26+
timeout-minutes: 30
27+
permissions:
28+
contents: read
29+
pull-requests: read
30+
issues: read
31+
id-token: write
32+
steps:
33+
- name: Checkout repository
34+
uses: actions/checkout@v4
35+
with:
36+
fetch-depth: 1
37+
38+
- name: Validate required secrets
39+
run: |
40+
if [ -z "${{ secrets.ANTHROPIC_API_KEY }}" ]; then
41+
echo "Error: Missing required secret ANTHROPIC_API_KEY"
42+
echo "Please add the ANTHROPIC_API_KEY secret to your repository settings"
43+
exit 1
44+
fi
45+
46+
- name: Run Claude Code
47+
id: claude
48+
uses: anthropics/claude-code-action@beta
49+
with:
50+
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
51+
52+
# Optional: Specify model (defaults to Claude Sonnet 4, uncomment for Claude Opus 4)
53+
# model: "claude-opus-4-20250514"
54+
55+
# Optional: Customize the trigger phrase (default: @claude)
56+
# trigger_phrase: "/claude"
57+
58+
# Optional: Trigger when specific user is assigned to an issue
59+
# assignee_trigger: "claude-bot"
60+
61+
# Project-specific tools for Python development
62+
allowed_tools: "Bash(pytest),Bash(ruff check .),Bash(ruff format .),Bash(mypy .),Bash(uv pip install -e .[dev,solvers])"
63+
64+
# Custom instructions for linopy project
65+
custom_instructions: |
66+
You are working on linopy, a optimization library built on xarray.
67+
Follow these guidelines:
68+
- Use type hints and ensure mypy compliance
69+
- Follow xarray patterns for dimension handling
70+
- Write tests for new features or bug fixes
71+
- Use ruff for linting and formatting (run ruff check --fix .)
72+
- Place tests in the test directory with test_*.py naming
73+
- Maintain consistency with existing codebase patterns, avoiding redundant code
74+
- Consider memory efficiency for large-scale optimization problems
75+
76+
# Optional: Custom environment variables for Claude
77+
# claude_env: |
78+
# NODE_ENV: test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ benchmark/scripts/leftovers/
3939

4040
# IDE
4141
.idea/
42+
43+
# Claude
44+
.claude/settings.local.json

CLAUDE.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# CLAUDE.md
2+
3+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4+
5+
## Common Development Commands
6+
7+
### Running Tests
8+
```bash
9+
# Run all tests
10+
pytest
11+
12+
# Run tests with coverage
13+
pytest --cov=./ --cov-report=xml linopy --doctest-modules test
14+
15+
# Run a specific test file
16+
pytest test/test_model.py
17+
18+
# Run a specific test function
19+
pytest test/test_model.py::test_model_creation
20+
```
21+
22+
### Linting and Type Checking
23+
```bash
24+
# Run linter (ruff)
25+
ruff check .
26+
ruff check --fix . # Auto-fix issues
27+
28+
# Run formatter
29+
ruff format .
30+
31+
# Run type checker
32+
mypy .
33+
34+
# Run all pre-commit hooks
35+
pre-commit run --all-files
36+
```
37+
38+
### Development Setup
39+
```bash
40+
# Create virtual environment and install development dependencies
41+
python -m venv venv
42+
source venv/bin/activate # On Windows: venv\Scripts\activate
43+
pip install uv
44+
uv pip install -e .[dev,solvers]
45+
```
46+
47+
## High-Level Architecture
48+
49+
linopy is a linear optimization library built on top of xarray, providing N-dimensional labeled arrays for variables and constraints. The architecture follows these key principles:
50+
51+
### Core Components
52+
53+
1. **Model** (`model.py`): Central container for optimization problems
54+
- Manages variables, constraints, and objective
55+
- Handles solver integration through abstract interfaces
56+
- Supports chunked operations for memory efficiency
57+
- Provides matrix representations for solver APIs
58+
59+
2. **Variables** (`variables.py`): Multi-dimensional decision variables
60+
- Built on xarray.Dataset with labels, lower, and upper bounds
61+
- Arithmetic operations automatically create LinearExpressions
62+
- Support for continuous and binary variables
63+
- Container class (Variables) manages collections with dict-like access
64+
65+
3. **Constraints** (`constraints.py`): Linear inequality/equality constraints
66+
- Store coefficients, variable references, signs, and RHS values
67+
- Support ≤, ≥, and = constraints
68+
- Container class (Constraints) provides organized access
69+
70+
4. **Expressions** (`expressions.py`): Linear combinations of variables
71+
- LinearExpression: coeffs × vars + const
72+
- QuadraticExpression: for non-linear optimization
73+
- Support full arithmetic operations with automatic broadcasting
74+
- Special `_term` dimension for handling multiple terms
75+
76+
5. **Solvers** (`solvers.py`): Abstract interface with multiple implementations
77+
- File-based solvers: Write LP/MPS files, call solver, parse results
78+
- Direct API solvers: Use Python bindings (e.g., gurobipy)
79+
- Automatic solver detection based on installed packages
80+
81+
### Data Flow Pattern
82+
83+
1. User creates Model and adds Variables with coordinates (dimensions)
84+
2. Variables combined into LinearExpressions through arithmetic
85+
3. Expressions used to create Constraints and Objective
86+
4. Model.solve() converts to solver format and retrieves solution
87+
5. Solution stored back in xarray format with original dimensions
88+
89+
### Key Design Patterns
90+
91+
- **xarray Integration**: All data structures use xarray for dimension handling
92+
- **Lazy Evaluation**: Expressions built symbolically before solving
93+
- **Broadcasting**: Operations automatically align dimensions
94+
- **Solver Abstraction**: Clean separation between model and solver specifics
95+
- **Memory Efficiency**: Support for dask arrays and chunked operations
96+
97+
When modifying the codebase, maintain consistency with these patterns and ensure new features integrate naturally with the xarray-based architecture.
98+
99+
## Working with the Github Repository
100+
101+
* The main branch is `master`.
102+
* Always create a feature branch for new features or bug fixes.
103+
* Use the github cli (gh) to interact with the Github repository.
104+
105+
### GitHub Claude Code Integration
106+
107+
This repository includes Claude Code GitHub Actions for automated assistance:
108+
109+
1. **Automated PR Reviews** (`claude-code-review.yml`):
110+
- Automatically reviews PRs only when first created (opened)
111+
- Subsequent reviews require manual `@claude` mention
112+
- Focuses on Python best practices, xarray patterns, and optimization correctness
113+
- Can run tests and linting as part of the review
114+
- **Skip initial review by**: Adding `[skip-review]` or `[WIP]` to PR title, or using draft PRs
115+
116+
2. **Manual Claude Assistance** (`claude.yml`):
117+
- Trigger by mentioning `@claude` in any:
118+
- Issue comments
119+
- Pull request comments
120+
- Pull request reviews
121+
- New issue body or title
122+
- Claude can help with bug fixes, feature implementation, code explanations, etc.
123+
124+
**Note**: Both workflows require the `ANTHROPIC_API_KEY` secret to be configured in the repository settings.
125+
126+
127+
## Development Guidelines
128+
129+
1. Always write tests for new features or bug fixes.
130+
2. Always run the tests after making changes and ensure they pass.
131+
3. Always use ruff for linting and formatting, run `ruff check --fix .` to auto-fix issues.
132+
4. Use type hints and mypy for type checking.
133+
5. Always write tests into the `test` directory, following the naming convention `test_*.py`.
134+
6. Always write temporary and non git-tracked code in the `dev-scripts` directory.

0 commit comments

Comments
 (0)