Label critical changes with semantic code annotations

The hasAnnotation built-in can be used to access code annotations in Revy configurations. A key differentiator is that Reviewpad can understand the context of changes allowing for powerful checks.

To use the annotation in Revy files, you need to annotate functions of interest with the comment reviewpad-an: critical. For example, consider the following Go function:

// reviewpad-an: critical
func quicksort(a []int) []int {
    if len(a) < 2 {
        return a
    }

    left, right := 0, len(a)-1

    pivot := rand.Int() % len(a)

    a[pivot], a[right] = a[right], a[pivot]

    for i := range a {
        if a[i] < a[right] {
            a[left], a[i] = a[i], a[left]
            left++
        }
    }

    a[left], a[right] = a[right], a[left]

    quicksort(a[:left])
    quicksort(a[left+1:])

    return a
}

From now on, we can automatically label PRs that modify the content of this function:

apiVersion: reviewpad.com/v0.0.4

labels:
  critical:
    description: critical code

rules:
   changesCriticalCode:
    description: Patch involves a critical method or function
    kind: patch
    spec: $hasAnnotation("critical")

protectionGates:
  - name: critical
    description: Modifications of critical code require careful review
    patchRules:
      - rule: changesCriticalCode
    actions:
      - $addLabel("critical")

As an example, check out reviewpad/action-demo/#10.

In a future version, we plan to allow such specification in configuration files, for developers that do not want to add such annotations to the source code. Effectively, Reviewpad allows to implement a much more advanced version of CODEOWNERS.


Did this page help you?