-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathget-team-id.sh
executable file
·52 lines (43 loc) · 1.42 KB
/
get-team-id.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#! /bin/bash
# Get team ID from name
#
# Uses the GitHub "List teams" API feature
# http://developer.github.com/v3/orgs/teams/#list-teams
#
# Usage:
# get-team-id ORG_NAME TEAM_NAME AUTH_TOKEN
#
# Params:
# ORG_NAME - GitHub orgnaization name
# TEAM_NAME - Name of the team
# AUTH_TOKEN - Your GitHub OAuth token. Requires `read:org` scope. See http://developer.github.com/v3/oauth/#scopes
# Sanity check: Make sure we have the appropriate number of arguments
if [ $# -ne 3 ]; then
echo "Error: Requires exactly three arguments: The organization name, the team name, and your OAuth token."
echo
exit 1;
fi
# Module constants
API_BASE="https://api.github.com"
API_VER_ACCEPTS="application/vnd.github.v3+json"
# Collect cmd args
ORG_NAME=$1
TEAM_NAME=$2
AUTH_TOKEN=$3
# Construct curl command
cmd="curl -s -i -X GET -u $AUTH_TOKEN:x-oauth-basic -d '' -H 'Accepts:$API_VER_ACCEPTS' $API_BASE/orgs/$ORG_NAME/teams"
# Capture stdout, exit on errors
cmd_out="$($cmd || exit 1)"
# Sniff the HTTP response status
http_status="$(echo $cmd_out | cut -d ' ' -f 2)"
# If we did not get a 200, something went wrong. Exit with failure
if [ $http_status -ne 200 ]; then
echo
echo "Error getting team ID! Response header + body from GitHub:"
echo
echo "$cmd_out"
echo
exit 1
fi
# Parse and return the team ID
echo "$cmd_out" | grep -A1 "\"name\": \"$TEAM_NAME\"" | grep '"id":' | sed 's/^ *"id": \(.*\),/\1/g'