-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogle-group-request.sh
More file actions
executable file
·246 lines (209 loc) · 7.46 KB
/
Copy pathgoogle-group-request.sh
File metadata and controls
executable file
·246 lines (209 loc) · 7.46 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env bash
# Description: Gather and resolve Google Group requests from RT
# Dependencies: gam, rt
# Written by scellef, 25 Mar 2017
### VARIABLES ###
IFS=',' # Set field separator to comma to allow for multiple owners/managers
rtUrl="https://rt.example.com/Ticket/Display.html?id="
groupUrl="https://groups.google.com/forum/#!forum/"
ticketQuery=("Subject LIKE '[Google Group]' AND Status = 'new'")
groupString="'Group Name:'"
ownerString="'Owner Username:'"
managerString=="'Manager Username'"
descString=="'Description:'"
### DOCUMENTATION ###
function usage {
cat << EOF
Usage: google-group-request.sh
google-group-request.sh -h
google-group-request.sh [-z] -a
google-group-request.sh [-z] -t TICKET [-t TICKET ...]
google-group-request.sh [-z] -n NAME -o OWNER [-o OWNER2 ...]
[-m MANAGER [-m MANAGER2 ...]] [-d 'DESCRIPTION']
Options:
-z Runs through script without making any changes (NOOP)
-a Queries RT for new Google Group requests and iterates through them,
interactively prompting for corrections
-t Specifies the RT ticket to gather information from and resolve
-n Specifies the name of the Google Group to create
-o Specifies the owner of the Google Group to create
-m Specifies the manager of the Google Group to create
-d Specifies the description of the Google Group to create
Notes:
NAME and OWNER must be set before a group can be created.
OWNER and MANAGER should be valid and active Google usernames.
DESCRIPTION has a 300-character limit and will be truncated accordingly
While in the group settings confirmation prompt, multiple owners or managers
may be specified in a comma-delimited list such as "larry,curly,moe".
EOF
exit
}
### HELPER FUNCTIONS ###
function error { IFS='\n' printf >&2 "[1;31mERROR: %s[0m\n" $* ;}
function success { IFS='\n' printf >&2 "[1;32mSUCCESS: %s[0m\n" $* ;}
function warning { IFS='\n' printf >&2 "[1;33mWARNING: %s[0m\n" $* ;}
function prompt { IFS='\n' printf >&2 "[1;36m%s[0m\n" $* ;}
function quit { prompt "Exiting..." ; exit 0 ;}
### PRIMARY FUNCTIONS ###
function check_dependencies {
command -v rt > /dev/null || error "'rt' not found in your PATH."
command -v gam > /dev/null || error "'gam' not found in your PATH."
}
function parse_arguments {
if [ $# -eq 0 ] ; then
prompt_for_info
elif [ "$1" == '-' ] ; then
error "Unknown option: $1" ; usage ; exit 2
elif [[ ! ($1 =~ "-") ]] ; then
error "Unknown option: $1" ; usage ; exit 2
else
while getopts ":t:n:o:m:d:ah" opt ; do
case $opt in
a) find_tickets ;;
t) ticketNum+=("$OPTARG") ;;
n) groupName="$OPTARG" ;;
o) groupOwner+=("$OPTARG") ;;
m) groupManager+=("$OPTARG") ;;
d) groupDesc="$OPTARG" ;;
h) usage ;;
z) dryRun="echo" ;;
\?) error "Unknown option: -$OPTARG" ; usage ; exit 2 ;;
:) error "-$OPTARG requires an argument." ; usage ; exit 2 ;;
esac
done
fi
shift $((OPTIND-1)) # Cleaning up arguments
if [ $ticketNum ] ; then
parse_ticket_info
else
confirm_group_info
fi
}
function prompt_for_info {
prompt 'Choose from the following options: '
PS3="Enter a selection [1-4]: "
select choice in 'Enter ticket' 'Find new requests' 'Manually create group' 'Exit' ; do
case $choice in
'Find new requests' ) find_tickets ; break ;;
'Enter ticket' ) read -p 'Enter ticket number: ' ticketNum ; break ;;
'Manually create group' ) confirm_group_info ; exit ;;
'Exit' ) exit ;;
esac
done
}
function find_tickets {
readarray -t ticketNum <<< "$(rt ls -f id "$ticketQuery" | tail -n +2)"
}
function confirm_group_info {
prompt 'Choose a field to update, or select an action: '
while [ "$confirmed" != "yes" ] ; do
PS3="Enter a selection [1-6]: "
select choice in \
"Group name: $groupName" \
"Owner(s): ${groupOwner[*]}" \
"Manager(s): ${groupManager[*]}" \
"Description: $groupDesc" \
"Create group" \
"Abort" ; do
case $choice in
"Group name: $groupName")
read -p 'Enter the Group Name: ' groupName ; break ;;
"Owner(s): ${groupOwner[*]}")
read -p 'Enter the Group Owner(s): ' -a groupOwner ; break ;;
"Manager(s): ${groupManager[*]}")
read -p 'Enter the Group Manager(s): ' -a groupManager ; break ;;
"Description: $groupDesc")
IFS='\n' read -p 'Enter the Group Description: ' groupDesc ; break ;;
"Create group")
local confirmed="yes" ; break ;;
"Abort") quit ; exit ;;
esac
done
done
create_group
}
function check_group_info {
if [ -z $groupName ] ; then
error "Missing group name!" ; break
elif [ -z $groupOwner ] ; then
error "At least one owner must be specified!" ; break
fi
}
function parse_ticket_info {
for ticket in ${ticketNum[*]} ; do
if [ ! $ticket -gt 0 ]; then
error "'$ticket' is not a valid ticket number" ; break
fi
rt show $ticket > /tmp/$ticket.rt 2> /dev/null
grep -q '^Subject:.*\[Google Group\]' /tmp/$ticket.rt
if [ $? -ne 0 ] ; then
warning "RT #$ticket subject does not match; is this a Google Group request?"
fi
prompt
prompt "RT #${ticket}:"
prompt "${rtUrl}${ticket}"
prompt
groupName="$(grep $groupString /tmp/$ticket.rt | cut -f 2 -d ':' | tail -c+2)"
groupOwner="$(grep $ownerString /tmp/$ticket.rt | cut -f 2 -d ':' | tail -c+2)"
groupManager="$(grep $managerString /tmp/$ticket.rt | cut -f 2 -d ':' | tail -c+2)"
groupDesc="$(grep $descString /tmp/$ticket.rt | cut -f 2 -d ':' | tail -c+2)"
confirm_group_info
done
}
function create_group {
check_dependencies
check_group_info
$dryRun gam create group $groupName name $groupName
if [ $? -ne 0 ] ; then
error "Group creation failed!"
exit 128
fi
sleep 1
for owner in ${groupOwner[*]} ; do
$dryRun gam update group $groupName add owner $owner
if [ $? -ne 0 ] ; then
warning "Could not set '$owner' as owner of $groupName!"
errorOwner+=1
fi
sleep 1
done
for manager in ${groupManager[*]} ; do
$dryRun gam update group $groupName add manager $manager
if [ $? -ne 0 ] ; then
warning "Could not set '$manager' as manager of $groupName!"
errorManager+=1
fi
sleep 1
done
if [ ! -z "$groupDesc" ] ; then
IFS='\n' gam update group $groupName description ${groupDesc:0:300}
if [ $? -ne 0 ] ; then
warning "Could not set description on $groupName!"
errorDesc+=1
fi
fi
success "Group created: ${groupUrl}${groupName}"
if [ $errorOwner ] ; then
warning "Double-check group owner(s): ${groupOwner[*]}"
elif [ $errorManager ] ; then
warning "Double-check group manager(s): ${groupManager[*]}"
elif [ $errorDesc ] ; then
IFS='\n' warning "Double-check group description: '${groupDesc:0:300}'"
fi
if [ "$(echo ${!error*})" ] ; then
gam info group $groupName | grep -e ' \(name:\|owner:\|manager:\|description:\)'
fi
if [ $ticket ] ; then
resolve_ticket
fi
unset groupName groupOwner groupManager groupDesc
unset errorOwner errorManager errorDesc
}
function resolve_ticket {
$dryRun rt correspond $ticket -m - -s resolved 2> /dev/null << EOF
The '${groupName}' Google group has been created. You can access it by visiting the following URL:
${groupUrl}${groupName}
EOF
success "Ticket resolved: ${rtUrl}${ticket}"
}
parse_arguments $@