Skip to content

Commit e395573

Browse files
refactor!: standardize parameter order for invite/add scripts and improve error handling (#98)
BREAKING CHANGE: Parameter order changed in add-collaborator-to-repository.sh and add-user-to-project.sh - BREAKING: Change parameter order in add-collaborator-to-repository.sh (role before login) - BREAKING: Change parameter order in add-user-to-project.sh (role before user) - Add comprehensive error handling to add-user-to-project.sh for API failures - Add invite-user-to-repository.sh wrapper script for better discoverability
1 parent 7bec0a7 commit e395573

File tree

4 files changed

+73
-13
lines changed

4 files changed

+73
-13
lines changed

gh-cli/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ Adds a user to a ProjectV2 with a given role
143143
Example usage:
144144

145145
```shell
146-
./add-user-to-project.sh <organization> <repository> <project-number> <user> <role>
146+
./add-user-to-project.sh <organization> <repository> <project-number> <role> <user>
147147
./add-user-to-project.sh joshjohanning-org my-repo 1234 joshjohanning ADMIN"
148148
```
149149

@@ -1148,6 +1148,10 @@ Example output:
11481148
],
11491149
```
11501150

1151+
### invite-user-to-repository.sh
1152+
1153+
Calls the `./add-collaborator-to-repository.sh` script to add a user to a repository (this is a wrapper script as an alias since `invite == add`.
1154+
11511155
### invite-users-to-organization-from-list.sh
11521156

11531157
Adds users to an organization team from a CSV input list.

gh-cli/add-collaborator-to-repository.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
#!/bin/bash
22

3+
# adds (or invites) a collaborator to a repository
4+
35
permissions=("pull" "triage" "push" "maintain" "admin")
6+
# corresponds to read, triage, write, maintain, admin
47

58
if [ $# -ne 4 ]
69
then
7-
echo "usage: $(basename $0) <org> <repo> <login> <role>"
10+
echo "usage: $(basename $0) <org> <repo> <role> <login>"
811
exit 1
912
fi
1013

1114
org=$1
1215
repo=$2
13-
login=$3
14-
role=$4
16+
role=$3
17+
login=$4
1518

1619
if [[ ! " ${permissions[*]} " =~ ${role} ]]
1720
then

gh-cli/add-user-to-project.sh

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# needs: gh auth login -s project
66

77
function 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
}
@@ -18,8 +18,8 @@ fi
1818
organization="$1"
1919
repository="$2"
2020
project_number="$3"
21-
user="$4"
22-
role=$(echo "$5" | tr '[:lower:]' '[:upper:]')
21+
role=$(echo "$4" | tr '[:lower:]' '[:upper:]')
22+
user="$5"
2323

2424
case "$role" in
2525
"ADMIN" | "WRITER" | "READER" | "NONE")
@@ -30,7 +30,7 @@ case "$role" in
3030
esac
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

5165
echo "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='
5569
query ($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

6491
echo "user_id: $user_id"
6592

@@ -84,6 +111,19 @@ EOF
84111
token=$(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

89129
rm request-$epoch.json
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# invites a collaborator to a repository
4+
# this is a wrapper script that calls add-collaborator-to-repository.sh
5+
6+
# sample:
7+
# ./invite-user-to-repository.sh my-org my-repo push joshjohanning
8+
9+
# Get the directory where this script is located
10+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11+
12+
# Call the add-collaborator-to-repository.sh script with all arguments
13+
exec "$SCRIPT_DIR/add-collaborator-to-repository.sh" "$@"

0 commit comments

Comments
 (0)