|
| 1 | +<!-- page-journey: all --> |
| 2 | +<!-- page-adventure: advanced --> |
| 3 | +# Build Your First Event-Driven Workflow: PR Auto-Reviewer |
| 4 | + |
| 5 | +_You've built a scheduled workflow. Now build one that fires when something happens — and posts a useful review the moment a pull request opens._ |
| 6 | + |
| 7 | +## 🎯 What You'll Do |
| 8 | + |
| 9 | +Create a new agentic workflow triggered by the `pull_request` event. When a contributor opens or updates a PR, your workflow will read the changed files and the PR description, then post an automated review comment summarising what changed and flagging anything worth a second look. |
| 10 | + |
| 11 | +By the end you will have a working PR reviewer workflow and understand how event-driven triggers differ from scheduled ones. |
| 12 | + |
| 13 | +## 📋 Before You Start |
| 14 | + |
| 15 | +- You have a working `daily-report-status` workflow from [Test and Improve Your Workflow](12-test-and-iterate.md). |
| 16 | +- You understand the two-file structure (`.md` source + `.lock.yml`) from earlier steps. |
| 17 | + |
| 18 | +## Why Event-Driven Triggers? |
| 19 | + |
| 20 | +Your daily-status workflow runs on a schedule — it wakes up on its own, checks what happened, and reports. A PR reviewer workflow is different: it runs the moment a developer opens or updates a pull request, with the full PR context handed to it automatically. |
| 21 | + |
| 22 | +This is the `pull_request` trigger: |
| 23 | + |
| 24 | +```yaml |
| 25 | +on: |
| 26 | + pull_request: |
| 27 | + types: [opened, synchronize] |
| 28 | +``` |
| 29 | +
|
| 30 | +`opened` fires when a PR is first created. `synchronize` fires every time a new commit is pushed to the PR branch. Together they cover the full PR lifecycle without any manual intervention. |
| 31 | + |
| 32 | +Event-driven workflows are the foundation of most real-world agentic review automation. The same pattern powers auto-labellers, summary generators, and quality checklists — three of which you can explore in the side quests at the end of this step. |
| 33 | + |
| 34 | +## Create the Workflow File |
| 35 | + |
| 36 | +In your practice repository, create `.github/workflows/pr-reviewer.md`: |
| 37 | + |
| 38 | +```markdown |
| 39 | +--- |
| 40 | +name: PR Auto-Reviewer |
| 41 | +on: |
| 42 | + pull_request: |
| 43 | + types: [opened, synchronize] |
| 44 | +permissions: |
| 45 | + pull-requests: write |
| 46 | + contents: read |
| 47 | +safe-outputs: |
| 48 | + create-issue-comment: |
| 49 | + limit: 1 |
| 50 | +--- |
| 51 | +
|
| 52 | +You are a helpful PR reviewer. When a pull request is opened or updated: |
| 53 | +
|
| 54 | +1. Read the list of changed files and the PR title and description. |
| 55 | +2. Write a short summary (3–5 sentences) of what the PR does. |
| 56 | +3. List up to three things a reviewer should pay close attention to, based on the file names and PR description alone. |
| 57 | +4. Post the summary and checklist as a single comment on the pull request. |
| 58 | +
|
| 59 | +Keep the tone constructive and specific. Do not speculate about code you have not seen. |
| 60 | +``` |
| 61 | + |
| 62 | +A few things to notice in this frontmatter: |
| 63 | + |
| 64 | +- `permissions: pull-requests: write` lets the agent post a comment on the PR. |
| 65 | +- `safe-outputs: create-issue-comment: limit: 1` caps the workflow at one comment per run, preventing spam if the workflow is triggered repeatedly. |
| 66 | +- The agent brief uses only information available in the trigger context (changed file paths, PR title, PR description) — it does not need to read raw file contents to produce a useful first-pass review. |
| 67 | + |
| 68 | +## Compile and Push |
| 69 | + |
| 70 | +From your repository root, compile the workflow: |
| 71 | + |
| 72 | +```bash |
| 73 | +gh aw compile |
| 74 | +``` |
| 75 | + |
| 76 | +Then commit both files: |
| 77 | + |
| 78 | +```bash |
| 79 | +git add .github/workflows/pr-reviewer.md .github/workflows/pr-reviewer.lock.yml |
| 80 | +git commit -m "feat: add PR auto-reviewer workflow" |
| 81 | +git push |
| 82 | +``` |
| 83 | + |
| 84 | +> [!TIP] |
| 85 | +> If you want to watch the compiler update the lock file every time you save, run `gh aw compile --watch` instead and keep it running in a background terminal while you edit. |
| 86 | + |
| 87 | +## Test It by Opening a PR |
| 88 | + |
| 89 | +Create a small branch with a trivial change — for example, add a comment to any file — and open a pull request against your default branch. |
| 90 | + |
| 91 | +The workflow fires automatically within a few seconds of the PR being created. To watch it: |
| 92 | + |
| 93 | +1. Go to the **Actions** tab of your repository. |
| 94 | +2. Find the **PR Auto-Reviewer** run next to your pull request. |
| 95 | +3. Open the run and watch the agent step process the PR context. |
| 96 | +4. Navigate back to the pull request and check the **Conversation** tab — your automated review comment should appear there. |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## Inspect the Agent's Reasoning |
| 101 | + |
| 102 | +Open the run log and look for the agent's `[plan]` and `[tool]` lines. You should see the agent: |
| 103 | + |
| 104 | +1. Reading the PR metadata (title, description, file list). |
| 105 | +2. Planning its summary based on available context. |
| 106 | +3. Calling the `create-issue-comment` tool to post the result. |
| 107 | + |
| 108 | +If the agent posted a comment but it feels generic, the brief is working but may need tightening. Try adding a sentence like: |
| 109 | + |
| 110 | +```text |
| 111 | +When the PR only touches test files, note that explicitly and skip the "things to watch" list. |
| 112 | +``` |
| 113 | + |
| 114 | +Then recompile, push, and update the PR branch to trigger another run. |
| 115 | + |
| 116 | +## ✅ Checkpoint |
| 117 | + |
| 118 | +- [ ] I created `.github/workflows/pr-reviewer.md` with a `pull_request` trigger |
| 119 | +- [ ] `gh aw compile` completed without errors and `.lock.yml` is committed and pushed |
| 120 | +- [ ] I opened a test pull request and the workflow triggered automatically |
| 121 | +- [ ] The workflow posted exactly one comment on my pull request |
| 122 | +- [ ] I can explain why `safe-outputs: create-issue-comment: limit: 1` matters for an event-driven workflow |
| 123 | + |
| 124 | +<!-- journey: all --> |
| 125 | +**Next:** [What's Next? Keep Exploring](14-next-steps.md) |
| 126 | +<!-- /journey --> |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Go Further: PR Reviewer Patterns |
| 131 | + |
| 132 | +Ready to extend this workflow? These side quests each cover a popular PR reviewer pattern: |
| 133 | + |
| 134 | +- [Pattern: Auto-Label PRs by Content](side-quest-13-01-pr-labeler-pattern.md) — apply labels based on which files changed. |
| 135 | +- [Pattern: Generate a PR Summary Comment](side-quest-13-02-pr-summary-pattern.md) — post a structured summary that PR authors can use as a release note draft. |
| 136 | +- [Pattern: PR Review Checklist](side-quest-13-03-pr-checklist-pattern.md) — check PRs against a quality checklist and post results. |
0 commit comments