Skip to content

Commit 68e7be0

Browse files
feat: update add-user-to-repository script to include existing invitation check (#104)
* refactor!: swap usage order of parameters in add-team-to-repository script * feat: update add-user-to-repository script to include existing invitation check * fix: correct parameter order in usage examples and add existing invitation check for user addition * feat: only re-send expired invitations
1 parent 0df0e78 commit 68e7be0

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

gh-cli/README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Adds a team to a repository with a given permission level
133133
Example usage:
134134

135135
```shell
136-
./add-team-to-repository.sh joshjohanning-org my-repo my-team push"
136+
./add-team-to-repository.sh joshjohanning-org my-repo push my-team"
137137
```
138138

139139
### add-user-to-project.sh
@@ -144,7 +144,7 @@ Example usage:
144144

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

150150
Example roles:
@@ -161,7 +161,14 @@ Adds a user to a repository with a given permission
161161
Example usage:
162162

163163
```shell
164-
./add-user-to-repository.sh joshjohanning-org my-repo joshjohanning write"
164+
./add-user-to-repository.sh joshjohanning-org my-repo write joshjohanning"
165+
```
166+
167+
This also will attempt to check if there is an existing invitation for this user pending, and if it's expired, cancel it. This can be opted out of by passing in `true` as the 5th parameter, such as
168+
169+
```shell
170+
# don't check to see if existing invite is present; i.e. if adding existing organization user to repository
171+
./add-user-to-repository.sh joshjohanning-org my-repo write joshjohanning true"
165172
```
166173

167174
### add-user-to-team.sh
@@ -1150,7 +1157,7 @@ Example output:
11501157

11511158
### invite-user-to-repository.sh
11521159

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`.
1160+
Calls the `./add-user-to-repository.sh` script to add a user to a repository (this is a wrapper script as an alias since `invite == add`).
11541161

11551162
### invite-users-to-organization-from-list.sh
11561163

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# Adds a team to a repo
44

55
function print_usage {
6-
echo "Usage: $0 <org> <repo> <team_slug> <role>"
7-
echo "Example: ./add-team-to-repository.sh joshjohanning-org my-repo my-team push"
6+
echo "Usage: $0 <org> <repo> <role> <team_slug>"
7+
echo "Example: ./add-team-to-repository.sh joshjohanning-org my-repo push my-team"
88
echo "Valid roles: admin, maintain, push (write), triage, pull (read)"
99
exit 1
1010
}
@@ -15,8 +15,8 @@ fi
1515

1616
org=$1
1717
repo=$2
18-
team=$3
19-
permission=$(echo "$4" | tr '[:upper:]' '[:lower:]')
18+
permission=$(echo "$3" | tr '[:upper:]' '[:lower:]')
19+
team=$4
2020

2121
case "$permission" in
2222
"admin" | "maintain" | "push" | "triage" | "pull")

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
# Adds a user to a repo
44

55
function print_usage {
6-
echo "Usage: $0 <org> <repo> <user> <role>"
7-
echo "Example: ./add-user-to-repository.sh joshjohanning-org my-repo joshjohanning ADMIN"
6+
echo "Usage: $0 <org> <repo> <role> <user> [skip_invite_check]"
7+
echo "Example: ./add-user-to-repository.sh joshjohanning-org my-repo ADMIN joshjohanning"
8+
echo "Example: ./add-user-to-repository.sh joshjohanning-org my-repo ADMIN joshjohanning true"
89
echo "Valid roles: ADMIN, MAINTAIN, WRITE, TRIAGE, READ"
10+
echo "skip_invite_check: true to skip checking/canceling pending invitations, defaults to false"
911
exit 1
1012
}
1113

@@ -15,8 +17,9 @@ fi
1517

1618
org="$1"
1719
repo="$2"
18-
user="$3"
19-
permission=$(echo "$4" | tr '[:lower:]' '[:upper:]')
20+
permission=$(echo "$3" | tr '[:lower:]' '[:upper:]')
21+
user="$4"
22+
skip_invite_check="${5:-false}"
2023

2124
case "$permission" in
2225
"ADMIN" | "MAINTAIN" | "WRITE" | "TRIAGE" | "READ")
@@ -26,4 +29,32 @@ case "$permission" in
2629
;;
2730
esac
2831

32+
# Check for existing pending invitations (unless skipped)
33+
if [ "$skip_invite_check" != "true" ]; then
34+
echo "Checking for existing invitations for $user..."
35+
invitation_data=$(gh api "/repos/$org/$repo/invitations" --jq ".[] | select(.invitee.login == \"$user\") | {id: .id, expired: .expired}" 2>/dev/null)
36+
37+
if [ -n "$invitation_data" ]; then
38+
invitation_id=$(echo "$invitation_data" | jq -r '.id')
39+
is_expired=$(echo "$invitation_data" | jq -r '.expired')
40+
41+
if [ "$is_expired" = "true" ]; then
42+
echo "Found expired invitation (ID: $invitation_id) for $user. Canceling it..."
43+
gh api -X DELETE "/repos/$org/$repo/invitations/$invitation_id"
44+
if [ $? -eq 0 ]; then
45+
echo "Successfully canceled expired invitation."
46+
else
47+
echo "Warning: Failed to cancel expired invitation. Proceeding anyway..."
48+
fi
49+
else
50+
echo "Found active invitation (ID: $invitation_id) for $user. Leaving it as is."
51+
echo "Skipping new invitation since an active one already exists."
52+
exit 0
53+
fi
54+
fi
55+
else
56+
echo "Skipping invitation check as requested..."
57+
fi
58+
59+
echo "Adding/inviting $user to $org/$repo with $permission permission..."
2960
gh api -X PUT /repos/$org/$repo/collaborators/$user -f permission=$permission

gh-cli/invite-user-to-repository.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1111

1212
# Call the add-collaborator-to-repository.sh script with all arguments
13-
exec "$SCRIPT_DIR/add-collaborator-to-repository.sh" "$@"
13+
exec "$SCRIPT_DIR/add-user-to-repository.sh" "$@"

0 commit comments

Comments
 (0)