-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-to-project.sh
executable file
·139 lines (115 loc) · 3.65 KB
/
add-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
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
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
# if no agruments are provided, print usage
if [ -z "$1" ]; then
echo "Usage: add-to-project.sh <org_or_user>/<project_number> <search_query>"
echo "For url https://github.com/users/ekroon/projects/1, <org_or_user>/<project_number> is: ekroon/1"
echo "Example: add-to-project.sh ekroon/1 1 is:open is:issue"
echo "Example: add-to-project.sh ekroon/1 1 is:open is:pr"
exit 1
fi
# if gh is not install display message to install it
if ! command -v gh &> /dev/null; then
echo "gh could not be found"
echo "More information here: https://github.com/cli/cli#installation"
exit 1
fi
# check if jq is installed
if ! command -v jq &> /dev/null; then
echo "jq could not be found"
echo "More information here: https://stedolan.github.io/jq/download/"
exit 1
fi
# check if first argument is in the correct format "org_or_user/project_number"
if ! [[ "$1" =~ ^[a-zA-Z0-9_-]+/[0-9]+$ ]]; then
echo "Invalid project format"
echo "Example: cli/1"
exit 1
fi
# Get first argument of script and split on '/' to get the project name and number
org_or_user=$(echo "$1" | cut -d'/' -f1)
project_number=$(echo "$1" | cut -d'/' -f2)
# capture the rest of the arguments to the program in a string
search="${*:2}"
# exit if search is empty
if [ -z "$search" ]; then
echo "No search query provided"
exit 1
fi
user_query=$(cat <<EOF
query(\$login: String!, \$project_number: Int!) {
user(login: \$login) {
projectV2(number: \$project_number) {
id
}
}
}
EOF
)
organization_query=$(cat <<EOF
query(\$login: String!, \$project_number: Int!) {
organization(login: \$login) {
projectV2(number: \$project_number) {
id
}
}
}
EOF
)
project_id=$(gh api graphql -F login="$org_or_user" -F project_number="$project_number" -f query="$user_query" 2> /dev/null | jq -r '.data.user.projectV2.id')
if [ "$project_id" = "null" ]; then
project_id=$(gh api graphql -F login="$org_or_user" -F project_number="$project_number" -f query="$organization_query" 2> /dev/null | jq -r '.data.organization.projectV2.id')
fi
# exit if project_id is still null
if [ "$project_id" = "null" ]; then
echo "Project not found"
exit 1
fi
# add -project to $search query
search="$search -project:$org_or_user/$project_number"
# add var to keep track of number of issues added
added=0
while true; do
# get the issues and node_ids out the results at 'items[].node_id' and split on whitespace in result
result=$(gh api -X GET search/issues -f q="$search" | jq -r '.items[].node_id')
# exit if result is empty
if [ -z "$result" ]; then
if [ "$added" -eq 0 ]; then
echo "No results found"
else
echo "Added $added issues"
fi
exit 1
fi
# create template for graphql query from node_id
mutations=""
counter=0
for node_id in $result; do
# increment counter
counter=$((counter+1))
# crate alias for mutation based on counter as string
alias_node_id="mutation_$counter"
mutations+=$(cat <<EOF
$alias_node_id: addProjectV2ItemById(input: {contentId: "$node_id", projectId: "$project_id"}) {
clientMutationId
item {
content {
... on Issue {
title
}
... on PullRequest {
title
}
}
}
}
EOF
)
done
result=$(gh api graphql -f query="mutation { $mutations }")
titles=$(echo "$result" | jq -r '.data | to_entries[] | .value.item.content.title')
number_of_titles=$(echo "$titles" | wc -l)
added=$((added+number_of_titles))
# every title is on a new line
# print it surrounded with quotes and with "Added " prefixed
echo "$titles" | sed 's/^/"/' | sed 's/$/"/' | sed 's/^/Added /'
done