-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathcommon.sh
More file actions
114 lines (102 loc) · 2.75 KB
/
common.sh
File metadata and controls
114 lines (102 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# The output appears in a dedicated emphasized GitHub step box.
# Multiple lines are not supported.
# Markdown is not supported.
gh_log() {
local LEVEL="$1"
shift
if [ "$CI" = "false" ]; then
case "$LEVEL" in
"debug")
printf "\e[1;90m"
;;
"error")
printf "\e[1;31m"
;;
"warning")
printf "\e[1;33m"
;;
*)
printf "\e[1;32m"
;;
esac
fi
echo "::$LEVEL::$*"
if [ "$CI" = "false" ]; then printf "\e[0m"; fi
}
export -f gh_log
# Sets a step output value.
# Examples:
# gh_output name value
# gh_output name <<EOF
# value
# EOF
gh_output() {
local NAME="$1"
if [ "$#" -eq 1 ]; then
echo "$NAME<<END_OF_VALUE"
cat
echo "END_OF_VALUE"
else
shift
echo "$NAME=$*"
fi >>"$GITHUB_OUTPUT"
}
export -f gh_output
check_not_empty() {
for V in "$@"; do
typeset -n VAR="$V"
if [ -z "${VAR:-}" ]; then
gh_log error "Variable $V is not set or empty"
exit 1
fi
done
}
export -f check_not_empty
check_not_empty GITHUB_STEP_SUMMARY
# The output appears in the GitHub step summary box.
# Multiple lines are supported.
# Markdown is supported.
# Examples:
# gh_summary "Markdown summary"
# gh_summary <<EOF
# Markdown summary
# EOF
gh_summary() {
if [ "$CI" = "false" ]; then printf "\e[94m"; fi
if [ "$#" -eq 0 ]; then
cat # for the data passed via the pipe
else
echo -e "$@" # for the data passed as arguments
fi >>"$GITHUB_STEP_SUMMARY"
if [ "$CI" = "false" ]; then printf "\e[0m"; fi
}
export -f gh_summary
# Fetches the CHANGELOG from a release branch
# and returns the content of the section for a given version.
fetch_changelog() {
RELEASE_BRANCH="$1"
VERSION="$2"
check_not_empty \
RELEASE_BRANCH \
VERSION
ESCAPED_VERSION="${VERSION//./\.}"
CHANGELOG="$(gh api \
-H "Accept: application/vnd.github.v3.raw" \
"/repos/${GITHUB_REPOSITORY}/contents/CHANGELOG.md?ref=${RELEASE_BRANCH}"
)"
echo "$CHANGELOG" | sed -n "/^## \[$ESCAPED_VERSION]$/,/^## \[/p" | sed '1d;$d' | sed '/./,$!d'
}
export -f fetch_changelog
# bash trick to check if the script is sourced.
if ! (return 0 2>/dev/null); then # called
SCRIPT="$1"
check_not_empty \
SCRIPT \
GITHUB_REPOSITORY \
GITHUB_REF_NAME
URL="/repos/$GITHUB_REPOSITORY/contents/.github/workflows/scripts/$SCRIPT.sh?ref=$GITHUB_REF_NAME"
shift
gh_log debug "Executing '$SCRIPT.sh' from '$GITHUB_REPOSITORY' $GITHUB_REF_NAME branch with: ${*@Q}"
gh api -H "Accept: application/vnd.github.v3.raw" "$URL" | bash -s -- "$@"
fi