Sub topic 6
Choosing pull request activity types
This chapter assumes you can write a basic on: pull_request trigger and understand branch filters from the previous chapters. Here you will learn how activity types change when a pull request workflow runs.
What activity types are
A pull request is not one event. GitHub emits a pull_request event each time something happens to it, and each kind of change is an activity type. The types key lets you choose which ones start a run.
If you omit types, GitHub uses a default set of three: opened, synchronize and reopened.
opened: the pull request is created.synchronize: new commits are pushed to the pull request's head branch.reopened: a closed pull request is reopened.
These three cover most CI needs. Every new push to the branch reruns your tests.
Other useful types
Other types exist for specific cases:
| Type | Fires when |
|---|---|
| `ready_for_review` | A draft pull request is marked ready |
| `converted_to_draft` | A pull request is turned back into a draft |
| `edited` | The title, body or base branch changes |
| `labeled` / `unlabeled` | A label is added or removed |
| `closed` | The pull request is closed or merged |
Setting types replaces the default list. It does not add to it. If you write types: [labeled], the workflow no longer runs on new commits.
on:
pull_request:
branches: [main]
types: [opened, synchronize, reopened, ready_for_review]This is a common pattern. It keeps the defaults and adds ready_for_review, so a draft that was skipped earlier gets tested once it is ready.
Skipping drafts
Draft pull requests still trigger opened and synchronize. To avoid spending runner minutes on them, check the draft flag in the event payload.
name: PR checks
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
jobs:
test:
if: github.event.pull_request.draft == false
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: echo "Testing PR #${{ github.event.pull_request.number }}"The job is skipped for drafts. When someone marks the pull request ready, ready_for_review fires and the job runs.
Reacting to merges
There is no "merged" type. A merge appears as closed, so you must check the payload.
on:
pull_request:
types: [closed]
jobs:
cleanup:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
- run: echo "Merged into ${{ github.event.pull_request.base.ref }}"Common errors
- Overriding the defaults by accident. Listing only
labeledorready_for_reviewsilences runs on new commits. - Expecting
editedon pushes. Pushing commits firessynchronize, notedited. - Treating
closedas merged. Without themergedcheck, the job also runs for pull requests closed without merging. - Forgetting that
branchesfilters the base branch. It matches the branch the pull request targets, not the branch it comes from.
Exercise
A workflow uses on: pull_request: types: [ready_for_review]. A developer opens a non-draft pull request and then pushes two more commits. How many runs start, and what would you change so each push is tested?
Show answer
No runs start. Setting types replaces the default list, and ready_for_review only fires when a draft is marked ready. Add the defaults back: types: [opened, synchronize, reopened, ready_for_review].