Skip to content

Commit

Permalink
bash script refactorings
Browse files Browse the repository at this point in the history
- format with shfmt
- safer curl args
- Use `jq -r` to remove quotes
- consistent use of curlies around variable names
  • Loading branch information
focusaurus authored Jul 24, 2019
1 parent 0adaa05 commit 0eb643c
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions .circleci/eslint-changed-files.sh
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
#!/bin/bash
#!/usr/bin/env bash

set -eou pipefail
# Please Use Google Shell Style: https://google.github.io/styleguide/shell.xml

CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}" # Determine PR number from pull request link
if [[ -v CIRCLE_PR_NUMBER ]] && [ -n ${CIRCLE_PR_NUMBER} ]; then
url="https://api.github.com/repos/${DOCKER_REPOSITORY}/pulls/$CIRCLE_PR_NUMBER" # Get PR from github API
TARGET_BRANCH=$(curl "$url" | jq '.base.ref' | tr -d '"') # Determine target/base branch from API response
fi
# ---- Start unofficial bash strict mode boilerplate
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -o errexit # always exit on error
set -o errtrace # trap errors in functions as well
set -o pipefail # don't ignore exit codes when piping output
set -o posix # more strict failures in subshells
# set -x # enable debugging

if [ -z "${TARGET_BRANCH}" ] || [ ${TARGET_BRANCH} == "null" ]; then
echo "Not a PR. Skipping eslint-changed-files."
exit 0
fi
IFS=$'\n\t'
# ---- End unofficial bash strict mode boilerplate

echo "Getting list of changed files..."
CHANGED_FILES=$(git diff --name-only "origin/${TARGET_BRANCH}"..$CIRCLE_BRANCH -- '*.js' | tr '\n' ' ')
# Determine PR number from pull request link
CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"
if [[ -n "${CIRCLE_PR_NUMBER}" ]]; then
# Get PR from github API
url="https://api.github.com/repos/${DOCKER_REPOSITORY}/pulls/${CIRCLE_PR_NUMBER}"
# Determine target/base branch from API response
TARGET_BRANCH=$(curl --silent --location --fail --show-error "${url}" | jq -r '.base.ref')
fi

# If we have changed files
if [ -n "${CHANGED_FILES}" ]; then
echo "Files have been changed. Run eslint against these files."
npm run lint:warnings $CHANGED_FILES
else
echo "We have no changed files, don't run eslint-changed-files"
fi
if [[ -z "${TARGET_BRANCH}" ]] || [[ ${TARGET_BRANCH} == "null" ]]; then
echo "Not a PR. Skipping eslint-changed-files."
exit 0
fi

echo "Getting list of changed files..."
CHANGED_FILES=$(git diff --name-only "origin/${TARGET_BRANCH}"..$CIRCLE_BRANCH -- '*.js')

# If we have changed files
if [[ -n "${CHANGED_FILES}" ]]; then
echo "Files have been changed. Run eslint against these files."
echo "${CHANGED_FILES}" | xargs npm run lint:warnings
else
echo "We have no changed files, don't run eslint-changed-files"
fi

0 comments on commit 0eb643c

Please sign in to comment.