-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_info.sh
More file actions
executable file
·173 lines (156 loc) · 5.58 KB
/
user_info.sh
File metadata and controls
executable file
·173 lines (156 loc) · 5.58 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
#!/usr/bin/env bash
# Get GitHub user or organization profile info
# MIT License — Copyright 2026 Paul van Oorschot
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/_common.sh"
JSON_MODE=false
USERNAME=""
show_help() {
cat <<'EOF'
Usage: user_info.sh <username> [--json]
Get profile info for a GitHub user or organization, including
repos, followers, contributions, and organization memberships.
Options:
--json Output raw JSON
-h, --help Show this help
Examples:
user_info.sh torvalds
user_info.sh microsoft --json
user_info.sh octocat
EOF
exit 0
}
while [[ $# -gt 0 ]]; do
case "$1" in
--json) JSON_MODE=true; shift ;;
-h|--help) show_help ;;
-*) echo "Error: unknown option '$1'. See --help" >&2; exit 1 ;;
*)
if [[ -z "$USERNAME" ]]; then USERNAME="$1"; else echo "Error: unexpected argument '$1'" >&2; exit 1; fi
shift ;;
esac
done
if [[ -z "$USERNAME" ]]; then
echo "Error: username required. See --help" >&2
exit 1
fi
require_gh
# Try user first, fall back to organization
USER_GQL='query($login: String!) {
user(login: $login) {
__typename
login
name
bio
company
location
websiteUrl
twitterUsername
avatarUrl
createdAt
followers { totalCount }
following { totalCount }
repositories(privacy: PUBLIC) { totalCount }
starredRepositories { totalCount }
contributionsCollection { contributionCalendar { totalContributions } }
organizations(first: 10) { nodes { login name } }
pinnedItems(first: 6, types: REPOSITORY) {
nodes { ... on Repository { nameWithOwner stargazerCount description } }
}
}
}'
ORG_GQL='query($login: String!) {
organization(login: $login) {
__typename
login
name
description
websiteUrl
location
email
twitterUsername
avatarUrl
createdAt
repositories(privacy: PUBLIC) { totalCount }
membersWithRole { totalCount }
teams { totalCount }
pinnedItems(first: 6, types: REPOSITORY) {
nodes { ... on Repository { nameWithOwner stargazerCount description } }
}
}
}'
# Try user (may fail for orgs, that's ok)
# gh appends error text after JSON, so extract just the JSON object
raw=$(gh api graphql -f query="$USER_GQL" -f login="$USERNAME" 2>/dev/null) || true
user_data=$(echo "$raw" | jq '.data.user // null' 2>/dev/null || echo "null")
if [[ "$user_data" != "null" ]]; then
if $JSON_MODE; then
echo "$user_data" | jq '{
type: .__typename, login, name, bio, company, location,
websiteUrl, twitterUsername, createdAt,
followers: .followers.totalCount,
following: .following.totalCount,
publicRepos: .repositories.totalCount,
starredRepos: .starredRepositories.totalCount,
contributions: .contributionsCollection.contributionCalendar.totalContributions,
organizations: [.organizations.nodes[] | {login, name}],
pinnedRepos: [.pinnedItems.nodes[] | {nameWithOwner, stargazerCount, description}]
}'
exit 0
fi
echo "$user_data" | jq -r '
.login + (if .name then " (" + .name + ")" else "" end) +
(if .bio then "\n" + .bio else "" end) +
(if .company then "\n🏢 " + .company else "" end) +
(if .location then " | 📍 " + .location else "" end) +
(if .websiteUrl then "\n🔗 " + .websiteUrl else "" end) +
"\n" +
"\n👥 " + (.followers.totalCount | tostring) + " followers | " +
(.following.totalCount | tostring) + " following" +
"\n📦 " + (.repositories.totalCount | tostring) + " public repos | " +
"⭐ " + (.starredRepositories.totalCount | tostring) + " starred" +
"\n📊 " + (.contributionsCollection.contributionCalendar.totalContributions | tostring) + " contributions (last year)" +
(if (.organizations.nodes | length) > 0 then
"\n🏛️ Orgs: " + ([.organizations.nodes[].login] | join(", "))
else "" end) +
(if (.pinnedItems.nodes | length) > 0 then
"\n\nPinned repos:" +
([.pinnedItems.nodes[] | "\n ⭐ " + (.stargazerCount | tostring) + " " + .nameWithOwner + " — " + (.description // "No description")] | join(""))
else "" end)
'
exit 0
fi
# Try organization
raw=$(gh api graphql -f query="$ORG_GQL" -f login="$USERNAME" 2>/dev/null) || true
org_data=$(echo "$raw" | jq '.data.organization // null' 2>/dev/null || echo "null")
if [[ "$org_data" != "null" ]]; then
if $JSON_MODE; then
echo "$org_data" | jq '{
type: .__typename, login, name, description, location,
websiteUrl, email, twitterUsername, createdAt,
publicRepos: .repositories.totalCount,
members: .membersWithRole.totalCount,
teams: .teams.totalCount,
pinnedRepos: [.pinnedItems.nodes[] | {nameWithOwner, stargazerCount, description}]
}'
exit 0
fi
echo "$org_data" | jq -r '
.login + " [Organization]" + (if .name then " — " + .name else "" end) +
(if .description then "\n" + .description else "" end) +
(if .location then "\n📍 " + .location else "" end) +
(if .websiteUrl then " | 🔗 " + .websiteUrl else "" end) +
(if .email then " | ✉️ " + .email else "" end) +
"\n" +
"\n📦 " + (.repositories.totalCount | tostring) + " public repos | " +
"👥 " + (.membersWithRole.totalCount | tostring) + " members | " +
"🏷️ " + (.teams.totalCount | tostring) + " teams" +
(if (.pinnedItems.nodes | length) > 0 then
"\n\nPinned repos:" +
([.pinnedItems.nodes[] | "\n ⭐ " + (.stargazerCount | tostring) + " " + .nameWithOwner + " — " + (.description // "No description")] | join(""))
else "" end)
'
exit 0
fi
echo "Error: user or organization '$USERNAME' not found" >&2
exit 1