|
| 1 | +--- |
| 2 | +title: Hooks |
| 3 | +description: Run custom commands when tasks are merged, pushed, or completed |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | +--- |
| 7 | + |
| 8 | +import { Aside, CardGrid, LinkCard } from '@astrojs/starlight/components'; |
| 9 | + |
| 10 | +Hooks let you run custom shell commands at specific points in the task lifecycle. Use them to trigger notifications, update dashboards, run CI pipelines, or integrate with other tools. |
| 11 | + |
| 12 | +## Available Hooks |
| 13 | + |
| 14 | +| Hook | Trigger | |
| 15 | +|------|---------| |
| 16 | +| `onMerge` | Runs after a task is successfully merged via `rover merge` | |
| 17 | +| `onPush` | Runs after a task branch is pushed via `rover push` | |
| 18 | +| `onComplete` | Runs when a task finishes (success or failure), detected via `rover list` | |
| 19 | + |
| 20 | +## Configuration |
| 21 | + |
| 22 | +Add hooks to your `rover.json` file: |
| 23 | + |
| 24 | +```jsonc |
| 25 | +{ |
| 26 | + "version": "1.2", |
| 27 | + "languages": ["typescript"], |
| 28 | + "packageManagers": ["npm"], |
| 29 | + "hooks": { |
| 30 | + "onComplete": ["./scripts/on-complete.sh"], |
| 31 | + "onMerge": ["./scripts/on-merge.sh"], |
| 32 | + "onPush": ["echo 'Task $ROVER_TASK_ID pushed'"] |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Each hook accepts an array of shell commands. Commands are executed sequentially in the order specified. |
| 38 | + |
| 39 | +## Environment Variables |
| 40 | + |
| 41 | +Rover passes task information to hooks via environment variables: |
| 42 | + |
| 43 | +| Variable | Description | Available In | |
| 44 | +|----------|-------------|--------------| |
| 45 | +| `ROVER_TASK_ID` | The task ID | All hooks | |
| 46 | +| `ROVER_TASK_BRANCH` | The task branch name | All hooks | |
| 47 | +| `ROVER_TASK_TITLE` | The task title | All hooks | |
| 48 | +| `ROVER_TASK_STATUS` | Task status: `completed` or `failed` | `onComplete` only | |
| 49 | + |
| 50 | +## Example Hook Script |
| 51 | + |
| 52 | +Create a script that notifies your team when a task completes: |
| 53 | + |
| 54 | +```bash |
| 55 | +#!/bin/bash |
| 56 | +# scripts/on-complete.sh |
| 57 | + |
| 58 | +echo "Task $ROVER_TASK_ID ($ROVER_TASK_TITLE) finished with status: $ROVER_TASK_STATUS" |
| 59 | + |
| 60 | +# Send a Slack notification |
| 61 | +if [ "$ROVER_TASK_STATUS" = "completed" ]; then |
| 62 | + curl -X POST -H 'Content-type: application/json' \ |
| 63 | + --data "{\"text\":\"Task #$ROVER_TASK_ID completed: $ROVER_TASK_TITLE\"}" \ |
| 64 | + "$SLACK_WEBHOOK_URL" |
| 65 | +fi |
| 66 | +``` |
| 67 | + |
| 68 | +Make sure to set execute permissions: |
| 69 | + |
| 70 | +```sh |
| 71 | +chmod +x scripts/on-complete.sh |
| 72 | +``` |
| 73 | + |
| 74 | +## Hook Behavior |
| 75 | + |
| 76 | +### Execution Context |
| 77 | + |
| 78 | +- Hooks run in the **project directory** (where `rover.json` is located) |
| 79 | +- Commands execute via the shell (`sh -c`), supporting pipes and complex commands |
| 80 | +- Hooks inherit the current environment plus `ROVER_*` variables |
| 81 | + |
| 82 | +### Error Handling |
| 83 | + |
| 84 | +- Hook failures are logged as **warnings** but don't interrupt the main operation |
| 85 | +- If a hook fails, subsequent hooks in the array still execute |
| 86 | +- The primary command (`merge`, `push`, `list`) completes regardless of hook failures |
| 87 | + |
| 88 | +<Aside type="tip">Hooks are designed to be non-blocking. A failing notification script won't prevent your merge from completing.</Aside> |
| 89 | + |
| 90 | +### Deduplication |
| 91 | + |
| 92 | +The `onComplete` hook only triggers on **new** status transitions. If a task was already completed and you run `rover list` again, the hook won't re-execute. |
| 93 | + |
| 94 | +## Use Cases |
| 95 | + |
| 96 | +### Notify on Completion |
| 97 | + |
| 98 | +Send notifications when tasks finish: |
| 99 | + |
| 100 | +```jsonc |
| 101 | +{ |
| 102 | + "hooks": { |
| 103 | + "onComplete": [ |
| 104 | + "curl -X POST https://api.example.com/webhook -d '{\"task\": \"$ROVER_TASK_ID\", \"status\": \"$ROVER_TASK_STATUS\"}'" |
| 105 | + ] |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### Trigger CI on Push |
| 111 | + |
| 112 | +Start a CI pipeline when a task branch is pushed: |
| 113 | + |
| 114 | +```jsonc |
| 115 | +{ |
| 116 | + "hooks": { |
| 117 | + "onPush": [ |
| 118 | + "gh workflow run ci.yml --ref $ROVER_TASK_BRANCH" |
| 119 | + ] |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +### Update Issue Tracker on Merge |
| 125 | + |
| 126 | +Close related issues when tasks are merged: |
| 127 | + |
| 128 | +```jsonc |
| 129 | +{ |
| 130 | + "hooks": { |
| 131 | + "onMerge": [ |
| 132 | + "./scripts/close-issue.sh" |
| 133 | + ] |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### Multiple Commands |
| 139 | + |
| 140 | +Chain multiple commands for a single hook: |
| 141 | + |
| 142 | +```jsonc |
| 143 | +{ |
| 144 | + "hooks": { |
| 145 | + "onComplete": [ |
| 146 | + "echo 'Task $ROVER_TASK_ID completed'", |
| 147 | + "./scripts/notify-slack.sh", |
| 148 | + "./scripts/update-dashboard.sh" |
| 149 | + ] |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## Next Steps |
| 155 | + |
| 156 | +<CardGrid> |
| 157 | + <LinkCard title="Project Configuration" href="/rover/config/project-config" description="Configure project-wide settings" /> |
| 158 | + <LinkCard title="User Settings" href="/rover/config/user-settings" description="Configure user-specific preferences" /> |
| 159 | +</CardGrid> |
0 commit comments