-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-github-metadata.sh
More file actions
executable file
·196 lines (170 loc) · 5.04 KB
/
Copy pathsync-github-metadata.sh
File metadata and controls
executable file
·196 lines (170 loc) · 5.04 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
# Sync GitHub repository description, homepage, topics, and template flag from
# structure/repos.json.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=lib/repos.sh
source "$SCRIPT_DIR/lib/repos.sh"
ORG="${FLEET_ORG:-${OPENPHYSICS_ORG:-$(repos_org)}}"
DRY_RUN=0
usage() {
cat <<'EOF'
Usage: sync-github-metadata.sh [options]
Update GitHub repo description, website URL, topics, and template flag using
structure/repos.json.
Catalog rows with `"type": "template"` also get `gh repo edit --template` so
"Use this template" stays enabled.
Description:
Prefer shortDescription when set; otherwise description. GitHub caps at 350
characters (truncate with a warning if needed).
Topics (simulations + template only):
- Simulations: physics, scenerystack, simulation + kebab-case physicsTopics
+ optional githubTopics extras
- Template: physics, scenerystack, simulation, template
Topics are replaced (catalog is source of truth). Non-sim / non-template
repos leave topics untouched. GitHub caps topics at 20.
Options:
--dry-run Print planned changes without calling gh
--simulation Only simulation repositories
--repo NAME Sync a single repository
-h, --help Show this help
Requires: gh auth login with repo scope, jq installed.
EOF
}
REPO_NAME=""
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run)
DRY_RUN=1
shift
;;
--simulation)
FILTER_SIMULATION="true"
shift
;;
--repo)
REPO_NAME="${2:?Missing value for --repo}"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
if ! command -v gh >/dev/null 2>&1; then
echo "gh CLI is required" >&2
exit 1
fi
repos_require_jq
if [[ -n "$REPO_NAME" ]]; then
FILTER_NAME="$REPO_NAME"
fi
repos_json="$(repos_list_json)"
FILTER_NAME=""
# Build GitHub topic names for a catalog row. Prints nothing to skip.
topics_json_for_repo() {
local repo="$1"
# Apostrophe stripped via \u0027 so this jq program stays single-quote-safe in bash.
jq -c '
def slug:
ascii_downcase
| gsub("\u0027"; "")
| gsub("[^a-z0-9]+"; "-")
| gsub("^-+"; "")
| gsub("-+$"; "")
| .[0:50];
def uniq:
reduce .[] as $x ([]; if index($x) then . else . + [$x] end);
if .type == "template" then
["physics", "scenerystack", "simulation", "template"]
elif .isSimulation == true then
(
["physics", "scenerystack", "simulation"]
+ [(.physicsTopics // [])[] | slug | select(length > 0)]
+ [(.githubTopics // [])[]]
) | uniq | .[0:20]
else
empty
end
' <<<"$repo"
}
github_description_for_repo() {
local repo="$1"
jq -r '
if ((.shortDescription // "") | length) > 0 then .shortDescription
else .description // ""
end
' <<<"$repo"
}
put_topics() {
local name="$1"
local topics_json="$2"
# PUT /repos/{owner}/{repo}/topics replaces the full topic set.
jq -n --argjson names "$topics_json" '{names: $names}' \
| gh api -X PUT "repos/$ORG/$name/topics" --input - >/dev/null
}
count=0
while IFS= read -r repo; do
name="$(jq -r '.name' <<<"$repo")"
description="$(github_description_for_repo "$repo")"
homepage="$(jq -r '.githubHomepage // ""' <<<"$repo")"
repo_type="$(jq -r '.type // ""' <<<"$repo")"
topics_json="$(topics_json_for_repo "$repo" || true)"
echo "$name: homepage=${homepage:-"(none)"}"
if [[ -n "$description" ]]; then
if [[ ${#description} -le 80 ]]; then
echo " description: $description"
else
echo " description: ${description:0:77}..."
fi
fi
if [[ -n "$topics_json" ]]; then
echo " topics: $(jq -r 'join(", ")' <<<"$topics_json")"
fi
if [[ "$repo_type" == "template" ]]; then
echo " template: ensure is_template=true"
fi
if [[ "$DRY_RUN" -eq 1 ]]; then
echo " dry-run: gh repo edit $ORG/$name ..."
if [[ -n "$topics_json" ]]; then
echo " dry-run: PUT repos/$ORG/$name/topics"
fi
count=$((count + 1))
continue
fi
cmd=(gh repo edit "$ORG/$name")
if [[ -n "$description" ]]; then
# GitHub caps repository descriptions at 350 characters.
if [[ ${#description} -gt 350 ]]; then
truncated="${description:0:347}"
truncated="${truncated% *}..."
echo " warning: description is ${#description} chars; truncating to ${#truncated} for GitHub" >&2
description="$truncated"
fi
cmd+=(--description "$description")
fi
if [[ -n "$homepage" ]]; then
cmd+=(--homepage "$homepage")
fi
# Catalog type "template" must stay a GitHub template repo (Use this template).
if [[ "$repo_type" == "template" ]]; then
cmd+=(--template)
fi
if ! "${cmd[@]}"; then
exit 1
fi
if [[ -n "$topics_json" ]]; then
if ! put_topics "$name" "$topics_json"; then
echo " error: failed to update topics for $ORG/$name" >&2
exit 1
fi
fi
count=$((count + 1))
done < <(jq -c '.[]' <<<"$repos_json")
echo "Synced $count repositories."