-
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
·122 lines (104 loc) · 2.88 KB
/
Copy pathsync-github-metadata.sh
File metadata and controls
executable file
·122 lines (104 loc) · 2.88 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
#!/usr/bin/env bash
# Sync GitHub repository description and homepage 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="${OPENPHYSICS_ORG:-OpenPhysics}"
DRY_RUN=0
usage() {
cat <<'EOF'
Usage: sync-github-metadata.sh [options]
Update GitHub repo description, website URL, and template flag using structure/repos.json.
Catalog rows with `"type": "template"` also get `gh repo edit --template` so
"Use this template" stays enabled.
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=""
count=0
while IFS= read -r repo; do
name="$(jq -r '.name' <<<"$repo")"
description="$(jq -r '.description // ""' <<<"$repo")"
homepage="$(jq -r '.githubHomepage // ""' <<<"$repo")"
repo_type="$(jq -r '.type // ""' <<<"$repo")"
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 [[ "$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 ..."
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
count=$((count + 1))
done < <(jq -c '.[]' <<<"$repos_json")
echo "Synced $count repositories."