File: pr-should-include-tests

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (112 lines) | stat: -rwxr-xr-x 3,862 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
#
# Intended for use in CI: check git commits, barf if no tests added.
#
ME=$(basename $0)

# Github label which allows overriding this check
OVERRIDE_LABEL="No New Tests"

# Docs-only changes are excused
if [[ "${CIRRUS_CHANGE_TITLE}" =~ CI:DOCS ]]; then
    exit 0
fi

# HEAD should be good enough, but the CIRRUS envariable allows us to test
head=${CIRRUS_CHANGE_IN_REPO:-HEAD}
# Base of this PR. Here we absolutely rely on cirrus.
base=$(git merge-base ${GITVALIDATE_EPOCH:-main} $head)

# Sanity check:
if [[ -z "$base" ]]; then
    echo "$(basename $0): internal error: could not determine merge-base"
    echo "   head                  = $head"
    echo "   CIRRUS_CHANGE_IN_REPO = $CIRRUS_CHANGE_IN_REPO"
    echo "   GITVALIDATE_EPOCH     = $GITVALIDATE_EPOCH"
    exit 1
fi

# This gives us a list of files touched in all commits, e.g.
#    A    foo.c
#    M    bar.c
# We look for Added or Modified (not Deleted!) files under 'tests'.
# --no-renames ensures that renamed tests show up as 'A'dded.
if git diff --name-status --no-renames $base $head | grep -E -q '^[AM]\s+(tests/|.*_test\.go)'; then
    exit 0
fi

# Nothing changed under test subdirectory.
#
# This is OK if the only files being touched are "safe" ones.
filtered_changes=$(git diff --name-status $base $head   |
                       awk '{print $2}'                 |
                       grep -F -vx .cirrus.yml          |
                       grep -F -vx .packit.yaml         |
                       grep -F -vx .gitignore           |
                       grep -F -vx changelog.txt        |
                       grep -F -vx go.mod               |
                       grep -F -vx go.sum               |
                       grep -F -vx buildah.spec.rpkg    |
                       grep -F -vx .golangci.yml        |
                       grep -F -vx Makefile             |
                       grep -E -v  '^[^/]+\.md$'        |
                       grep -E -v  '^\.github/'         |
                       grep -E -v  '^contrib/'          |
                       grep -E -v  '^docs/'             |
                       grep -E -v  '^hack/'             |
                       grep -E -v  '^nix/'              |
                       grep -E -v  '^vendor/')
if [[ -z "$filtered_changes" ]]; then
    exit 0
fi

# Nope. Only allow if the github 'no-tests-needed' label is set
if [[ -z "$CIRRUS_PR" ]]; then
    echo "$ME: cannot query github: \$CIRRUS_PR is undefined" >&2
    exit 1
fi
if [[ -z "$CIRRUS_REPO_CLONE_TOKEN" ]]; then
    echo "$ME: cannot query github: \$CIRRUS_REPO_CLONE_TOKEN is undefined" >&2
    exit 1
fi

query="{
  \"query\": \"query {
  repository(owner: \\\"containers\\\", name: \\\"buildah\\\") {
    pullRequest(number: $CIRRUS_PR) {
      labels(first: 100) {
        nodes {
          name
        }
      }
    }
  }
}\"
}"

result=$(curl -s -H "Authorization: bearer $CIRRUS_REPO_CLONE_TOKEN" -H "Accept: application/vnd.github.antiope-preview+json" -H "Content-Type: application/json" -X POST --data @- https://api.github.com/graphql <<<"$query")

labels=$(jq -r '.data.repository.pullRequest.labels.nodes[].name' <<<"$result")

if grep -F -x -q "$OVERRIDE_LABEL" <<<"$labels"; then
    # PR has the label set
    exit 0
fi

cat <<EOF
$ME: PR does not include changes in the 'tests' directory

Please write a regression test for what you're fixing. Even if it
seems trivial or obvious, try to add a test that will prevent
regressions.

If your change is minor, feel free to piggyback on already-written
tests, possibly just adding a small step to a similar existing test.
Every second counts in CI.

If your commit really, truly does not need tests, you can proceed
by asking a repo maintainer to set the '$OVERRIDE_LABEL' github label.
This will only be done when there's no reasonable alternative.
EOF

exit 1