-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathadd-all-users-in-repository-to-project.sh
executable file
·57 lines (49 loc) · 1.39 KB
/
add-all-users-in-repository-to-project.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
53
54
55
56
57
#!/bin/bash
# Adds a all users in a repository to a ProjectV2
# needs: gh auth login -s project
# needs: ./add-user-to-project.sh
function print_usage {
echo "Usage: $0 <organization> <repository> <project-number> <role>"
echo "Example: ./add-all-users-in-repository-to-project.sh joshjohanning-org my-repo 1234 WRITER"
echo "Valid roles: ADMIN, WRITER, READER, NONE"
exit 1
}
if [ -z "$4" ]; then
print_usage
fi
organization="$1"
repository="$2"
project_number="$3"
role=$(echo "$4" | tr '[:lower:]' '[:upper:]')
case "$role" in
"ADMIN" | "WRITER" | "READER" | "NONE")
;;
*)
print_usage
;;
esac
# get list of directly added users in a repository
users=$(gh api graphql --paginate -f owner="$organization" -f repo="$repository" -f query='
query($owner: String!, $repo: String!, $endCursor:String) {
repository(owner: $owner, name: $repo) {
collaborators(first: 100, affiliation: DIRECT, after:$endCursor) {
edges {
node {
login
id
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
' --jq '.data.repository.collaborators.edges[].node')
# for each user, add them to the project
for user in $users; do
user_login=$(echo $user | jq -r '.login')
echo "Adding $user_login to project $project_id with role $role"
./add-user-to-project.sh $organization $repository $project_number $user_login $role
done