aboutsummaryrefslogtreecommitdiff
path: root/contrib/cirrus/check_go_changes.sh
blob: aa8f4ccf46583bc07b84e3d05b34f0fe773c5494 (plain)
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
#!/bin/bash

set -eo pipefail

# This script is intended to confirm new go code conforms to certain
# conventions and/or does not introduce use of old/deprecated packages
# or functions.  It needs to run in the Cirrus CI environment, on behalf
# of PRs, via runner.sh.  This ensures a consistent and predictable
# environment not easily reproduced by a `Makefile`.

# shellcheck source=contrib/cirrus/lib.sh
source $(dirname $0)/lib.sh

check_msg() {
    msg "#####"  # Cirrus-CI logs automatically squash empty lines
    msg "##### $1"  # Complains if $1 is empty
}

# First arg is check description, second is regex to search $diffs for.
check_diffs() {
    local check regex
    check="$1"
    regex="$2"
    check_msg "Confirming changes have no $check"
    req_env_vars check regex diffs
    if egrep -q "$regex"<<<"$diffs"; then
        # Show 5 context lines before/after as compromise for script simplicity
        die "Found $check:
$(egrep -B 5 -A 5 "$regex"<<<"$diffs")"
    fi
}

# Defined by Cirrus-CI
# shellcheck disable=SC2154
if [[ "$CIRRUS_BRANCH" =~ pull ]]; then
    for var in CIRRUS_CHANGE_IN_REPO CIRRUS_PR DEST_BRANCH; do
        if [[ -z "${!var}" ]]; then
            warn "Skipping: Golang code checks require non-empty '\$$var'"
            exit 0
        fi
    done
else
    warn "Skipping: Golang code checks in tag and branch contexts"
    exit 0
fi

# Defined by/in Cirrus-CI config.
# shellcheck disable=SC2154
base=$(git merge-base $DEST_BRANCH $CIRRUS_CHANGE_IN_REPO)
diffs=$(git diff $base $CIRRUS_CHANGE_IN_REPO -- '*.go' ':^vendor/')

if [[ -z "$diffs" ]]; then
    check_msg "There are no golang diffs to check between $base...$CIRRUS_CHANGE_IN_REPO"
    exit 0
fi

check_diffs \
    "use of deprecated ioutil vs recommended io or os packages." \
    "^(\\+[^#]+io/ioutil)|(\\+.+ioutil\\..+)"

check_diffs \
    "use of os.IsNotExists(err) vs recommended errors.Is(err, os.ErrNotExist)" \
    "^\\+[^#]*os\\.IsNotExists\\("