Skip to content

Commit

Permalink
Add a setting to configure accepted fallback solutions for
Browse files Browse the repository at this point in the history
retrieving changelog texts from git or GitHub.
  • Loading branch information
JonatanAntoni authored Nov 7, 2022
1 parent ae7f7f4 commit c8db65f
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 22 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This library is written for Bash v5 or later and uses a couple of standard
- echo
- find
- git (optional)
- gh (optional)
- grep
- mkdir
- mv
Expand Down Expand Up @@ -108,6 +109,17 @@ can use the same relative file names as within the `.pdsc` file.
Packs specified in the `<requirements>` section are considered automatically and do not need to be listed.
For example, add `ARM.CMSIS.pdsc` to include this file during packchk. The `.pdsc` files are referenced from
`${CMSIS_PACK_ROOT}/.Web` folder. Missing files are downloaded.
1. Replace `<full|release|tag>` for `PACK_CHANGELOG_MODE` with either of these choices. It defaults to `full`. This setting
is only effective when generating the changelog from Git history. It affects the fallback solutions
used to retrieve the changelog text from git:

`full` allows fallback to GitHub release description or commit message.

`release` allows fallback to GitHub release description only.

`tag` forces tag annotation messages to be used without any fallback.

If no changelog text can be retrieved pack generation is aborted.
1. Put custom commands to be executed before/after populating the pack build folder
into the `preprocess` and `postprocess` functions.

Expand Down
21 changes: 18 additions & 3 deletions gen-pack
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

GEN_PACK_LIB_VERSION="0.5.2"
GEN_PACK_LIB_VERSION="0.6.0"
GEN_PACK_LIB_SOURCE="$(realpath $(dirname ${BASH_SOURCE}))"

shopt -s expand_aliases
Expand Down Expand Up @@ -170,7 +170,21 @@ function gen_pack {
fi
if [ -n "${CHANGELOG+x}" ]; then
find_git
find_ghcli
case ${PACK_CHANGELOG_MODE} in
'full'|'release')
find_ghcli
;;
'tag')
echo_v "Skipping ghcli"
;;
*)
echo_err "PACK_CHANGELOG_MODE has invalid value '${PACK_CHANGELOG_MODE}'!"
echo_log " Valid options are: full, release, or tag."
echo_log " Defaulting to: full"
PACK_CHANGELOG_MODE="full"
find_ghcli
;;
esac
fi

