Skip to content

Commit

Permalink
move (Laerdal.Build.targets): move the file under Leardal.Scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ksidirop-laerdal committed Apr 10, 2024
1 parent c8581b2 commit c1dbbde
Show file tree
Hide file tree
Showing 7 changed files with 442 additions and 28 deletions.
5 changes: 4 additions & 1 deletion Laerdal.Dfu.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Misc", "_Misc", "{37EBD209
LICENSE = LICENSE
azure-pipelines.yaml = azure-pipelines.yaml
Laerdal.Scripts\Laerdal.Version.sh = Laerdal.Scripts\Laerdal.Version.sh
Laerdal.Build.targets = Laerdal.Build.targets
Laerdal.Scripts\Laerdal.Build.targets = Laerdal.Scripts\Laerdal.Build.targets
Laerdal.Scripts\Laerdal.Changelog.sh = Laerdal.Scripts\Laerdal.Changelog.sh
Laerdal.Scripts\Laerdal.CreateNewReleaseInGithub.sh = Laerdal.Scripts\Laerdal.CreateNewReleaseInGithub.sh
Laerdal.Scripts\Laerdal.SetupBuildEnvironment.sh = Laerdal.Scripts\Laerdal.SetupBuildEnvironment.sh
EndProjectSection
EndProject
Global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
<PropertyGroup>
<Laerdal_Log_Level>High</Laerdal_Log_Level>

<PackageOutputPath Condition=" '$(PackageOutputPath)' == '' AND '$(TF_BUILD)' == '' ">$([System.IO.Path]::Combine($(MSBuildThisFileDirectory), `Output/`))</PackageOutputPath>
<PackageOutputPath Condition=" '$(PackageOutputPath)' == '' AND '$(TF_BUILD)' == '' ">$([System.IO.Path]::Combine($(MSBuildThisFileDirectory), `..`, `Output`))</PackageOutputPath>
<PackageOutputPath Condition=" '$(PackageOutputPath)' == '' AND '$(TF_BUILD)' != '' ">$(BUILD_ARTIFACTSTAGINGDIRECTORY)</PackageOutputPath>

<Laerdal_Project Condition=" '$(Laerdal_Project)' == '' ">$([System.IO.Path]::Combine($(MSBuildThisFileDirectory), `Laerdal.Dfu/Laerdal.Dfu.csproj`))</Laerdal_Project>
<Laerdal_Project Condition=" '$(Laerdal_Project)' == '' ">$([System.IO.Path]::Combine($(MSBuildThisFileDirectory), `..`, `Laerdal.Dfu`, `Laerdal.Dfu.csproj`))</Laerdal_Project>

<Laerdal_Script_FolderPath>$([System.IO.Path]::Combine($(MSBuildThisFileDirectory), `Laerdal.Scripts/`))</Laerdal_Script_FolderPath>
<Laerdal_Script_FolderPath>$(MSBuildThisFileDirectory)</Laerdal_Script_FolderPath>
</PropertyGroup>

<!-- VERSION -->
Expand Down
102 changes: 102 additions & 0 deletions Laerdal.Scripts/Laerdal.Changelog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

usage(){
echo "usage: ./Laerdal.Changelog.sh [-nv | --new-version X.Y.Z] [-o | --output version.txt] [-h | --help]"
echo "parameters:"
echo " -nv | --new-version [version] New major.minor.patch version (default is 0.0.0)"
echo " -o | --output [filename] Name of the output file"
echo " -h | --help Prints this message"
echo " -v | --verbose Verbose mode"
}

function log () {
if [[ $verbose -eq 1 ]]; then
echo "$@"
fi
}

filename="CHANGELOG.md"

while [ "$1" != "" ]; do
case $1 in
-nv | --new-version ) shift
newversion="$1"
;;
-o | --output ) shift
filename="$1"
;;
-h | --help ) usage
exit
;;
-v | --verbose ) verbose=1
;;
* ) echo
echo "### Wrong parameter: $1 ###"
echo
usage
exit 1
esac
shift
done


if [ ! -z "$newversion" ]; then
if [[ "$newversion" =~ .*"-".* ]]; then
log "New version contains a dash, skipping changelog generation"
else
currenthash=$(git show --format=%h --no-patch)
echo "$currenthash $newversion" > tags.txt
log "New version: $newversion"
fi
else
echo "" > tags.txt
fi

# Get all tags on develop and Filter out tags that are not in the format "HASH 1.2.3"
git tag --format='%(objectname:short) %(refname:short)' --sort=-version:refname --merged | grep -o '[a-z0-9]* [a-z0-9]*[.][a-z0-9]*[.][a-z0-9]*$' >> tags.txt

# Create changelog file
echo "# CHANGELOG" > "$filename"
echo "" >> "$filename"
log "Created changelog file: $filename"


# Loop through all tags and create changelog
lastline=''
while read line; do
if [ -z "$lastline" ]; then
lastline=$line
else
# Split the line into hash and version
lasthash=`echo $lastline | cut -d' ' -f1`
lastversion=`echo $lastline | cut -d' ' -f2`
hash=`echo $line | cut -d' ' -f1`

