55# needs: gh auth login -s project
66
77function print_usage {
8- echo " Usage: $0 <organization> <repository> <project-number> <user > <role >"
9- echo " Example: ./add-user-to-project.sh joshjohanning-org my-repo 1234 joshjohanning ADMIN"
8+ echo " Usage: $0 <organization> <repository> <project-number> <role > <user >"
9+ echo " Example: ./add-user-to-project.sh joshjohanning-org my-repo 1234 ADMIN joshjohanning "
1010 echo " Valid roles: ADMIN, WRITER, READER, NONE"
1111 exit 1
1212}
1818organization=" $1 "
1919repository=" $2 "
2020project_number=" $3 "
21- user= " $4 "
22- role= $( echo " $5 " | tr ' [:lower:] ' ' [:upper:] ' )
21+ role= $( echo " $4 " | tr ' [:lower:] ' ' [:upper:] ' )
22+ user= " $5 "
2323
2424case " $role " in
2525 " ADMIN" | " WRITER" | " READER" | " NONE" )
@@ -30,7 +30,7 @@ case "$role" in
3030esac
3131
3232# get project id
33- project_id =$( gh api graphql --paginate -f organization=" $organization " -f repository=" $repository " -f query='
33+ project_response =$( gh api graphql --paginate -f organization=" $organization " -f repository=" $repository " -f query='
3434 query ($organization: String!, $repository: String!) {
3535 organization (login: $organization) {
3636 repository (name: $repository) {
@@ -46,20 +46,47 @@ project_id=$(gh api graphql --paginate -f organization="$organization" -f reposi
4646 }
4747 }
4848 }
49- ' --jq " .data.organization.repository.projectsV2.nodes[] | select(.number == $project_number ) | .id" )
49+ ' 2>&1 )
50+
51+ # Check if the response contains scope error
52+ if echo " $project_response " | grep -q " INSUFFICIENT_SCOPES\|read:project" ; then
53+ echo " Error: Insufficient permissions to access projects."
54+ echo " You may need to authorize to projects; i.e.: gh auth login -s project"
55+ exit 1
56+ fi
57+
58+ project_id=$( echo " $project_response " | jq -r " .data.organization.repository.projectsV2.nodes[] | select(.number == $project_number ) | .id" )
59+
60+ if [ -z " $project_id " ] || [ " $project_id " = " null" ]; then
61+ echo " Error: Could not find project with number $project_number in $organization /$repository "
62+ exit 1
63+ fi
5064
5165echo " project_id: $project_id "
5266
5367# get user id
54- user_id =$( gh api graphql -H X-Github-Next-Global-ID:1 -f user=" $user " -f query='
68+ user_response =$( gh api graphql -H X-Github-Next-Global-ID:1 -f user=" $user " -f query='
5569query ($user: String!)
5670 { user(login: $user) {
5771 login
5872 name
5973 id
6074 }
6175}
62- ' --jq ' .data.user.id' )
76+ ' 2>&1 )
77+
78+ # Check for user API errors
79+ if echo " $user_response " | grep -q " error\|Error" ; then
80+ echo " Error: Could not find user $user "
81+ exit 1
82+ fi
83+
84+ user_id=$( echo " $user_response " | jq -r ' .data.user.id' )
85+
86+ if [ -z " $user_id " ] || [ " $user_id " = " null" ]; then
87+ echo " Error: Could not find user $user "
88+ exit 1
89+ fi
6390
6491echo " user_id: $user_id "
6592
84111token=$( gh auth token)
85112
86113# couldn't get this to work with gh api, had an error trying to pass in the object, so using curl
87- curl -H " Authorization: bearer $token " -H " X-Github-Next-Global-ID:1" -H " Content-Type: application/json" -X POST -d @request-$epoch .json https://api.github.com/graphql
114+ response=$( curl -s -H " Authorization: bearer $token " -H " X-Github-Next-Global-ID:1" -H " Content-Type: application/json" -X POST -d @request-$epoch .json https://api.github.com/graphql)
115+
116+ # Check for errors in the final response
117+ if echo " $response " | grep -q ' "status": "400"\|"errors"' ; then
118+ echo " Error updating project collaborators:"
119+ echo " $response "
120+ echo " "
121+ echo " You may need to authorize to projects; i.e.: gh auth login -s project"
122+ rm request-$epoch .json
123+ exit 1
124+ fi
125+
126+ echo " Successfully added $user to project with role $role "
127+ echo " $response "
88128
89129rm request-$epoch .json
0 commit comments