skwid138
11/2/2018 - 6:19 PM

Accept all invitations to collaborate on Github

Accept all invitations to collaborate on Github

#!/bin/bash

# must install JQ
# https://stedolan.github.io/jq/download/
# on OSX brew install jq

# must set Github Personal Access Token with full repo access only

### Set named arguments -t
while getopts ":t:" opt; do
  case $opt in
    t) TOKEN="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    ;;
  esac
done

### Validate inputs
if [ -z ${TOKEN+x} ]; then
  echo " Github Personal Access Token (-t) must be set"
  exit 1
fi

# get list of invites (to trouble shoot add -i to show headers)
INVITES=$(curl "https://api.github.com/user/repository_invitations?access_token=${TOKEN}")

# array to contain IDs
INVITE_ID_LIST=()

# Loop through JSON response, remove spaces and new lines as well as removing quotes (-r)
for INVITE in $(echo "${INVITES}" | jq -r '.[] | @base64')
do
    _jq() {
     echo ${INVITE} | base64 --decode | jq -r ${1}
    }

    echo 'Invite ID', $(_jq '.id')
    INVITE_ID_LIST+=($(_jq '.id'))
done

# loop through invite IDs and accept them
for ID in ${INVITE_ID_ARRAY[@]} 
do 
    curl -X PATCH "https://api.github.com/user/repository_invitations/${ID}?access_token=${TOKEN}"
done