This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathgithub-labels.sh
executable file
·176 lines (131 loc) · 3.4 KB
/
github-labels.sh
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
#
# Copyright (c) 2019 Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#
# Description: Generate the combined GitHub labels database for the
# specified repository.
set -e
script_name=${0##*/}
source "/etc/os-release" || "source /usr/lib/os-release"
self_dir=$(dirname "$(readlink -f "$0")")
cidir="${self_dir}/../../.ci"
source "${cidir}/lib.sh"
typeset -r labels_file="labels.yaml"
typeset -r labels_template="${labels_file}.in"
typeset -r master_labels_file="${self_dir}/${labels_file}"
typeset -r master_labels_template="${self_dir}/${labels_template}"
# The GitHub labels API requires a colour for each label so
# default to a white background.
typeset -r default_color="ffffff"
need_yq() {
# install yq if not exist
${cidir}/install_yq.sh
command -v yq &>/dev/null || \
die 'yq command not found. Ensure "$GOPATH/bin" is in your $PATH.'
}
merge_yaml()
{
local -r file1="$1"
local -r file2="$2"
local -r out="$3"
[ -n "$file1" ] || die "need 1st file"
[ -n "$file2" ] || die "need 2nd file"
[ -n "$out" ] || die "need output file"
need_yq
yq merge "$file1" --append "$file2" > "$out"
}
check_yaml()
{
local -r file="$1"
[ -n "$file" ] || die "need file to check"
need_yq
yq read "$file" >/dev/null
[ -z "$(command -v yamllint)" ] && die "need yamllint installed"
# Deal with different versions of the tool
local opts=""
local has_strict_opt=$(yamllint --help 2>&1|grep -- --strict)
[ -n "$has_strict_opt" ] && opts+="--strict"
yamllint $opts "$file"
}
# Expand the variables in the labels database.
generate_yaml()
{
local repo="$1"
local template="$2"
local out="$3"
[ -n "$repo" ] || die "need repo"
[ -n "$template" ] || die "need template"
[ -n "$out" ] || die "need output file"
local repo_slug=$(echo "${repo}"|sed 's!github.com/!!g')
sed \
-e "s|REPO_SLUG|${repo_slug}|g" \
-e "s|DEFAULT_COLOUR|${default_color}|g" \
"$template" > "$out"
check_yaml "$out"
}
cmd_generate()
{
local repo="$1"
local out_file="$2"
[ -n "$repo" ] || die "need repo"
[ -n "$out_file" ] || die "need output file"
# Create the master database from the template
generate_yaml \
"${repo}" \
"${master_labels_template}" \
"${master_labels_file}"
local -r repo_labels_template="${GOPATH}/src/${repo}/${labels_template}"
local -r repo_labels_file="${GOPATH}/src/${repo}/${labels_file}"
# Check for a repo-specific set of labels
if [ -e "${repo_labels_template}" ]; then
info "Found repo-specific labels database"
# Generate repo-specific labels from template
generate_yaml \
"${repo}" \
"${repo_labels_template}" \
"${repo_labels_file}"
# Combine the two databases
tmp=$(mktemp)
merge_yaml \
"${master_labels_file}" \
"${repo_labels_file}" \
"${tmp}"
mv "${tmp}" "${out_file}"
else
info "No repo-specific labels database"
cp "${master_labels_file}" "${out_file}"
fi
info "Generated labels database ${out_file}"
# Perform checks
kata-github-labels check "${out_file}"
}
usage()
{
cat <<EOF
Usage: ${script_name} help
${script_name} generate <repo-name> <output-file>
Examples:
# Generate combined labels database for runtime repo and write to
# specified file
\$ ${script_name} generate github.com/kata-containers/kata-containers /tmp/out.yaml
EOF
}
main()
{
case "$1" in
generate)
shift
cmd_generate "$@"
;;
help|"")
usage
exit 0
;;
*)
die "Invalid command: '$1'"
;;
esac
}
main "$@"