Chapter 6: Code Review and CI
Every change to the system passes through two gates before it merges. The first is mechanical: a CI workflow that runs on every push and pull request, and commit messages a machine can read. The second is human: a reviewer who reads the change against the system it modifies and finds the defects no test anticipated. This chapter builds both.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Pull request (PR): a proposed change, made of a branch, a description, and a diff against a base branch. Read more: 6.2 Code review
- Branch protection: a hosting rule that blocks merges into a branch until named CI jobs pass, turning CI into a gate. Read more: 6.1 The repository and its CI gate
- commit-msg hook: an executable git runs with the path of the message file before creating a commit; a nonzero exit aborts the commit. Read more: 6.1 The repository and its CI gate
- Conventional Commit: a commit whose header matches
type(scope)!: description, so tools can derive version bumps and changelogs. Read more: 6.1 The repository and its CI gate - Data race: two accesses to the same memory from different goroutines, at least one a write, with no happens-before order between them. Read more: 6.2 Code review
- goroutine: a function running concurrently, scheduled by the Go runtime. Read more: 1.6 Go: packages, interfaces, goroutines, channels, context, net/http, testing in Programming and Algorithms
- Continuous integration (CI): a service that runs a workflow of jobs and steps on every push and pull request, passing or failing by exit status. Read more: 6.1 The repository and its CI gate
It starts with the mechanical gate, because it should exist from the very first commit. CI is a function from a commit to pass or fail, and branch protection turns that function into a gate. The rule it enforces lives in one place: the hook you run locally is the same file CI runs, so the two cannot disagree. Conventional commit headers make the history machine-readable, which is what releases are later computed from. Keep the gate strict from the start: loosening it later is easy, while tightening it means rewriting history.
The chapter then turns to the human gate. Once the system bills tenants and pages on-call engineers, a green test suite is not enough, because tests catch only what someone thought to test. The second section puts you in the reviewer’s seat for a pull request that passes its own test and still ships five bugs.
Learning objectives
- Build a CI workflow and a tracked commit-msg hook that enforce Conventional Commits on every new commit.
- Explain how branch protection, exit codes, and full-history checkouts make CI a reliable gate.
- Review a pull request with the reviewer’s loop and write findings that carry evidence and a severity tied to consequence.
- Recognize data races and other state-dependent defects in Go code, and avoid false alarms on correct code.
Before you begin
Section titled “Before you begin”You should be comfortable with processes, exit codes, and git, as covered in Shell, git, make, processes in Programming and Algorithms. The code review section also draws on Go for goroutines and the race detector, and on Mutation testing in depth from earlier in this book.
In this chapter
Section titled “In this chapter”- The repository and its CI gate: a Conventional Commits hook and a GitHub Actions workflow that lints every new commit and runs the tests, with the shallow-clone and first-push edge cases handled.
- Code review: the reviewer’s loop, the shape and severity of a finding, and a worked review of a rate-limiter pull request that finds five seeded defects without raising false alarms.
Further reading
Section titled “Further reading”- Software Engineering at Google, ch. 9, “Code Review”, and ch. 23, “Continuous Integration”, abseil.io/resources/swe-book (free).
- Conventional Commits 1.0.0, conventionalcommits.org (free).