echo_log " "
Expand Down Expand Up @@ -203,6 +217,7 @@ function gen_pack {
[[ ${PREPROCESS} == 1 && "$(type -t preprocess)" == "function" ]] && preprocess

if [ -n "${CHANGELOG+x}" ]; then
echo_log "Generating changelog with mode '${PACK_CHANGELOG_MODE}' ..."
pdsc_update_releases "${PACK_DESCRIPTION_FILE}" "${PACK_BUILD_DESCRIPTION_FILE}" "${CHANGELOG}"
else
cp "${PACK_DESCRIPTION_FILE}" "${PACK_BUILD_DESCRIPTION_FILE}"
Expand All @@ -229,5 +244,5 @@ function gen_pack {
fi

echo_log ""
echo_log "Pack $(basename ${PACK_ARCHIVE}) generated successfully!"
echo_log "${LOG_COLOR_GREEN}Pack $(basename ${PACK_ARCHIVE}) generated successfully!"
}
43 changes: 34 additions & 9 deletions lib/gittools
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

PACK_CHANGELOG_MODE="${PACK_CHANGELOG_MODE:-full}"

function git_describe {
local PREFIX="$1"

Expand Down Expand Up @@ -141,19 +143,42 @@ function git_changelog {
fi

for TAG in ${TAGS[@]}; do
local TYPE=$("${UTILITY_GIT}" for-each-ref --format "%(objecttype)" ${TAG})
local TYPE="$("${UTILITY_GIT}" for-each-ref --format "%(objecttype)" ${TAG})"
local TAG="${TAG#refs/tags/}"
local DESC=$("${UTILITY_GIT}" tag -l -n99 --format "%(contents)" ${TAG})
local DATE=$("${UTILITY_GIT}" tag -l -n99 --format "%(taggerdate:short)" ${TAG})
local DESC="$("${UTILITY_GIT}" tag -l -n99 --format "%(contents)" ${TAG})"
local DATE="$("${UTILITY_GIT}" tag -l -n99 --format "%(taggerdate:short)" ${TAG})"
if [[ -z "$DATE" ]]; then
local DATE=$( "${UTILITY_GIT}" tag -l -n99 --format "%(committerdate:short)" ${TAG})
local DATE="$( "${UTILITY_GIT}" tag -l -n99 --format "%(committerdate:short)" ${TAG})"
fi
if [[ $TYPE == "commit" ]] && "${UTILITY_GHCLI}" release view ${TAG} >/dev/null 2>/dev/null; then
local RELEASE=$("${UTILITY_GHCLI}" release view ${TAG} --json "body" -t "{{ .body }}")
if [ -n "$RELEASE" ]; then
local DESC="$RELEASE"
if [ "${TYPE}" = "tag" ]; then
echo_v "Changelog for tag '${TAG}' taken from annotation message."
elif [ "${TYPE}" = "commit" ] && [ "${PACK_CHANGELOG_MODE}" != "tag" ]; then
if "${UTILITY_GHCLI}" release view ${TAG} >/dev/null 2>/dev/null; then
local RELEASE="$("${UTILITY_GHCLI}" release view ${TAG} --json "body" -t "{{ .body }}")"
if [ -n "${RELEASE}" ]; then
local DESC="${RELEASE}"
echo_v "Changelog for tag '${TAG}' taken from GitHub release description."
elif [ "${PACK_CHANGELOG_MODE}" = "full" ]; then
echo_v "Changelog for tag '${TAG}' taken from commit message."
else
echo_err "Changelog generation for tag '${TAG}' failed!"
echo_log " => Tag has no annotation message."
echo_log " => GitHub release description is empty."
exit 1
fi
local DATE="$("${UTILITY_GHCLI}" release view ${TAG} --json "publishedAt" -t "{{ timefmt \"2006-01-02\" .publishedAt }}")"
elif [ "${PACK_CHANGELOG_MODE}" = "full" ]; then
echo_v "Changelog for tag '${TAG}' taken from commit message."
else
echo_err "Changelog generation for tag '${TAG}' failed!"
echo_log " => Tag has no annotation message."
echo_log " => GitHub release description could not be queried."
exit 1
fi
local DATE=$("${UTILITY_GHCLI}" release view ${TAG} --json "publishedAt" -t "{{ timefmt \"2006-01-02\" .publishedAt }}")
else
echo_err "Changelog generation for tag '${TAG}' failed!"
echo_log " => Tag has no annotation message."
exit 1
fi
git_changelog_$FORMAT "${TAG#${PREFIX}}" "${DATE}" "${DESC}" "${TAG}"
done
Expand Down
8 changes: 8 additions & 0 deletions template/gen_pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ PACKCHK_DEPS="
<list pdsc files here>
"

# Optional: restrict fallback modes for changelog generation
# Default: full
# Values:
# - full Tag annotations, release descriptions, or commit messages (in order)
# - release Tag annotations, or release descriptions (in order)
# - tag Tag annotations only
PACK_CHANGELOG_MODE="<full|release|tag>"

# custom pre-processing steps
function preprocess() {
# add custom steps here to be executed
Expand Down
106 changes: 96 additions & 10 deletions test/tests_gittools.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash

. "$(dirname "$0")/../lib/logging"
. "$(dirname "$0")/../lib/gittools"

setUp() {
Expand All @@ -12,6 +13,7 @@ tearDown() {
unset GIT_MOCK_DESCRIBE
unset GIT_MOCK_REVPARSE
unset GIT_MOCK_REVLIST
PACK_CHANGELOG_MODE="full"
}

git_mock() {
Expand All @@ -33,18 +35,38 @@ git_mock() {
fi
;;
'for-each-ref')
echo "refs/tags/v1.5.0"
echo "refs/tags/v1.2.4"
echo "refs/tags/v1.2.3"
echo "refs/tags/v0.9.0"
return 0
if [[ " $* " =~ " --format %(refname)" ]]; then
echo "refs/tags/v1.5.0"
echo "refs/tags/v1.2.4"
echo "refs/tags/v1.2.3"
echo "refs/tags/v0.9.0"
echo "refs/tags/v0.0.1"
return 0
elif [[ " $* " =~ " --format %(objecttype)" ]]; then
case "${@: -1}" in
"refs/tags/v1.5.0"|"refs/tags/v1.2.4"|"refs/tags/v1.2.3")
echo "tag"
;;
*)
echo "commit"
;;
esac
return 0
fi
;;
'rev-list')
return ${GIT_MOCK_REVLIST-1}
;;
'tag')
if [[ " $* " =~ " %(contents) " ]]; then
echo "Change log text for release version ${@: -1}"
case "${@: -1}" in
"v1.5.0"|"v1.2.4"|"v1.2.3")
echo "Change log text for release version ${@: -1}"
;;
*)
echo "Commit message for ${@: -1}"
;;
esac
return 0
elif [[ " $* " =~ " %(taggerdate:short) " ]]; then
case "${@: -1}" in
Expand Down Expand Up @@ -73,6 +95,26 @@ git_mock() {
return 1
}

ghcli_mock() {
echo "gh $*" >&2

if [ $1 = "release" ] && [ $2 = "view" ]; then
if [ $3 == "v0.0.1" ]; then
return 1
fi

if [[ " $* " =~ " --json body " ]]; then
echo "Release description for version $3"
elif [[ " $* " =~ " --json publishedAt " ]]; then
echo "2021-07-29"
fi
return 0
fi

echo "Error: unrecognized gh command '$1'" >&2
return 1
}

test_git_describe() {
UTILITY_GIT="git_mock"

Expand Down Expand Up @@ -115,6 +157,7 @@ test_git_describe() {

test_git_changelog_pdsc() {
UTILITY_GIT="git_mock"
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"

Expand All @@ -135,7 +178,10 @@ test_git_changelog_pdsc() {
Change log text for release version v1.2.3
</release>
<release version="0.9.0" date="2021-07-29" tag="v0.9.0">
Change log text for release version v0.9.0
Release description for version v0.9.0
</release>
<release version="0.0.1" date="2021-07-29" tag="v0.0.1">
Commit message for v0.0.1
</release>
</releases>
EOF
Expand Down Expand Up @@ -165,7 +211,10 @@ test_git_changelog_pdsc_with_empty_devlog() {
Change log text for release version v1.2.3
</release>
<release version="0.9.0" date="2021-07-29" tag="v0.9.0">
Change log text for release version v0.9.0
Release description for version v0.9.0
</release>
<release version="0.0.1" date="2021-07-29" tag="v0.0.1">
Commit message for v0.0.1
</release>
</releases>
EOF
Expand Down Expand Up @@ -195,7 +244,10 @@ test_git_changelog_pdsc_with_devlog() {
Change log text for release version v1.2.3
</release>
<release version="0.9.0" date="2021-07-29" tag="v0.9.0">
Change log text for release version v0.9.0
Release description for version v0.9.0
</release>
<release version="0.0.1" date="2021-07-29" tag="v0.0.1">
Commit message for v0.0.1
</release>
</releases>
EOF
Expand Down Expand Up @@ -239,7 +291,13 @@ test_git_changelog_html() {
<tr>
<td>v0.9.0</td>
<td>
Change log text for release version v0.9.0
Release description for version v0.9.0
</td>
</tr>
<tr>
<td>v0.0.1</td>
<td>
Commit message for v0.0.1
</td>
</tr>
</table>
Expand All @@ -249,4 +307,32 @@ EOF
assertEquals "${expected}" "${changelog}"
}

test_git_changelog_fail_tag() {
UTILITY_GIT="git_mock"
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
PACK_CHANGELOG_MODE="tag"

changelog=$(git_changelog -f text -p v 2>&1)
assertNotEquals 0 $?

echo "$changelog"
assertContains "${changelog}" "Changelog generation for tag 'v0.9.0' failed!"
}

test_git_changelog_fail_release() {
UTILITY_GIT="git_mock"
UTILITY_GHCLI="ghcli_mock"

GIT_MOCK_DESCRIBE="v1.5.0-3-g1abcdef"
PACK_CHANGELOG_MODE="release"

changelog=$(git_changelog -f text -p v 2>&1)
assertNotEquals 0 $?

echo "$changelog"
assertContains "${changelog}" "Changelog generation for tag 'v0.0.1' failed!"
}

. "$(dirname "$0")/shunit2/shunit2"

0 comments on commit c8db65f

Please sign in to comment.