-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-cooker2rolling
More file actions
executable file
·127 lines (120 loc) · 3.54 KB
/
Copy pathgithub-cooker2rolling
File metadata and controls
executable file
·127 lines (120 loc) · 3.54 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
#!/bin/sh
json() {
python -c "
import json,sys
try:
print(json.load(sys.stdin)$@)
except (IndexError, KeyError, TypeError):
pass
"
}
# Print a GitHub GraphQL/REST error body to stderr.
# Exit 0 only when the payload has data.organization.
graphql_ok() {
python -c '
import json, sys
raw = sys.stdin.read()
try:
payload = json.loads(raw)
except Exception:
sys.stderr.write(raw[:800] + "\n")
sys.exit(1)
message = payload.get("message")
if message:
sys.stderr.write(message + "\n")
for err in payload.get("errors") or []:
sys.stderr.write(str(err.get("message", err)) + "\n")
if not (payload.get("data") or {}).get("organization"):
sys.exit(1)
'
}
github_graphql() {
curl -sS \
-H "Authorization: Bearer $OAUTH_TOKEN" \
-H "Content-Type: application/json" \
-H "User-Agent: OpenMandriva-infra-scripts" \
-X POST -d "$1" https://api.github.com/graphql
}
token_usable() {
_probe="$(curl -sS \
-H "Authorization: Bearer $1" \
-H "Content-Type: application/json" \
-H "User-Agent: OpenMandriva-infra-scripts" \
-d '{"query":"{ viewer { login } }"}' \
https://api.github.com/graphql)"
echo "$_probe" | python -c '
import json, sys
try:
payload = json.load(sys.stdin)
except Exception:
sys.exit(1)
sys.exit(0 if (payload.get("data") or {}).get("viewer") else 1)
'
}
if [ -n "$OAUTH_TOKEN" ] && ! token_usable "$OAUTH_TOKEN"; then
echo "OAUTH_TOKEN was rejected by GitHub (bad or expired credentials)." >&2
OAUTH_TOKEN=""
fi
if [ -z "$OAUTH_TOKEN" ] && [ -n "$GH_TOKEN" ] && token_usable "$GH_TOKEN"; then
OAUTH_TOKEN="$GH_TOKEN"
fi
if [ -z "$OAUTH_TOKEN" ] && command -v gh >/dev/null 2>&1; then
_gh="$(gh auth token 2>/dev/null)" || _gh=""
if [ -n "$_gh" ] && token_usable "$_gh"; then
echo "Using GitHub token from: gh auth token" >&2
OAUTH_TOKEN="$_gh"
fi
fi
if [ -z "$OAUTH_TOKEN" ]; then
echo "Before running this script, set OAUTH_TOKEN to a token"
echo "generated using https://github.com/settings/tokens"
echo "or authenticate with: gh auth login"
exit 1
fi
MYDIR="$(realpath $(dirname $0))"
TEMP="`mktemp -d /tmp/ghc2rXXXXXX`"
cd $TEMP
START=""
PERPAGE=100
COUNT=0
for org in OpenMandrivaAssociation; do
while true; do
QUERY="{\"query\": \"query {organization(login:\\\"$org\\\") { id name repositories(first:$PERPAGE $START) { edges { node { id name } cursor } pageInfo { endCursor hasNextPage } } } }\""
if ! REPOS="$(github_graphql "$QUERY")"; then
echo "$QUERY failed" >&2
break
fi
if [ -z "$REPOS" ]; then
echo "$QUERY failed" >&2
break
fi
if ! echo "$REPOS" | graphql_ok; then
echo "$QUERY failed" >&2
exit 1
fi
for i in $(seq 0 $((PERPAGE-1))); do
R="$(echo "$REPOS" | json "['data']['organization']['repositories']['edges'][$i]['node']['name']")"
[ "$R" = "distro-release" -o "$R" = "distro-release-desktop-Plasma6" -o "$R" = "distro-release-desktop-Plasma-Mobile" -o "$R" = "openmandriva-repos" -o "$R" = "lsb-release" -o "$R" = "om-midna-sddm" ] && continue
if [ -z "$R" ]; then
echo "No more repositories at $QUERY $i" >&2
break
fi
COUNT=$((COUNT+1))
echo "Retagging $org/$R"
cd "$TEMP"
git clone "git@github.com:$org/$R"
if [ -d "$R" ]; then
cd "$R"
git checkout -b rolling origin/master
git push --force origin rolling
cd ..
rm -rf "$R"
fi
done
[ "$(echo "$REPOS" | json "['data']['organization']['repositories']['pageInfo']['hasNextPage']")" != "True" ] && break
START="after:\\\"$(echo "$REPOS" | json "['data']['organization']['repositories']['pageInfo']['endCursor']")\\\""
done
done
echo "$COUNT repositories retagged."
cd "$MYDIR"
rm -rf "$TEMP"