diff --git a/org-scripts/README.md b/org-scripts/README.md new file mode 100644 index 0000000..7ec2b5a --- /dev/null +++ b/org-scripts/README.md @@ -0,0 +1,8 @@ +# org-scripts + +This directory contains scripts to help with org management. + +Set the environment variable `GITHUB_TOKEN` to: +``` +export GITHUB_TOKEN="token" +``` diff --git a/org-scripts/apply-teams.sh b/org-scripts/apply-teams.sh new file mode 100755 index 0000000..de2f807 --- /dev/null +++ b/org-scripts/apply-teams.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# TODO Make the mapping more dynamic... +# TEAMS=`./githubapi-get_json.sh $GITHUB_TOKEN /orgs/xmidt-org/teams | jq -r -j ".[] | .name, \"=\", .id, \"\n\""` +TEAMS='admins=3322421 writers=3307585' + +REPOS=`./githubapi-get_json.sh $GITHUB_TOKEN /orgs/xmidt-org/repos|jq -r ".[] | .name "` + +WRITER_ID=3307585 +ADMIN_ID=3322421 + +for name in $REPOS +do + echo "Updating $name" + curl -X PUT -H "Authorization: token $GITHUB_TOKEN" -s -H "Accept: application/vnd.github.hellcat-preview+json" https://api.github.com/teams/$WRITER_ID/repos/xmidt-org/$name -d '{"permission": "push" }' + curl -X PUT -H "Authorization: token $GITHUB_TOKEN" -s -H "Accept: application/vnd.github.hellcat-preview+json" https://api.github.com/teams/$ADMIN_ID/repos/xmidt-org/$name -d '{"permission": "admin" }' +done diff --git a/org-scripts/githubapi-get_json.sh b/org-scripts/githubapi-get_json.sh new file mode 100755 index 0000000..d9ef8a0 --- /dev/null +++ b/org-scripts/githubapi-get_json.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -e + +if [ ${#@} -lt 2 ]; then + echo "usage: $0 [your github token] [REST expression]" + exit 1; +fi + +GITHUB_TOKEN=$1 +GITHUB_API_REST=$2 + +GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json" + +temp=`basename $0` +TMPFILE=`mktemp /tmp/${temp}.XXXXXX` || exit 1 + +function rest_call { + curl -s $1 -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | sed -e 's/^\[$//g' -e 's/^\]$/,/g' >> $TMPFILE +} + +# single page result-s (no pagination), have no Link: section, the grep result is empty +last_page=`curl -s -I "https://api.github.com${GITHUB_API_REST}" -H "${GITHUB_API_HEADER_ACCEPT}" -H "Authorization: token $GITHUB_TOKEN" | grep '^Link:' | sed -e 's/^Link:.*page=//g' -e 's/>.*$//g'` + +# does this result use pagination? +if [ -z "$last_page" ]; then + # no - this result has only one page + rest_call "https://api.github.com${GITHUB_API_REST}" +else + # yes - this result is on multiple pages + for p in `seq 1 $last_page`; do + rest_call "https://api.github.com${GITHUB_API_REST}?page=$p" + done +fi + +line_counter=`wc -l $TMPFILE | sed -e 's/[/a-zA-Z].*$//g'` +#echo "TMPFILE:$TMPFILE" +#echo "line_counter:$line_counter" +echo "[" +head -n $(($line_counter - 1)) $TMPFILE +echo "]"