Skip to content

Commit cd3db2b

Browse files
authored
Merge pull request #9 from andyfeller/users-file
Add support for username file
2 parents 59c92cb + 75db986 commit cd3db2b

File tree

2 files changed

+102
-59
lines changed

2 files changed

+102
-59
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ I must give thanks to [@martinwoodward](https://github.com/martinwoodward) becau
1818

1919
## Usage
2020

21+
> **Note**
22+
> Processing username files assumes 1 username per line and will fail if any username is invalid.
23+
> This is because `gh-montage` is not retrieving usernames from GitHub API before processing them.
24+
2125
```shell
2226
$ gh montage --help
2327

2428
Generate montage from GitHub user avatars.
2529

2630
USAGE
27-
gh-montage [options] <organization>
28-
gh-montage [options] <organization>/<team>
31+
gh montage [options] <organization>
32+
gh montage [options] <organization>/<team>
33+
gh montage [options] <path/to/username file>
2934

3035
FLAGS
3136
-a, --avatar-pixels <integer> Size of GitHub avatar icons in pixels; default '48'
3237
-d, --debug Enable debugging
3338
-f, --force Whether to overwrite output file if it exists
39+
-h, --help Displays help usage
3440
-m, --montage-width <integer> Width of GitHub montage in number of avatar icons; default '58'
3541
-o, --output-file <output-file> Name of GitHub montage file to generate, without '.jpg' extension
3642
-p, --preserve Preserve temporary directory containing data

gh-montage

+94-57
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@ ORGANIZATION=
88
OUTPUTFILE=
99
PRESERVE=false
1010
TEAM=
11+
USERS=()
1112

1213
__USAGE="
1314
Generate montage from GitHub user avatars.
1415
1516
USAGE
16-
$(basename $0) [options] <organization>
17-
$(basename $0) [options] <organization>/<team>
17+
gh montage [options] <organization>
18+
gh montage [options] <organization>/<team>
19+
gh montage [options] <path/to/username file>
1820
1921
FLAGS
2022
-a, --avatar-pixels <integer> Size of GitHub avatar icons in pixels; default '$AVATARPIXELS'
2123
-d, --debug Enable debugging
2224
-f, --force Whether to overwrite output file if it exists
25+
-h, --help Displays help usage
2326
-m, --montage-width <integer> Width of GitHub montage in number of avatar icons; default '$MONTAGEWIDTH'
2427
-o, --output-file <output-file> Name of GitHub montage file to generate, without '.png' extension
2528
-p, --preserve Preserve temporary directory containing data
@@ -92,6 +95,18 @@ elif [[ "$1" == *"/"* ]]; then
9295
if [ -z "${OUTPUTFILE}" ]; then
9396
OUTPUTFILE="$ORGANIZATION-$TEAM-$AVATARPIXELS.png"
9497
fi
98+
elif [ -f "$1" ]; then
99+
# If argument is valid file, read usernames from file
100+
USERS_FILE="$1"
101+
printf "Reading users from file: %s\n" "$USERS_FILE"
102+
103+
while IFS= read -r USER; do
104+
USERS+=("$USER")
105+
done < "$USERS_FILE"
106+
107+
if [ -z "${OUTPUTFILE}" ]; then
108+
OUTPUTFILE="$(basename ""$USERS_FILE"").png"
109+
fi
95110
else
96111
ORGANIZATION="$1"
97112

@@ -121,64 +136,62 @@ if ! $PRESERVE; then
121136
trap 'rm -rf -- "$TMPDIR"' EXIT
122137
fi
123138

124-
# Determine how many GitHub user
125-
if [ -z "${TEAM}" ]; then
126-
QUERY='
127-
query CountOrgAvatars($login: String!) {
128-
organization(login: $login) {
129-
membersWithRole {
130-
totalCount
139+
# Generate script around retrieving user avatars and generating montage as necessary
140+
echo "Generating GitHub montage script: $TMPFILE"
141+
142+
if [ -n "${USERS}" ]; then
143+
echo "GitHub avatars found: ${#USERS[@]}"
144+
145+
for USER in "${USERS[@]}"; do
146+
QUERY='
147+
query GetUser($login: String!) {
148+
user(login: $login) {
149+
avatarUrl
150+
databaseId
131151
}
132-
}
133-
}'
152+
}'
134153

135-
JQ='.data.organization.membersWithRole.totalCount'
154+
TEMPLATE="
155+
curl -s \"{{ .data.user.avatarUrl }}\" --output $TMPDIR/{{ .data.user.databaseId }}.jpg"
156+
157+
gh api graphql -f query="${QUERY}" -F login="$USER" --template="${TEMPLATE}" >> $TMPFILE
158+
done
136159
else
137-
QUERY='
138-
query CountOrgTeamAvatars($login: String!, $slug: String!) {
139-
organization(login: $login) {
140-
team(slug: $slug) {
141-
members {
160+
if [ -z "${TEAM}" ]; then
161+
QUERY='
162+
query CountOrgAvatars($login: String!) {
163+
organization(login: $login) {
164+
membersWithRole {
142165
totalCount
143166
}
144167
}
145-
}
146-
}'
147-
148-
JQ='.data.organization.team.members.totalCount'
149-
fi
150-
151-
AVATARCOUNT=$(gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --jq "$JQ")
152-
echo "GitHub avatars found: $AVATARCOUNT"
153-
154-
# Generate script commands to download GitHub user avatar images sized accordingly
155-
if [ -z "${TEAM}" ]; then
156-
QUERY='
157-
query GetOrgAvatars($login: String!, $endCursor: String) {
158-
organization(login: $login) {
159-
membersWithRole(first: 100, after: $endCursor) {
160-
pageInfo {
161-
endCursor
162-
hasNextPage
163-
}
164-
nodes {
165-
avatarUrl
166-
databaseId
168+
}'
169+
170+
JQ='.data.organization.membersWithRole.totalCount'
171+
else
172+
QUERY='
173+
query CountOrgTeamAvatars($login: String!, $slug: String!) {
174+
organization(login: $login) {
175+
team(slug: $slug) {
176+
members {
177+
totalCount
178+
}
167179
}
168180
}
169-
}
170-
}'
181+
}'
171182

172-
TEMPLATE="
173-
{{ range .data.organization.membersWithRole.nodes }}
174-
curl -s \"{{ .avatarUrl }}\" --output $TMPDIR/{{ .databaseId }}.jpg
175-
{{ end }}"
176-
else
177-
QUERY='
178-
query GetOrgTeamAvatars($login: String!, $slug: String!, $endCursor: String) {
179-
organization(login: $login) {
180-
team(slug: $slug) {
181-
members(first: 100, after: $endCursor) {
183+
JQ='.data.organization.team.members.totalCount'
184+
fi
185+
186+
AVATARCOUNT=$(gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --jq "$JQ")
187+
echo "GitHub avatars found: $AVATARCOUNT"
188+
189+
# Generate script commands to download GitHub user avatar images sized accordingly
190+
if [ -z "${TEAM}" ]; then
191+
QUERY='
192+
query GetOrgAvatars($login: String!, $endCursor: String) {
193+
organization(login: $login) {
194+
membersWithRole(first: 100, after: $endCursor) {
182195
pageInfo {
183196
endCursor
184197
hasNextPage
@@ -189,17 +202,41 @@ else
189202
}
190203
}
191204
}
192-
}
193-
}'
205+
}'
206+
207+
TEMPLATE="
208+
{{ range .data.organization.membersWithRole.nodes }}
209+
curl -s \"{{ .avatarUrl }}\" --output $TMPDIR/{{ .databaseId }}.jpg
210+
{{ end }}"
211+
212+
else
213+
QUERY='
214+
query GetOrgTeamAvatars($login: String!, $slug: String!, $endCursor: String) {
215+
organization(login: $login) {
216+
team(slug: $slug) {
217+
members(first: 100, after: $endCursor) {
218+
pageInfo {
219+
endCursor
220+
hasNextPage
221+
}
222+
nodes {
223+
avatarUrl
224+
databaseId
225+
}
226+
}
227+
}
228+
}
229+
}'
194230

195-
TEMPLATE="
231+
TEMPLATE="
196232
{{ range .data.organization.team.members.nodes }}
197233
curl -s \"{{ .avatarUrl }}\" --output $TMPDIR/{{ .databaseId }}.jpg
198234
{{ end }}"
199-
fi
200235

201-
echo "Generating GitHub montage script: $TMPFILE"
202-
gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --paginate --template="${TEMPLATE}" >> $TMPFILE
236+
fi
237+
238+
gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --paginate --template="${TEMPLATE}" >> $TMPFILE
239+
fi
203240

204241
# Conclude script creation with commands to generate montage and execute
205242
cat << EOF >> $TMPFILE

0 commit comments

Comments
 (0)