Skip to content

Commit

Permalink
adding script files
Browse files Browse the repository at this point in the history
  • Loading branch information
dmekala-va committed Jul 9, 2024
1 parent 738ad50 commit 3a4bd1f
Show file tree
Hide file tree
Showing 3 changed files with 299 additions and 0 deletions.
145 changes: 145 additions & 0 deletions .github/workflows/gh-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env bash
# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail
# Turn on traces, useful while debugging but commented out by default
# set -o xtrace

usage() {
echo "Usage: $(basename "$0") [options]"
echo "Options:
-r <repository>
-c <commit_hash>
-x <build projects to exclude> : separated by comma e.g. check1,check2
-o <repository owner> : defaults to department-of-veterans-affairs" 1>&2
}

err() {
printf "$*" >&2
}

XBUILDS=""
OWNER="department-of-veterans-affairs"

# Verifies arguments
while getopts "hc:r:x:o:" o; do
case "${o}" in
c)
COMMIT_HASH="${OPTARG:-}"
;;
r)
REPO="${OPTARG:-}"
;;
x)
XBUILDS="${OPTARG}"
;;
o)
OWNER="${OPTARG}"
;;
h)
usage
exit
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
*)
echo "Illegal option: ${OPTARG:-}"$'\n'
usage
exit 1
;;
esac
done

shift $((OPTIND-1))


# GH CLI requires authorization prior to API calls
if [[ -z "${GITHUB_TOKEN:-}" ]] ; then
err "Please set environment variable for GITHUB_TOKEN."
exit 1
fi

# Required arguments
if [[ -z "${COMMIT_HASH:-}" ]] || [[ -z "${REPO:-}" ]]; then
usage
exit 1
fi