echo "## **$lastversion**" >> "$filename"
log "Added version: $lastversion"
# Get the commit message and author of the tag
git log -n 1 --pretty=tformat:"%b" $lasthash >> "$filename"

echo "" >> "$filename"

# Get all commits between the current tag and the previous tag
git log $hash..$lasthash --pretty=format:"- %s [%cn]" --no-merges >> "$filename"

echo "" >> "$filename"
echo "" >> "$filename"

# Get the commit message and author of the tag
git log -n 1 --pretty=tformat:"> by _%cn_ on _%cd_" --date=format:'%Y-%m-%d %H:%M:%S' $lasthash >> "$filename"

echo "" >> "$filename"
echo "---" >> "$filename"
echo "" >> "$filename"
lastline=$line
fi
done < tags.txt

rm -r -f tags.txt

log "Done"

exit 0
167 changes: 167 additions & 0 deletions Laerdal.Scripts/Laerdal.CreateNewReleaseInGithub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#!/bin/bash

declare VERBOSE=0
declare TAG_VERSION=""

declare GIT_BRANCH=""
declare GITHUB_ACCESS_TOKEN=""
declare GITHUB_REPOSITORY_PATH=""

function parse_arguments() {
while [[ $# -gt 0 ]]; do
case $1 in

-v | --log)
VERBOSE=1
shift
;;

-r | --repository-path)
GITHUB_REPOSITORY_PATH="$2"
shift
;;

-t | --tag-version)
TAG_VERSION="$2"
shift
;;

-b | --git-branch)
GIT_BRANCH="$2"
shift
;;

-a | --access-token)
GITHUB_ACCESS_TOKEN="$2"
shift
;;

*)
echo "Unknown option: $1"
usage
exit 1
;;

esac
shift
done

if [[ -z $GIT_BRANCH ]]; then
echo "Missing git-branch."
usage
exit 1
fi

if [[ -z $GITHUB_REPOSITORY_PATH ]]; then
echo "Missing github-repository."
usage
exit 1
fi

if [[ -z $GITHUB_ACCESS_TOKEN ]]; then
echo "Missing github-access-token."
usage
exit 1
fi

validate_tag_format "$TAG_VERSION"
}

function validate_tag_format() {
local -r tag="$1"
local -r pattern='^[0-9]+\.[0-9]+(\.[0-9]+)?$'

if ! [[ $tag =~ $pattern ]]; then
exit_with_error "Tag format is invalid: '$tag'"
fi
}

function usage() {
local -r script_name=$(basename "$0")

echo "Usage: $script_name [--verbose|-v] [--repository-path|-r]=<repository_path> [--git-branch|-b]=<branch> [--access-token|-a]=<token> [--tag-version|-t]=<version>"
}

function create_release_on_github() {
# https://docs.github.com/en/rest/releases/releases?apiVersion=2022-11-28#create-a-release

local eventual_tag_name=""
local eventual_singleline_summary=""
if [[ $GIT_BRANCH == "refs/heads/main" || $GIT_BRANCH == "refs/heads/master" ]]; then
eventual_tag_name="v$TAG_VERSION" # builds targeting main have this simple and straightforward tag name
eventual_singleline_summary="Release $eventual_tag_name"

elif [[ $GIT_BRANCH == "refs/heads/develop" ]]; then # all builds that target develop are beta builds
eventual_tag_name="v$TAG_VERSION-beta"
eventual_singleline_summary="Beta $eventual_tag_name"

else # all other builds that dont target main are alpha builds should rarely happen in practice but just in case
eventual_tag_name="v$TAG_VERSION-alpha"
eventual_singleline_summary="Alpha $eventual_tag_name"
fi

local -r payload=$(
cat <<EOF
{
"tag_name": "$eventual_tag_name",
"target_commitish": "$GIT_BRANCH",
"name": "$eventual_singleline_summary",
"generate_release_notes": true,
"draft": false,
"prerelease": false
}
EOF
)

local -r api_url="https://api.github.com/repos/$GITHUB_REPOSITORY_PATH/releases"

echo "** Creating release on GitHub ..."

local -r response=$(
curl \
-i \
-sS \
-X "POST" \
-o /dev/null \
-d "$payload" \
-w "%{http_code}" \
-H "Content-Type:application/json" \
-H "Accept:application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_ACCESS_TOKEN" \
"$api_url"
)
local -r curl_exit_code=$?

log "** api_url=$api_url"
log "** payload=$payload"
log "** response=$response"
log "** curl_exit_code=$curl_exit_code"
if [[ $curl_exit_code -ne 0 ]]; then
exit_with_error "curl failed with exit code $?"
fi

local -r http_status_code="${response}"
if [[ $http_status_code -ge 300 ]]; then
exit_with_error "API returned HTTP status $http_status_code"
fi
}

function log() {
if [[ $VERBOSE -ne 0 ]]; then
echo "$@"
fi
}

function exit_with_error() {
echo "Error: $1"
exit 1
}

function main() {
parse_arguments "$@"
create_release_on_github
}

main "$@"
Loading

0 comments on commit c1dbbde

Please sign in to comment.