Tutorials Automations
Event-Driven Refactor
This tutorial builds an event-driven refactor workflow. When a dependency is updated, an Autohand agent runs a migration script and opens a pull request with the changes.
What you will build
- A webhook receiver that listens for Dependabot or Renovate updates.
- An Autohand agent that applies migration rules.
- A script that commits changes and opens a follow-up pull request.
Prerequisites
- A repository with dependency update automation.
- A server or Worker that can receive webhooks.
- Git credentials configured for the agent environment.
Step 1: receive the webhook
Extract the dependency name and new version from the webhook payload.
JavaScript
const { dependency, version, migrationGuideUrl } = payload;
const prompt = [
`Update ${dependency} to ${version}.`,
`Follow the migration guide: ${migrationGuideUrl}.`,
'Run the relevant tests and summarize any manual follow-up.'
].join(' ');
const result = await autohand.run({ cwd: repoPath, prompt });
TypeScript
type DependencyUpdate = {
dependency: string;
version: string;
migrationGuideUrl?: string;
};
function refactorPrompt(event: DependencyUpdate): string {
return [
`Update ${event.dependency} to ${event.version}.`,
event.migrationGuideUrl ? `Follow ${event.migrationGuideUrl}.` : '',
'Run tests and summarize any manual follow-up.'
].filter(Boolean).join(' ');
}
Python
dependency = payload["dependency"]
version = payload["version"]
guide = payload.get("migration_guide_url")
prompt = f"Update {dependency} to {version}. "
if guide:
prompt += f"Follow the migration guide: {guide}. "
prompt += "Run tests and summarize any manual follow-up."
result = await sdk.run(prompt)
Go
prompt := fmt.Sprintf("Update %s to %s. ", event.Dependency, event.Version)
if event.MigrationGuideURL != "" {
prompt += fmt.Sprintf("Follow the migration guide: %s. ", event.MigrationGuideURL)
}
prompt += "Run tests and summarize any manual follow-up."
result, err := sdk.Run(ctx, prompt)
Java
String prompt = "Update " + event.dependency() + " to " + event.version() + ". ";
if (event.migrationGuideUrl() != null) {
prompt += "Follow the migration guide: " + event.migrationGuideUrl() + ". ";
}
prompt += "Run tests and summarize any manual follow-up.";
AgentResult result = sdk.run(prompt);
Swift
var prompt = "Update \(event.dependency) to \(event.version). "
if let guide = event.migrationGuideURL {
prompt += "Follow the migration guide: \(guide). "
}
prompt += "Run tests and summarize any manual follow-up."
let result = try await sdk.run(prompt)
curl
curl -X POST "$REFACTOR_WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d '{"dependency":"react","version":"19.0.0","migration_guide_url":"https://react.dev/blog"}'Step 2: run the refactor agent
Run the Autohand CLI in command mode with a focused prompt and limits on requests and time.
autohand -p "$prompt" --yes --max-requests 40 --max-duration 1800 --json local > result.jsonStep 3: commit and open a pull request
git checkout -b "autohand/migrate-$dependency-$version"
git add .
git commit -m "chore: migrate $dependency to $version"
git push origin "autohand/migrate-$dependency-$version"
gh pr create --title 'Migrate $dependency to $version' --body 'Autohand agent run'Next steps
Add a human approval step before merging, or run the workflow on a schedule to catch missed updates.