# Check for erroneous arguments
if [[ $# -ne 0 ]]; then
err "Illegal number of arguments.\n"
usage
exit 1
fi

build_filter () {
# Builds CI filter from passing multiple jobs to xbuilds
filter=""
OLDIFS=$IFS
IFS=','
for buildjob in $XBUILDS; do
# Not sure if we want to add this here or not. Problem this solves is matching on multiple checks with same name.
# Codebuild anchors checks with () which gives us a exact match. We could instruct that as in put instead of adding the characters here.
# ie. gh-status.sh -x (ci-1),(ci-2)
# Triple \ needed for proper escape
#filter="${filter} select(.context | test(\".*\\\(${buildjob}\\\).*\") | not ) |"
filter="${filter} select(.context | test(\".*${buildjob}.*\") | not ) |"
done
IFS=$OLDIFS
echo "${filter}"
}

status_check () {
pending=0
success=0
checks=$( ci_status | jq -r ".statuses[] | $(build_filter) \"\(.context),\(.state)\"")
OLDIFS=$IFS
IFS=$'\n'$'\r'
for check in ${checks}; do
IFS=',' read context status <<< "${check}"
if [[ ${status} == "pending" ]]; then
echo "${context} check is pending"
((pending=pending+1))
elif [[ ${status} == "success" ]]; then
echo "${context} check is successful"
((success=success+1))
else
echo "${context} failed! Status: ${status}"
exit 1
fi
done
IFS=$OLDIFS
}

ci_status () {
status=$(gh api /repos/${OWNER}/${REPO}/commits/${COMMIT_HASH}/status)
if [[ $? -eq 0 ]]; then
echo ${status}
else
echo ${status} >&2
exit 1
fi
}

loop_status () {
count=0
status_check
while [[ ${pending} -gt 0 ]];
do
echo "${pending} pending check(s)... ${count}s"
sleep 10s
((count=count+10))
status_check
done
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
loop_status
fi
68 changes: 68 additions & 0 deletions .github/workflows/increment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
set -euo pipefail
# Get current release number, increment, and create a new release.

increment_version() {
local usage=" USAGE: $FUNCNAME [-l] [-t] <version> [<position>] [<leftmost>]
-l : remove leading zeros
-t : drop trailing zeros
<version> : The version string.
<position> : Optional. The position (starting with one) of the number
within <version> to increment. If the position does not
exist, it will be created. Defaults to last position.
<leftmost> : The leftmost position that can be incremented. If does not
exist, position will be created. This right-padding will
occur even to right of <position>, unless passed the -t flag."

# Get flags.
local flag_remove_leading_zeros=0
local flag_drop_trailing_zeros=0
while [ "${1:0:1}" == "-" ]; do
if [ "$1" == "--" ]; then shift; break
elif [ "$1" == "-l" ]; then flag_remove_leading_zeros=1
elif [ "$1" == "-t" ]; then flag_drop_trailing_zeros=1
else echo -e "Invalid flag: ${1}\n$usage"; return 1; fi
shift; done

# Get arguments.
if [ ${#@} -lt 1 ]; then echo "$usage"; return 1; fi
local v="${1}" # version string
local targetPos=${2-last} # target position
local minPos=${3-${2-0}} # minimum position

# Split version string into array using its periods.
local IFSbak; IFSbak=IFS; IFS='.' # IFS restored at end of func to
read -ra v <<< "$v" # avoid breaking other scripts.

# Determine target position.
if [ "${targetPos}" == "last" ]; then
if [ "${minPos}" == "last" ]; then minPos=0; fi
targetPos=$((${#v[@]}>${minPos}?${#v[@]}:$minPos)); fi
if [[ ! ${targetPos} -gt 0 ]]; then
echo -e "Invalid position: '$targetPos'\n$usage"; return 1; fi
(( targetPos-- )) || true # offset to match array index

# Make sure minPosition exists.
while [ ${#v[@]} -lt ${minPos} ]; do v+=("0"); done;

# Increment target position.
v[$targetPos]=$(printf %0${#v[$targetPos]}d $((10#${v[$targetPos]}+1)));

# Remove leading zeros, if -l flag passed.
if [ $flag_remove_leading_zeros == 1 ]; then
for (( pos=0; $pos<${#v[@]}; pos++ )); do
v[$pos]=$((${v[$pos]}*1)); done; fi

# If targetPosition was not at end of array, reset following positions to
# zero (or remove them if -t flag was passed).
if [[ ${flag_drop_trailing_zeros} -eq "1" ]]; then
for (( p=$((${#v[@]}-1)); $p>$targetPos; p-- )); do unset v[$p]; done
else for (( p=$((${#v[@]}-1)); $p>$targetPos; p-- )); do v[$p]=0; done; fi

echo "${v[*]}"
IFS=IFSbak
return 0
}

increment_version "${1}"

86 changes: 86 additions & 0 deletions .github/workflows/slackpost.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/usr/bin/env bash
# This script is utilized to post slack messages based up on templates from the templates directory. Checkout the# README for more information on how to generate a template.
#
# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail
# Turn on traces, useful while debugging but commented out by default
#set -o xtrace

SLACK_CHANNEL=vaapi-cicd
TEMPLATE=general
DIRECTORY=${PWD}

usage() { echo "Usage: $(basename "$0") [options] <text>
Options:
-c <slack channel>
-w <webhook URL>
-t <template>
-d <template directory>
" 1>&2; exit 1; }

# Verifies arguments
while getopts ":c:w:t:d:" o; do
case "${o}" in
c)
SLACK_CHANNEL="${OPTARG}"
;;
w)
SLACK_WEBHOOK="${OPTARG}"
;;
t)
TEMPLATE="${OPTARG}"
;;
d)
DIRECTORY="${OPTARG}"
;;
*)
echo "Illegal option: ${OPTARG}"$'\n'
usage
;;
esac
done

# Verifies SLACK_WEBHOOK is defined
if [[ -z ${SLACK_WEBHOOK:-} ]]; then
echo "No webhook URL specified. Please use the -w option."$'\n'
usage
fi

shift $((OPTIND-1))

# Reads in the message.
message=$*
if [[ "${message}" == "" ]]; then
if [ -t 0 ]; then
echo "No text specified"$'\n'
echo "Usage: $(basename "$0") [options] <text>"
else
while IFS= read -r line || [ -n "$line" ]; do
message="${message}${line}"
done
fi
fi

# Checks the file exist
if [[ -f templates/${TEMPLATE}.json ]]; then
template=templates/${TEMPLATE}.json
elif [[ -f /etc/slackpost/templates/${TEMPLATE}.json ]]; then
template=/etc/slackpost/templates/${TEMPLATE}.json
elif [[ -f ${DIRECTORY}/${TEMPLATE}.json ]]; then
template=${DIRECTORY}/${TEMPLATE}.json
else
echo "template not found. Please place templates in /etc/slackpost/templates, local templates directory or specify directory with the -d argument. Also check for typos!"
usage
exit 1
fi

#Creates the JSON payload from the template.
slack_message=$(eval "echo '$(<${template})'")
# Posts to Slack
echo "$(curl --silent -X POST -H 'Content-type: application/json' --data "$slack_message" "$SLACK_WEBHOOK" 2>&1)"

0 comments on commit 3a4bd1f

Please sign in to comment.