Skip to content

Instantly share code, notes, and snippets.

@Drowze
Created September 22, 2023 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drowze/978d63a2f597200bfce1423748d520ac to your computer and use it in GitHub Desktop.
Save Drowze/978d63a2f597200bfce1423748d520ac to your computer and use it in GitHub Desktop.
Track removed/added users in a GitHub organization
function __github_org_current_users \
--argument-names org_name
set -l users '[]'
for i in (seq 1000)
set -l page_users (gh api "/orgs/$org_name/members?page=$i" | jq 'map(.login)')
test "$page_users" = '[]' && break
set users (jq -n --argjson users "$users" --argjson page_users "$page_users" '[$users, $page_users] | add')
sleep 0.1
end
echo "$users"
end
function __last_users_file
set -l files ~/.org_users/*.json
echo "$files[-1]"
end
function __last_users \
--argument-names file
if test -n $file
cat "$file"
else
echo '[]'
end
end
function github_org_track_users \
--argument-names org_name
set -l last_file (__last_users_file)
set -l last_users (__last_users $last_file)
set -l current_users (__github_org_current_users $org_name)
set -l current_date (date -j +'%F %R')
set -l removed_users (jq -n --argjson current_users "$current_users" --argjson last_users "$last_users" '$last_users - $current_users')
set -l added_users (jq -n --argjson current_users "$current_users" --argjson last_users "$last_users" '$current_users - $last_users')
if test "$removed_users" != '[]' || test "$added_users" != '[]'
if ! test -e ~/.org_users/changes.csv
echo "from_date,to_date,removed_users,added_users" > ~/.org_users/changes.csv
end
string match -rq '.*org_users/(?<last_file_date>.*)\.json' "$last_file"
echo "$last_file_date,$current_date,$removed_users,$added_users" >> ~/.org_users/changes.csv
end
echo "$current_users" > ~/.org_users/$current_date.json
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment