forked from joshjohanning/github-misc-scripts
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadd-user-to-project.sh
More file actions
executable file
·129 lines (107 loc) · 3.32 KB
/
Copy pathadd-user-to-project.sh
File metadata and controls
executable file
·129 lines (107 loc) · 3.32 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash
# Adds a user to a ProjectV2
# needs: gh auth login -s project
function print_usage {
echo "Usage: $0 <organization> <repository> <project-number> <role> <user>"
echo "Example: ./add-user-to-project.sh joshjohanning-org my-repo 1234 ADMIN joshjohanning"
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:]')
user="$5"
case "$role" in
"ADMIN" | "WRITER" | "READER" | "NONE")
;;
*)
print_usage
;;
esac
# get project id
project_response=$(gh api graphql --paginate -f organization="$organization" -f repository="$repository" -f query='
query ($organization: String!, $repository: String!) {
organization (login: $organization) {
repository (name: $repository) {
name
projectsV2 (first: 100) {
nodes {
title
id
url
number
}
}
}
}
}
' 2>&1)
# Check if the response contains scope error
if echo "$project_response" | grep -q "INSUFFICIENT_SCOPES\|read:project"; then
echo "Error: Insufficient permissions to access projects."
echo "You may need to authorize to projects; i.e.: gh auth login -s project"
exit 1
fi
project_id=$(echo "$project_response" | jq -r ".data.organization.repository.projectsV2.nodes[] | select(.number == $project_number) | .id")
if [ -z "$project_id" ] || [ "$project_id" = "null" ]; then
echo "Error: Could not find project with number $project_number in $organization/$repository"
exit 1
fi
echo "project_id: $project_id"
# get user id
user_response=$(gh api graphql -H X-Github-Next-Global-ID:1 -f user="$user" -f query='
query ($user: String!)
{ user(login: $user) {
login
name
id
}
}
' 2>&1)
# Check for user API errors
if echo "$user_response" | grep -q "error\|Error"; then
echo "Error: Could not find user $user"
exit 1
fi
user_id=$(echo "$user_response" | jq -r '.data.user.id')
if [ -z "$user_id" ] || [ "$user_id" = "null" ]; then
echo "Error: Could not find user $user"
exit 1
fi
echo "user_id: $user_id"
# get epoch time
epoch=$(date +%s)
# create request.json
cat << EOF > request-$epoch.json
{
"query": "mutation(\$projectId: ID!, \$collaborators: [ProjectV2Collaborator!]!) { updateProjectV2Collaborators(input: {projectId: \$projectId, collaborators: \$collaborators}) { clientMutationId, collaborators (first:100) { nodes { ... on User { login } } } } }",
"variables": {
"projectId": "$project_id",
"collaborators": [
{
"userId": "$user_id",
"role": "$role"
}
]
}
}
EOF
token=$(gh auth token)
# couldn't get this to work with gh api, had an error trying to pass in the object, so using curl
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)
# Check for errors in the final response
if echo "$response" | grep -q '"status": "400"\|"errors"'; then
echo "Error updating project collaborators:"
echo "$response"
echo ""
echo "You may need to authorize to projects; i.e.: gh auth login -s project"
rm request-$epoch.json
exit 1
fi
echo "Successfully added $user to project with role $role"
echo "$response"
rm request-$epoch.json