udondan/iam-floyd

View on GitHub
bin/mkchangelog

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash

# These are only used for creating a changelog file manually. by default the version is taken form the VERSION file
VERSION=$1
BEFORE=$2

TMP_ACTIONS_NEW="./actions-new.floyd"
TMP_ACTIONS_REMOVED="./actions-removed.floyd"

function getLastTag() {
  git describe --tags --abbrev=0
}

# Get diff since last tag
function getDiff() {
  if [[ -n "${VERSION}" && -n "${BEFORE}" ]]; then
    git diff --no-color "${BEFORE}..${VERSION}" -- "$1" ":!$1/.cache"
  else
    git diff --no-color "$(getLastTag)" -- "$1" ":!$1/.cache"
  fi
}

function getVersion() {
  if [[ -n "${VERSION}" ]]; then
    echo "${VERSION}"
  else
    echo "v$(cat VERSION)"
  fi
}

function getNewServices() {
  getDiff stats/services | grep '^+[^+]' | sed "s/^+/- /g" | sed "s/;.*//g" | sort
}

function getRemovedServices() {
  getDiff stats/services | grep '^-[^-]' | sed "s/^-/- /g" | sed "s/;.*//g" | sort
}

function getNewActions() {
  getDiff stats/actions | grep '^+[^+]' | sed "s/^+/- /g" | sed "s/;.*//g" | sort
}

function getRemovedActions() {
  getDiff stats/actions | grep '^-[^-]' | sed "s/^-/- /g" | sed "s/;.*//g" | sort
}

function getNewResourceTypes() {
  getDiff stats/resources | grep '^+[^+]' | sed "s/^+/- /g" | sort
}

function getRemovedResourceTypes() {
  getDiff stats/resources | grep '^-[^-]' | sed "s/^-/- /g" | sort
}

function getNewConditions() {
  getDiff stats/conditions | grep '^+[^+]' | sed "s/^+/- /g" | sort
}

function getRemovedConditions() {
  getDiff stats/conditions | grep '^-[^-]' | sed "s/^-/- /g" | sort
}

function getUpdatedAccessLevels() {
  comm -1 -2 ${TMP_ACTIONS_NEW} ${TMP_ACTIONS_REMOVED} | sed "s/^-//g" | \
  while read -r line; do
    getDiff stats/actions | \
    grep "$line" | perl -0777 -pe "s/-([^;]+);(.*?)\n\+[^;]+;(.*)/- \1: \2 -> \3/gm"
  done
}

# Services
newServices=$(getNewServices)
removedServices=$(getRemovedServices)

# Actions
getNewActions > ${TMP_ACTIONS_NEW}
getRemovedActions > ${TMP_ACTIONS_REMOVED}
newActions=$(grep -vxFf ${TMP_ACTIONS_REMOVED} ${TMP_ACTIONS_NEW})
removedActions=$(grep -vxFf ${TMP_ACTIONS_NEW} ${TMP_ACTIONS_REMOVED})
updatedAccessLevels=$(getUpdatedAccessLevels)
rm -f ${TMP_ACTIONS_NEW} ${TMP_ACTIONS_REMOVED}

# Resource types
newResourceTypes=$(getNewResourceTypes)
removedResourceTypes=$(getRemovedResourceTypes)

# Conditions
newConditions=$(getNewConditions)
removedConditions=$(getRemovedConditions)

file="CHANGELOG/$(getVersion).md"
hasContent=false

[[ -f "$file" ]] && printf "\n---\n" >> "$file" && hasContent=true
touch "${file}"


if [ -n "${removedServices}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf ":warning: **Removed services:**\n\n" >> "$file"
  echo "${removedServices}" >> "$file"
  hasContent=true
fi

if [ -n "${removedActions}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf ":warning: **Removed actions:**\n\n" >> "$file"
  echo "${removedActions}" >> "$file"
  hasContent=true
fi

if [ -n "${removedConditions}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf ":warning: **Removed condition keys:**\n\n" >> "$file"
  echo "${removedConditions}" >> "$file"
  hasContent=true
fi

if [ -n "${removedResourceTypes}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf ":warning: **Removed resource types:**\n\n" >> "$file"
  echo "${removedResourceTypes}" >> "$file"
  hasContent=true
fi

if [ "${newServices}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf "**New services:**\n\n" >> "$file"
  echo "${newServices}" >> "$file"
  hasContent=true
fi

if [ -n "${newActions}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf "**New actions:**\n\n" >> "$file"
  echo "${newActions}" >> "$file"
  hasContent=true
fi

if [ -n "${updatedAccessLevels}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf "**Updated action access level:**\n\n" >> "$file"
  echo "${updatedAccessLevels}" >> "$file"
  hasContent=true
fi

if [ -n "${newResourceTypes}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf "**New resource types:**\n\n" >> "$file"
  echo "${newResourceTypes}" >> "$file"
  hasContent=true
fi

if [ -n "${newConditions}" ]; then
  [[ "${hasContent}" = true ]] && echo "" >> "$file"
  printf "**New condition keys:**\n\n" >> "$file"
  echo "${newConditions}" >> "$file"
  hasContent=true
fi