Last active
April 12, 2021 16:56
-
-
Save kyanny/c91bde1ea81a63815baaa51ac139f309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
usage_exit() { | |
echo "Usage:" | |
echo " export GITHUB_API_TOKEN=xxxxxxxx" | |
echo " bash $0 org_name [ip_address]" | |
exit | |
} | |
if [[ -z $1 ]];then | |
usage_exit | |
fi | |
org=$1 | |
if [[ -z $2 ]];then | |
ip_address=$(curl ifconfig.me/ip) | |
else | |
ip_address=$2 | |
fi | |
if [[ $(which jq) -eq "" ]];then | |
curl -LO "https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64" | |
jq="./jq-linux64" | |
chmod +x $jq | |
else | |
jq=jq | |
fi | |
graphql_query=/tmp/graphql_query.txt | |
rm -f ${graphql_query} | |
cat <<EOF >$graphql_query | |
{ | |
organization(login: "$org") { | |
id | |
ipAllowListEntries(first: 100) { | |
nodes { | |
name | |
allowListValue | |
isActive | |
} | |
totalCount | |
} | |
} | |
} | |
EOF | |
cat >&2 $graphql_query | |
json=/tmp/graphql_payload.json | |
rm -f ${json} | |
$jq -n \ | |
--arg graphql_query "$(cat $graphql_query)" \ | |
'{query: $graphql_query}' > ${json} | |
cat >&2 ${json} | |
curl -v \ | |
-H 'Accept: application/vnd.github.audit-log-preview+json' \ | |
-H 'Content-Type: application/json' \ | |
-H "Authorization: token ${GITHUB_API_TOKEN}" \ | |
https://api.github.com/graphql -d @${json} | $jq | tee /tmp/result.json | |
owner_id=$($jq -r '.data.organization.id' /tmp/result.json) | |
graphql_query=/tmp/graphql_query.txt | |
rm -f ${graphql_query} | |
cat <<EOF >$graphql_query | |
mutation { | |
createIpAllowListEntry(input: {ownerId: "$owner_id", allowListValue: "$ip_address", isActive: true}) { | |
ipAllowListEntry { | |
id | |
allowListValue | |
isActive | |
} | |
} | |
} | |
EOF | |
cat >&2 $graphql_query | |
json=/tmp/graphql_payload.json | |
rm -f ${json} | |
$jq -n \ | |
--arg graphql_query "$(cat $graphql_query)" \ | |
'{query: $graphql_query}' > ${json} | |
cat >&2 ${json} | |
curl -v \ | |
-H 'Accept: application/vnd.github.audit-log-preview+json' \ | |
-H 'Content-Type: application/json' \ | |
-H "Authorization: token ${GITHUB_API_TOKEN}" \ | |
https://api.github.com/graphql -d @${json} | $jq | |
#rm -f ${graphql_query} | |
#rm -f ${json} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment