-
Notifications
You must be signed in to change notification settings - Fork 1
/
add-component
executable file
·172 lines (151 loc) · 4.45 KB
/
add-component
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
#!/bin/bash
components_file=$(mktemp)
function clean_up() {
rm -f $components_file
}
trap clean_up EXIT
# Get the latest versions of a topic per product
function get_topic_versions(){
local topic_type=${1^^}
declare -A limits
declare -A products
limits=( [OCP]=8 [OSP]=2 [RHEL]=10 )
products=( [OCP]=OpenShift [OSP]=OpenStack [RHEL]=RHEL )
# get product and limit from topic
product=${products[${topic_type}]}
limit=${limits[${topic_type}]}
if [[ -z ${product} ]]; then
echo "Invalid Topic Type: ${topic_type}"
echo "Valid Topic Types: ${VALID_DCI_TOPIC_TYPES[*]}"
exit 1
fi
# Get Product ID
product_id=$( dcictl \
--format json \
product-list \
--where "name:${product}" |
jq -r '.products[].id'
)
all_versions=( $( dcictl \
--format json \
topic-list \
--where "status:active" \
--where "product_id:${product_id}" \
--where "name:${topic_type}*" |
jq -r '.topics[0:'${limit}'][] | .name')
)
echo ${all_versions[@]}
}
VALID_DCI_RELEASE_TAGS=(
dev
candidate
ga
)
VALID_DCI_TOPIC_TYPES=(
OCP
OSP
RHEL
)
# API Secrets
DCI_CLIENT_ID=${INPUT_DCICLIENTID}
DCI_API_SECRET=${INPUT_DCIAPISECRET}
DCI_CS_URL=${INPUT_DCICSURL}
export DCI_CLIENT_ID
export DCI_API_SECRET
export DCI_CS_URL
# Required Inputs
TOPICS=(${INPUT_DCITOPICS//,/ })
COMPONENT_NAME=${INPUT_COMPONENTNAME}
COMPONENT_VERSION=${INPUT_COMPONENTVERSION}
COMPONENT_RELEASE=${INPUT_COMPONENTRELEASE}
# Optional Inputs
COMPONENT_TAGS=${INPUT_COMPONENTTAGS}
COMPONENT_URL=${INPUT_COMPONENTURL}
COMPONENT_DATA=${INPUT_COMPONENTDATA}
# componentRelease
if ! grep -q "${COMPONENT_RELEASE,,}" <<< "${VALID_DCI_RELEASE_TAGS[@]}"; then
echo "Invalid Release: ${COMPONENT_RELEASE}"
echo "Valid Releases: ${VALID_DCI_RELEASE_TAGS[@]}"
exit 1
fi
# componentTags
if [[ -n ${COMPONENT_TAGS} ]]; then
tags="--tags ${COMPONENT_TAGS}"
fi
# componentURL
if [[ -n ${COMPONENT_URL} ]]; then
if ! grep -iqP "^https?://" <<< "${COMPONENT_URL}"; then
echo "Invalid URL schema, must begin with 'http(s)://': ${COMPONENT_URL}"
exit 1
fi
url="--url ${COMPONENT_URL}"
fi
# componentData
if [[ -n ${COMPONENT_DATA} ]]; then
jq . <<< "${COMPONENT_DATA}" > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Invalid JSON in dciComponentData"
exit 1
fi
data="--data $(jq -c . <<<${COMPONENT_DATA})"
fi
# dciTopicVersion
if [[ -z "${TOPICS[*]}" ]]; then
echo "No topics provided"
exit 1
fi
# Detect topic wildcard "all-<TOPIC_TYPE>"
if [[ ${#TOPICS[@]} -eq 1 ]] && grep -q all- <<< "${TOPICS[0]}"; then
old_topic=${TOPICS[0]}
TOPICS=( $(get_topic_versions "${TOPICS[0]#all-*}") )
if [[ $? -ne 0 ]]; then
echo "Failed to get versions for ${old_topic}"
exit 1
fi
fi
for topic in ${TOPICS[@]}; do
output=$( \
dci-create-component \
--format json \
${topic} \
"${COMPONENT_NAME}" \
${COMPONENT_VERSION} \
${COMPONENT_RELEASE} \
${tags} ${url} ${data}
)
status_code=$(jq -r .status_code <<< "${output}")
# Component already exists, retrieve it
if [[ "${status_code}" -eq "409" ]]; then
topic_id=$(jq -r .message <<< "${output}" |
grep -oP "'topic_id': '[^,]+" |
cut -d "'" -f4
)
type=$(jq -r .message <<< "${output}" |
grep -oP "'type': '[^,]+" |
cut -d "'" -f4
)
name=$(jq -r .message <<< "${output}" |
grep -oP "'name': '[^,]+" |
cut -d "'" -f4
)
query="type:${type},version:${COMPONENT_VERSION},name:${name}"
component=$(\
dcictl \
--format json \
component-list \
--topic-id ${topic_id} \
--where "${query}" |
jq -r '.components[0]'
)
if [[ "${component}" == "null" ]]; then
echo "Failed to find component ${name}"
exit 1
fi
output='{"component": '${component}'}'
elif [[ "${status_code}" -ne "null" ]]; then
echo "Failed to create component for ${COMPONENT_NAME}-${COMPONENT_VERSION}"
exit 1
fi
jq . <<< "$output" >> ${components_file}
done
echo "components=$(jq -s -c '.' ${components_file})" >> "${GITHUB_OUTPUT}"