Para reducir el tiempo que dedico a escribir instrucciones de pull requests, he estado usando Aider con OpenRouter para generarlas.
¿Cómo funciona?
Aider es una herramienta que utiliza IA para ayudarte a escribir código. Es un chatbot que puede ayudarte a escribir código, corregir errores e incluso escribir pruebas.
OpenRouter es una herramienta que te permite enrutar peticiones a diferentes APIs. En este caso, la usaremos para enrutar la petición a Aider.
Con Aider podemos ejecutar el siguiente comando para generar una instrucción de pull request:
aider \
--model "openrouter/meta-llama/llama-3-8b-instruct:free" \
--message="/ask Generate PR description from diff.log using INSTRUCTIONS.MD and PR_TEMPLATE.MD" \
--read INSTRUCTIONS.MD \
--read PR_TEMPLATE.MD \
--read diff.log \
--no-stream \
--dry-run \
--no-git
Como puedes ver, estamos pasando el modelo que queremos usar, el mensaje que queremos enviar a Aider y los archivos que queremos usar para generar el pull request.
INSTRUCTIONS.MD
es un archivo que contiene las instrucciones para el pull request, por ejemplo:
You are a Pull Request description generator.
You generate helpful pull request descriptions using the pull request template.
You inspect the diff and fill out the template appropriately.
You are concise, insightful, and accurate.
You double check your work.
You return the filled-out template in markdown.
How to section must be in the beginning in the how to test
In how to test, use the experiment values instead of the name of the constants
PR_TEMPLATE.MD
es un archivo que contiene la plantilla para el pull request, por ejemplo:
# Description:
- Provide a clear and concise description of the changes introduced in this pull request.
- Highlight key updates or features.
# Jira:
- [x] Related ticket: ENG-
# What areas of the site does it impact?
- (Describe what parts of the site are impacted and _if_ code updated other areas) Ex. Checkout, Redemption, Homepage
# Other Notes
- (Add any additional information that would be useful to the developer or QA tester)
# How to Test
- Best way to test?
- Is this part of an A/B test? (Give experiment slug & variants)
- What should the tester look out for?
# Visual changes (if applicable)
### - Before:
| Mobile | Tablet | Desktop |
| ------------------- | ------------------- | -------------------- |
| <screenshot_mobile> | <screenshot_tablet> | <screenshot_desktop> |
### - After:
| Mobile | Tablet | Desktop |
| ------------------- | ------------------- | -------------------- |
| <screenshot_mobile> | <screenshot_tablet> | <screenshot_desktop> |
# Developer Checklist
- [ ] I have performed self-review of my code
- [ ] I have added unit tests that prove my feature works
- [ ] I have confirmed all the PR checks pass
- [ ] I have added Reviewers to this PR on Github
# Rollback Plan
- [ ] I have a documented plan to deploy and roll back if there is an issue
diff.log
es el archivo que contiene los cambios que hicimos en el código (básicamente git diff > diff.log
).
Script
Para automatizar el proceso, he creado un script que ejecuta el comando anterior y guarda la salida en un archivo.
#!/bin/bash
PROVIDER="openrouter/"
MODEL="google/gemini-2.0-flash-exp:free"
script_dir="$(cd "$(dirname "$0")" && pwd)"
# Parse command line arguments
project="my-default-project-folder" # Default project
while getopts "p:" opt; do
case $opt in
p)
project="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Change to project directory
cd ~/projects/$project
# Ticket number to use in the PR template
branch_name=$(git rev-parse --abbrev-ref HEAD)
ticket_number=$(echo "$branch_name" | awk -F'/' '{print $2}')
# Generate diff file
git diff origin/develop...HEAD > "$script_dir/diff.log"
# Go to the path where the script is located
cd "$script_dir"
aider \
--model "$PROVIDER$MODEL" \
--cache-prompts \
--message="/ask Generate PR description from diff.log using INSTRUCTIONS.MD and PR_TEMPLATE.MD (#$ticket_number)" \
--read INSTRUCTIONS.MD \
--read PR_TEMPLATE.MD \
--read diff.log \
--no-stream \
--dry-run \
--no-git \
--no-show-model-warnings
Uso
Este script es bastante específico para mis necesidades:
- Todos mis proyectos están en la misma carpeta llamada
projects
- Los números de tickets están en el nombre de la rama
- La rama con la que comparamos es
develop
Pero puedes usarlo como punto de partida para crear tu propio script.
# Ejecutar el script para el proyecto por defecto
./create-pr.sh
# Ejecutar el script para un proyecto específico
./create-pr.sh -p my-project-folder