get_changed_files() {
local compared_branch="${1:-master}"
while read fp; do
if [[ -e "$fp" ]]; then
echo "$fp"
fi
done < <(git diff --name-only "$compared_branch" || true)
}
add_label() {
local label
declare -A labels_set
for label in "$@"; do
labels_set["$label"]=1
done
local i=1 len="${#labels_set[@]}" item items
for label in "${!labels_set[@]}"; do
item=$(printf '"%s"' "$label")
items="${items}${item}"
if (( i < len )); then items="${items}, "; fi
((i+=1))
done
local json_str=$(printf '{"labels": [%s]}' "$items")
if [[ -z "ISSUE_URL" ]]; then
echo "[utils:add_label] Requires env variable: ISSUE_URL" >&2
exit 1
fi
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "[utils:add_label] Requires env variable: GITHUB_TOKEN" >&2
exit 1
fi
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${ISSUE_URL}/labels" \
--data "$json_str"
}
add_comment() {
local message="$1"
if [[ -z "ISSUE_URL" ]]; then
echo "[utils:add_comment] Requires env variable: ISSUE_URL" >&2
exit 1
fi
if [[ -z "$GITHUB_TOKEN" ]]; then
echo "[utils:add_comment] Requires env variable: GITHUB_TOKEN" >&2
exit 1
fi
local body="$(echo -n -e $message \
| python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')"
local json_str=$(printf '{"body": %s}' "$body")
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_TOKEN" \
"${ISSUE_URL}/comments" \
--data "$json_str"
}
push_commit() {
local message="$1"
if [[ -z "$GITHUB_ACTOR" ]]; then
echo "[utils:push_commit] Requires env variable: GITHUB_ACTOR" >&2
exit 1
fi
cd "$(git rev-parse --show-toplevel)"
if git ls-files --modified --error-unmatch ./tools/ci/utils.sh > /dev/null 2>&1; then
git restore -- ./tools/ci/utils.sh
fi
if [[ -z $(git ls-files --modified) ]]; then
echo "[utils:push_commit] No files changed, exiting."
exit 0
fi
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
git commit --all --message "$message"
git push
}