Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/tools/ci/utils.sh
25112 views
1
# Helper utilities for GitHub Actions jobs.
2
# To include: source ./tools/ci/utils.sh
3
4
# Print files modified in this pull request branch (and not deleted).
5
# Usage: readarray -t changed_files < <(get_changed_files)
6
get_changed_files() {
7
local compared_branch="${1:-master}"
8
9
while read fp; do
10
# Ignore files that no longer exist.
11
if [[ -e "$fp" ]]; then
12
echo "$fp"
13
fi
14
done < <(git diff --name-only "$compared_branch" || true)
15
# TODO: Only list files modified on *this* branch.
16
# `git diff --name-only "$compared_branch"...` doesn't work on shallow clones.
17
}
18
19
# Add label(s) to GitHub issue.
20
# Requires environment variables `ISSUE_URL` and `GITHUB_TOKEN`.
21
add_label() {
22
local label
23
# Deduplicate args.
24
declare -A labels_set
25
for label in "$@"; do
26
labels_set["$label"]=1
27
done
28
29
# Collect labels and join with comma.
30
local i=1 len="${#labels_set[@]}" item items
31
for label in "${!labels_set[@]}"; do
32
item=$(printf '"%s"' "$label")
33
items="${items}${item}"
34
35
if (( i < len )); then items="${items}, "; fi
36
((i+=1))
37
done
38
39
# Create the JSON payload.
40
local json_str=$(printf '{"labels": [%s]}' "$items")
41
42
if [[ -z "ISSUE_URL" ]]; then
43
echo "[utils:add_label] Requires env variable: ISSUE_URL" >&2
44
exit 1
45
fi
46
47
if [[ -z "$GITHUB_TOKEN" ]]; then
48
echo "[utils:add_label] Requires env variable: GITHUB_TOKEN" >&2
49
exit 1
50
fi
51
52
curl -X POST \
53
-H "Accept: application/vnd.github.v3+json" \
54
-H "Authorization: token ${GITHUB_TOKEN}" \
55
"${ISSUE_URL}/labels" \
56
--data "$json_str"
57
}
58
59
# Add comment to GitHub issue.
60
# Requires environment variables `ISSUE_URL` and `GITHUB_TOKEN`.
61
add_comment() {
62
local message="$1"
63
64
if [[ -z "ISSUE_URL" ]]; then
65
echo "[utils:add_comment] Requires env variable: ISSUE_URL" >&2
66
exit 1
67
fi
68
69
if [[ -z "$GITHUB_TOKEN" ]]; then
70
echo "[utils:add_comment] Requires env variable: GITHUB_TOKEN" >&2
71
exit 1
72
fi
73
74
# Escape string for JSON.
75
local body="$(echo -n -e $message \
76
| python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))')"
77
local json_str=$(printf '{"body": %s}' "$body")
78
79
curl -X POST \
80
-H "Accept: application/vnd.github.v3+json" \
81
-H "Authorization: token $GITHUB_TOKEN" \
82
"${ISSUE_URL}/comments" \
83
--data "$json_str"
84
}
85
86
# Create new commit and push. Requires `GITHUB_ACTOR` env variable.
87
# If no files changed, exit without commit.
88
push_commit() {
89
local message="$1"
90
91
if [[ -z "$GITHUB_ACTOR" ]]; then
92
echo "[utils:push_commit] Requires env variable: GITHUB_ACTOR" >&2
93
exit 1
94
fi
95
96
cd "$(git rev-parse --show-toplevel)" # Move to repo root.
97
98
# Exclude this file from commit if checked out from elsewhere.
99
if git ls-files --modified --error-unmatch ./tools/ci/utils.sh > /dev/null 2>&1; then
100
git restore -- ./tools/ci/utils.sh
101
fi
102
103
if [[ -z $(git ls-files --modified) ]]; then
104
echo "[utils:push_commit] No files changed, exiting."
105
exit 0
106
fi
107
108
# Set author and commit.
109
git config --global user.name "$GITHUB_ACTOR"
110
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
111
git commit --all --message "$message"
112
# Push to the current branch.
113
git push
114
}
115
116