github.com/containers/podman/v4@v4.9.4/contrib/cirrus/check_go_changes.sh (about)

     1  #!/bin/bash
     2  
     3  set -eo pipefail
     4  
     5  # This script is intended to confirm new go code conforms to certain
     6  # conventions and/or does not introduce use of old/deprecated packages
     7  # or functions.  It needs to run in the Cirrus CI environment, on behalf
     8  # of PRs, via runner.sh.  This ensures a consistent and predictable
     9  # environment not easily reproduced by a `Makefile`.
    10  
    11  # shellcheck source=contrib/cirrus/lib.sh
    12  source $(dirname $0)/lib.sh
    13  
    14  check_msg() {
    15      msg "#####"  # Cirrus-CI logs automatically squash empty lines
    16      msg "##### $1"  # Complains if $1 is empty
    17  }
    18  
    19  # First arg is check description, second is regex to search $diffs for.
    20  check_diffs() {
    21      local check regex
    22      check="$1"
    23      regex="$2"
    24      check_msg "Confirming changes have no $check"
    25      req_env_vars check regex diffs
    26      if grep -E -q "$regex"<<<"$diffs"; then
    27          # Show 5 context lines before/after as compromise for script simplicity
    28          die "Found $check:
    29  $(grep -E -B 5 -A 5 "$regex"<<<"$diffs")"
    30      fi
    31  }
    32  
    33  # Defined by Cirrus-CI
    34  # shellcheck disable=SC2154
    35  if [[ "$CIRRUS_BRANCH" =~ pull ]]; then
    36      for var in CIRRUS_CHANGE_IN_REPO CIRRUS_PR DEST_BRANCH; do
    37          if [[ -z "${!var}" ]]; then
    38              warn "Skipping: Golang code checks require non-empty '\$$var'"
    39              exit 0
    40          fi
    41      done
    42  else
    43      warn "Skipping: Golang code checks in tag and branch contexts"
    44      exit 0
    45  fi
    46  
    47  # Defined by/in Cirrus-CI config.
    48  # shellcheck disable=SC2154
    49  base=$(git merge-base $DEST_BRANCH $CIRRUS_CHANGE_IN_REPO)
    50  diffs=$(git diff $base $CIRRUS_CHANGE_IN_REPO -- '*.go' ':^vendor/' ':^test/tools/vendor/')
    51  
    52  if [[ -z "$diffs" ]]; then
    53      check_msg "There are no golang diffs to check between $base...$CIRRUS_CHANGE_IN_REPO"
    54      exit 0
    55  fi
    56  
    57  check_diffs \
    58      "use of deprecated ioutil vs recommended io or os packages." \
    59      "^(\\+[^#]+io/ioutil)|(\\+.+ioutil\\..+)"
    60  
    61  check_diffs \
    62      "use of os.IsNotExist(err) vs recommended errors.Is(err, fs.ErrNotExist)" \
    63      "^\\+[^#]*os\\.IsNotExist\\("