Skip to content

Commit

Permalink
Merge pull request #9 from andyfeller/users-file
Browse files Browse the repository at this point in the history
Add support for username file
  • Loading branch information
andyfeller authored Oct 31, 2023
2 parents 59c92cb + 75db986 commit cd3db2b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 59 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@ I must give thanks to [@martinwoodward](https://github.com/martinwoodward) becau

## Usage

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

Generate montage from GitHub user avatars.

USAGE
gh-montage [options] <organization>
gh-montage [options] <organization>/<team>
gh montage [options] <organization>
gh montage [options] <organization>/<team>
gh montage [options] <path/to/username file>

FLAGS
-a, --avatar-pixels <integer> Size of GitHub avatar icons in pixels; default '48'
-d, --debug Enable debugging
-f, --force Whether to overwrite output file if it exists
-h, --help Displays help usage
-m, --montage-width <integer> Width of GitHub montage in number of avatar icons; default '58'
-o, --output-file <output-file> Name of GitHub montage file to generate, without '.jpg' extension
-p, --preserve Preserve temporary directory containing data
Expand Down
151 changes: 94 additions & 57 deletions gh-montage
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ ORGANIZATION=
OUTPUTFILE=
PRESERVE=false
TEAM=
USERS=()

__USAGE="
Generate montage from GitHub user avatars.
USAGE
$(basename $0) [options] <organization>
$(basename $0) [options] <organization>/<team>
gh montage [options] <organization>
gh montage [options] <organization>/<team>
gh montage [options] <path/to/username file>
FLAGS
-a, --avatar-pixels <integer> Size of GitHub avatar icons in pixels; default '$AVATARPIXELS'
-d, --debug Enable debugging
-f, --force Whether to overwrite output file if it exists
-h, --help Displays help usage
-m, --montage-width <integer> Width of GitHub montage in number of avatar icons; default '$MONTAGEWIDTH'
-o, --output-file <output-file> Name of GitHub montage file to generate, without '.png' extension
-p, --preserve Preserve temporary directory containing data
Expand Down Expand Up @@ -92,6 +95,18 @@ elif [[ "$1" == *"/"* ]]; then
if [ -z "${OUTPUTFILE}" ]; then
OUTPUTFILE="$ORGANIZATION-$TEAM-$AVATARPIXELS.png"
fi
elif [ -f "$1" ]; then
# If argument is valid file, read usernames from file
USERS_FILE="$1"
printf "Reading users from file: %s\n" "$USERS_FILE"

while IFS= read -r USER; do
USERS+=("$USER")
done < "$USERS_FILE"

if [ -z "${OUTPUTFILE}" ]; then
OUTPUTFILE="$(basename ""$USERS_FILE"").png"
fi
else
ORGANIZATION="$1"

Expand Down Expand Up @@ -121,64 +136,62 @@ if ! $PRESERVE; then
trap 'rm -rf -- "$TMPDIR"' EXIT
fi

# Determine how many GitHub user
if [ -z "${TEAM}" ]; then
QUERY='
query CountOrgAvatars($login: String!) {
organization(login: $login) {
membersWithRole {
totalCount
# Generate script around retrieving user avatars and generating montage as necessary
echo "Generating GitHub montage script: $TMPFILE"

if [ -n "${USERS}" ]; then
echo "GitHub avatars found: ${#USERS[@]}"

for USER in "${USERS[@]}"; do
QUERY='
query GetUser($login: String!) {
user(login: $login) {
avatarUrl
databaseId
}
}
}'
}'

JQ='.data.organization.membersWithRole.totalCount'
TEMPLATE="
curl -s \"{{ .data.user.avatarUrl }}\" --output $TMPDIR/{{ .data.user.databaseId }}.jpg"

gh api graphql -f query="${QUERY}" -F login="$USER" --template="${TEMPLATE}" >> $TMPFILE
done
else
QUERY='
query CountOrgTeamAvatars($login: String!, $slug: String!) {
organization(login: $login) {
team(slug: $slug) {
members {
if [ -z "${TEAM}" ]; then
QUERY='
query CountOrgAvatars($login: String!) {
organization(login: $login) {
membersWithRole {
totalCount
}
}
}
}'

JQ='.data.organization.team.members.totalCount'
fi

AVATARCOUNT=$(gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --jq "$JQ")
echo "GitHub avatars found: $AVATARCOUNT"

# Generate script commands to download GitHub user avatar images sized accordingly
if [ -z "${TEAM}" ]; then
QUERY='
query GetOrgAvatars($login: String!, $endCursor: String) {
organization(login: $login) {
membersWithRole(first: 100, after: $endCursor) {
pageInfo {
endCursor
hasNextPage
}
nodes {
avatarUrl
databaseId
}'

JQ='.data.organization.membersWithRole.totalCount'
else
QUERY='
query CountOrgTeamAvatars($login: String!, $slug: String!) {
organization(login: $login) {
team(slug: $slug) {
members {
totalCount
}
}
}
}
}'
}'

TEMPLATE="
{{ range .data.organization.membersWithRole.nodes }}
curl -s \"{{ .avatarUrl }}\" --output $TMPDIR/{{ .databaseId }}.jpg
{{ end }}"
else
QUERY='
query GetOrgTeamAvatars($login: String!, $slug: String!, $endCursor: String) {
organization(login: $login) {
team(slug: $slug) {
members(first: 100, after: $endCursor) {
JQ='.data.organization.team.members.totalCount'
fi

AVATARCOUNT=$(gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --jq "$JQ")
echo "GitHub avatars found: $AVATARCOUNT"

# Generate script commands to download GitHub user avatar images sized accordingly
if [ -z "${TEAM}" ]; then
QUERY='
query GetOrgAvatars($login: String!, $endCursor: String) {
organization(login: $login) {
membersWithRole(first: 100, after: $endCursor) {
pageInfo {
endCursor
hasNextPage
Expand All @@ -189,17 +202,41 @@ else
}
}
}
}
}'
}'

TEMPLATE="
{{ range .data.organization.membersWithRole.nodes }}
curl -s \"{{ .avatarUrl }}\" --output $TMPDIR/{{ .databaseId }}.jpg
{{ end }}"

else
QUERY='
query GetOrgTeamAvatars($login: String!, $slug: String!, $endCursor: String) {
organization(login: $login) {
team(slug: $slug) {
members(first: 100, after: $endCursor) {
pageInfo {
endCursor
hasNextPage
}
nodes {
avatarUrl
databaseId
}
}
}
}
}'

TEMPLATE="
TEMPLATE="
{{ range .data.organization.team.members.nodes }}
curl -s \"{{ .avatarUrl }}\" --output $TMPDIR/{{ .databaseId }}.jpg
{{ end }}"
fi

echo "Generating GitHub montage script: $TMPFILE"
gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --paginate --template="${TEMPLATE}" >> $TMPFILE
fi

gh api graphql -f query="${QUERY}" -F login="$ORGANIZATION" -F slug="$TEAM" --paginate --template="${TEMPLATE}" >> $TMPFILE
fi

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

0 comments on commit cd3db2b

Please sign in to comment.