Skip to content

Commit 2d2e63c

Browse files
feat: add script to add all repository users to a project (#94)
1 parent 0aae642 commit 2d2e63c

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

gh-cli/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ See the [docs](https://cli.github.com/manual/gh_auth_login) for further informat
6969
7070
Adds all members of an organization to a team.
7171
72+
### add-all-users-in-repository-to-project.sh
73+
74+
Adds all users who are direct members of the repository to a ProjectV2 with a given role.
75+
76+
Requires: `./add-user-to-project.sh`
77+
7278
### add-branch-protection-status-checks.sh
7379

7480
Adds a status check to the branch protection status check contexts.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# Adds a all users in a repository to a ProjectV2
4+
5+
# needs: gh auth login -s project
6+
# needs: ./add-user-to-project.sh
7+
8+
function print_usage {
9+
echo "Usage: $0 <organization> <repository> <project-number> <role>"
10+
echo "Example: ./add-all-users-in-repository-to-project.sh joshjohanning-org my-repo 1234 WRITER"
11+
echo "Valid roles: ADMIN, WRITER, READER, NONE"
12+
exit 1
13+
}
14+
15+
if [ -z "$4" ]; then
16+
print_usage
17+
fi
18+
19+
organization="$1"
20+
repository="$2"
21+
project_number="$3"
22+
role=$(echo "$4" | tr '[:lower:]' '[:upper:]')
23+
24+
case "$role" in
25+
"ADMIN" | "WRITER" | "READER" | "NONE")
26+
;;
27+
*)
28+
print_usage
29+
;;
30+
esac
31+
32+
# get list of directly added users in a repository
33+
users=$(gh api graphql --paginate -f owner="$organization" -f repo="$repository" -f query='
34+
query($owner: String!, $repo: String!, $endCursor:String) {
35+
repository(owner: $owner, name: $repo) {
36+
collaborators(first: 100, affiliation: DIRECT, after:$endCursor) {
37+
edges {
38+
node {
39+
login
40+
id
41+
}
42+
}
43+
pageInfo {
44+
hasNextPage
45+
endCursor
46+
}
47+
}
48+
}
49+
}
50+
' --jq '.data.repository.collaborators.edges[].node')
51+
52+
# for each user, add them to the project
53+
for user in $users; do
54+
user_login=$(echo $user | jq -r '.login')
55+
echo "Adding $user_login to project $project_id with role $role"
56+
./add-user-to-project.sh $organization $repository $project_number $user_login $role
57+
done

0 commit comments

Comments
 (0)