forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfigure.sh
executable file
·182 lines (153 loc) · 5.3 KB
/
transfigure.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
177
178
179
180
181
182
#!/usr/bin/env bash
# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# A script that uses configurator to create a YAML file, and then pushes that
# YAML file to k8s/test-infra's config/testgrids directory
#
# Used by teams that host different instances of Prow, but want to show their
# results on testgrid.k8s.io
#
set -o errexit
set -o nounset
set -o pipefail
BINARY_DIR="${KO_DATA_PATH:-}"
main() {
branch="transfigure-branch"
if [[ $# -lt 5 ]]; then
echo "Usage: $(basename "$0") [github_token or 'test'] [prow_config] [prow_job_config] [testgrid_yaml] [repo_subdir] (remote_fork_repo) (git_user) (git_email)" >&2
echo "All [] arguments are required paths" >&2
exit 1
fi
parse-args "$@"
user-from-token
email-from-token
echo "Ensuring kubernetes/test-infra repo"
if [[ -d test-infra ]]; then
echo "Directory 'test-infra' found; using as kubernetes/test-infra repository"
else
git clone https://github.com/kubernetes/test-infra.git
trap "cleanup-repository" EXIT
echo "Created temporary repository"
fi
k8s_repo=$(readlink -f test-infra)
testgrid_dir=$(readlink -f "test-infra/config/testgrids")
echo "Checking out ${branch}"
cd "${k8s_repo}"
git checkout -B "${branch}"
if [[ ! -d "${testgrid_dir}/${testgrid_subdir}" ]]; then
echo "Subdirectory ${testgrid_subdir} doesn't exist; creating it" >&2
mkdir -p "${testgrid_dir}/${testgrid_subdir}"
fi
echo "Generating testgrid yaml"
"${BINARY_DIR}/configurator" \
--prow-config "${prow_config}" \
--prow-job-config "${job_config}" \
--output-yaml \
--yaml "${testgrid_config}" \
--oneshot \
--output "${testgrid_dir}/${testgrid_subdir}/gen-config.yaml"
git add --all
if git diff --cached --quiet --exit-code; then
echo "Transfigure did not change anything. Aborting no-op bump"
exit 0
fi
echo "Running kubernetes/test-infra tests..."
./hack/make-rules/go-run/arbitrary.sh test ./config/tests/...
make verify-spelling
echo "Tests successful!"
if [[ ${dry_run} = "true" ]]; then
echo "Dry-Run; skipping PR"
return 0
fi
ensure-git-config
title="Update TestGrid for ${testgrid_subdir}"
git commit -m "${title}"
echo "Pushing commit to ${user}/${remote_fork_repo}:${branch}..."
git push -f "https://${user}:$(cat "${token}")@github.com/${user}/${remote_fork_repo}" "HEAD:${branch}"
echo "Creating PR to merge ${user}:${branch} into k8s/test-infra:master..."
"${BINARY_DIR}/pr-creator" \
--github-token-path="${token}" \
--org="kubernetes" --repo="test-infra" --branch=master \
--title="${title}" --head-branch="${branch}" \
--body="Generated by transfigure.sh" \
--source="${user}:${branch}" \
--confirm
echo "PR created successfully!"
return 0
}
parse-args() {
if [[ "$1" == "test" || "$1" == "Test" ]]; then
dry_run=true
token=""
else
dry_run=false
token=$(readlink -m "$1")
fi
prow_config=$(readlink -m "$2")
job_config=$(readlink -m "$3")
testgrid_config=$(readlink -m "$4")
testgrid_subdir="$5"
remote_fork_repo=${6:-"test-infra"}
user=${7:-""}
email=${8:-""}
if [[ ! $dry_run && ! -f ${token} ]]; then
echo "ERROR: [github_token] ${token} must be a file path." >&2
exit 1
elif [[ ! -f "${prow_config}" ]]; then
echo "ERROR: [prow_config] ${prow_config} must be a file path." >&2
exit 1
elif [[ ! -e "${job_config}" ]]; then
echo "ERROR: [prow_job_config] ${job_config} must exist." >&2
exit 1
elif [[ ! -e "${testgrid_config}" ]]; then
echo "ERROR: [testgrid_yaml] ${testgrid_config} must exist." >&2
exit 1
elif [[ -z "${testgrid_subdir}" ]]; then
echo "ERROR: [repo_subdir] must be specified." >&2
exit 1
elif [[ -z "${remote_fork_repo}" ]]; then
echo "ERROR: [remote_fork_repo] cannot be empty" >&2
exit 1
fi
}
user-from-token() {
if [[ -z "${user}" ]]; then
user=$(curl -H "Authorization: token $(cat "${token}")" "https://api.github.com/user" 2>/dev/null | sed -n "s/\s\+\"login\": \"\(.*\)\",/\1/p")
echo "Using user from GitHub: ${user}"
else
echo "Using user from Argument: ${user}"
fi
}
email-from-token() {
if [[ -z "${email}" ]]; then
email=$(curl -H "Authorization: token $(cat "${token}")" "https://api.github.com/user" 2>/dev/null | sed -n "s/\s\+\"email\": \"\(.*\)\",/\1/p")
echo "Using email from GitHub: ${email}"
else
echo "Using email from Argument: ${email}"
fi
}
cleanup-repository() {
echo "Cleaning temporary repository at ${k8s_repo}"
cd ..
rm -rf "${k8s_repo}"
}
ensure-git-config() {
echo "Checking Git Config"
git config user.name ${user}
git config user.email ${email}
git config user.name &>/dev/null && git config user.email &>/dev/null && return 0
echo "ERROR: git config user.name, user.email unset. No defaults provided" >&2
return 1
}
main "$@"