-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate-catalog.sh
More file actions
executable file
·151 lines (132 loc) · 5.47 KB
/
generate-catalog.sh
File metadata and controls
executable file
·151 lines (132 loc) · 5.47 KB
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
#!/bin/bash
set -o pipefail
# Generates catalog/ directory by fetching pattern-metadata.yaml from all repos
# tagged with "pattern" in the validatedpatterns orgs.
#
# Output:
# catalog/catalog.yaml — lightweight index of pattern names
# catalog/<name>/pattern.yaml — full normalized metadata per pattern
#
# Dependencies: gh, yq (v4+), jq, base64
ORGS=(${ORGS[@]:-"validatedpatterns" "validatedpatterns-sandbox"})
TOPIC=${TOPIC:-"ui-catalog-enabled"}
GENERATOR_VERSION="1.0"
CATALOG_DIR="catalog"
CATALOG_LOGO="https://validatedpatterns.io/images/logo.png"
# Normalize a single pattern-metadata.yaml (JSON from yq) into catalog schema.
# Reads JSON on stdin, writes normalized JSON on stdout.
# Args: $1 = org name
normalize_pattern() {
local org="$1"
jq --arg org "$org" '
# Split owners string into array
.owners = (
if (.owners | type) == "string" then
.owners | split(",") | map(gsub("^\\s+|\\s+$"; ""))
else
.owners // []
end
) |
# Flatten platform wrapper in requirements
# upstream: requirements.hub.compute.platform.aws -> we want: requirements.hub.compute.aws
.requirements = (
if .requirements then
.requirements | to_entries | map(
.value = (
.value | to_entries | map(
if .value.platform then
.value = .value.platform
else
.
end
) | from_entries
)
) | from_entries
else
null
end
) |
# Add org field
.org = $org |
# Ensure optional fields default to null
.external_requirements = (.external_requirements // null) |
.extra_features = (.extra_features // null) |
.docs_repo_url = (.docs_repo_url // null) |
.ci_url = (.ci_url // null) |
.spoke = (.spoke // null)
'
}
rm -rf "${CATALOG_DIR}"
mkdir -p "${CATALOG_DIR}"
pattern_names=()
for org in "${ORGS[@]}"; do
repos=$(gh repo list "${org}" --no-archived --topic "${TOPIC}" --visibility public --limit 100 | awk '{ print $1 }' | sort)
ret=$?
if [ ${ret} -ne 0 ]; then
echo "Error retrieving pattern list for ${org}" >&2
exit ${ret}
fi
for full_slug in ${repos}; do
repo_name="${full_slug#*/}"
echo "Checking ${full_slug}..." >&2
# Fetch pattern-metadata.yaml; skip if not found
api_response=$(gh api "repos/${full_slug}/contents/pattern-metadata.yaml" 2>/dev/null)
if [ $? -ne 0 ]; then
echo " No pattern-metadata.yaml, skipping." >&2
continue
fi
# Decode and convert YAML to JSON
raw_yaml=$(echo "${api_response}" | jq -r '.content' | base64 -d)
if [ $? -ne 0 ]; then
echo " Failed to decode content, skipping." >&2
continue
fi
pattern_json=$(echo "${raw_yaml}" | yq -o json '.' | normalize_pattern "${org}")
if [ $? -ne 0 ]; then
echo " Failed to parse metadata, skipping." >&2
continue
fi
# Fetch values-global.yaml and extract clusterGroupName
global_response=$(gh api "repos/${full_slug}/contents/values-global.yaml" 2>/dev/null)
if [ $? -eq 0 ]; then
global_yaml=$(echo "${global_response}" | jq -r '.content' | base64 -d)
if [ $? -eq 0 ]; then
cluster_group_name=$(echo "${global_yaml}" | yq -r '.main.clusterGroupName // ""')
if [ -n "${cluster_group_name}" ]; then
echo " Found clusterGroupName: ${cluster_group_name}" >&2
pattern_json=$(echo "${pattern_json}" | jq --arg cgn "${cluster_group_name}" '.clustergroupname = $cgn')
fi
else
echo " Failed to decode values-global.yaml, skipping clusterGroupName." >&2
fi
fi
# Write per-pattern YAML
mkdir -p "${CATALOG_DIR}/${repo_name}"
echo "${pattern_json}" | yq -P '.' > "${CATALOG_DIR}/${repo_name}/pattern.yaml"
# Fetch values-secret.yaml.template if it exists
template_response=$(gh api "repos/${full_slug}/contents/values-secret.yaml.template" 2>/dev/null)
if [ $? -eq 0 ]; then
echo " Found values-secret.yaml.template, fetching..." >&2
template_content=$(echo "${template_response}" | jq -r '.content' | base64 -d)
if [ $? -eq 0 ]; then
echo "${template_content}" > "${CATALOG_DIR}/${repo_name}/values-secret.yaml.template"
else
echo " Failed to decode values-secret.yaml.template, skipping." >&2
fi
fi
pattern_names+=("${repo_name}")
done
done
# Build catalog index
CATALOG_DESCRIPTION=${CATALOG_DESCRIPTION:-'(Tech-Preview) Additional patterns can be found here: <a href="https://validatedpatterns.io">validatedpatterns.io</a>'}
{
echo "generated_at: \"$(date -u +%Y-%m-%dT%H:%M:%SZ)\""
echo "generator_version: \"${GENERATOR_VERSION}\""
echo "catalog_description: '${CATALOG_DESCRIPTION}'"
echo "catalog_logo: \"${CATALOG_LOGO}\""
echo "patterns:"
for name in "${pattern_names[@]}"; do
echo " - ${name}"
done
} > "${CATALOG_DIR}/catalog.yaml"
echo "Wrote ${CATALOG_DIR}/ with ${#pattern_names[@]} pattern(s)." >&2