package rules

import "golang.org/x/build/cmd/gerritbot/internal/rules"

Package rules specifies a simple set of rules for checking GitHub PRs or Gerrit CLs for certain common mistakes, like no package in the first line of the commit message or having long lines in the commit message body.

The rules attempt to be graceful when encountering a new or unknown repo. A new repo usually does not require updating anything here, especially if the repo primarily contains Go code and follows typical patterns like using the main Go issue tracker. If a new repo is unusual in some way that causes a notable problem, some simple options include:

A rule is primarily defined via a function that takes a Change (CL or PR) as input and reports zero or 1 findings, which is just a string (usually 1-2 short sentences). A rule can also optionally return a note, which might be auxiliary advice such as how to edit the commit message.

The FormatResults function renders the results into markdown. Each finding is shown to the user, currently in a numbered list of findings. The auxiliary notes are deduplicated, so that for example a set of rules looking for problems in a commit message can all return the same editing advice but that advice is then only shown to the user only once.

For example, for a commit message with a first line of:

fmt: Improve foo and bar.

That would currently trigger two rules, with an example formatted result of the following (including the deduplicated advice at the end):

Possible problems detected:
  1. The first word in the commit title after the package should be a
     lowercase English word (usually a verb).
  2. The commit title should not end with a period.

 To edit the commit message, see instructions [here](...). For guidance on commit
 messages for the Go project, see [here](...).

Rules currently err on the side of simplicity and avoiding false positives. It is intended to be straightforward to add a new rule.

Rules can be arranged to trigger completely independently of one another, or alternatively a set of rules can optionally be arranged in groups that form a precedence based on order within the group, where at most one rule from that group will trigger. This can be helpful in cases like:

Index

Functions

func FormatResults

func FormatResults(results []Result) string

FormatResults returns a string ready to be placed in a CL comment, formatted as simple markdown.

Types

type Change

type Change struct {
	// Repo is the repository as reported by Gerrit (e.g., "go", "tools", "vscode-go", "website").
	Repo string
	// Title is the commit message first line.
	Title string
	// Body is the commit message body (skipping the title on the first line and the blank second line,
	// and without the footers).
	Body string
}

Change represents a Gerrit CL and/or GitHub PR that we want to check rules against.

func ParseCommitMessage

func ParseCommitMessage(repo string, text string) (Change, error)

type Result

type Result struct {
	Name    string
	Finding string
	Note    string
}

Result contains the result of a single rule check against a Change.

func Check

func Check(change Change) (results []Result)

Check runs the defined rules against one Change.

Source Files

rules.go run.go

Version
v0.0.0-20250421191922-3619c213cff3 (latest)
Published
Apr 21, 2025
Platform
linux/amd64
Imports
5 packages
Last checked
2 months ago

Tools for package owners.