This repository was archived by the owner on Jul 21, 2021. It is now read-only.
forked from globau/bmo-admin-scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbulk-edit-user.pl
executable file
·130 lines (115 loc) · 3.35 KB
/
bulk-edit-user.pl
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
#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use 5.10.0;
$| = 1;
# opens editusers.cgi for the specified email addresses. this avoids the need
# to search for the email address to find the user_id, then click to edit, and
# is most useful when editing multiple users.
#
# syntax:
# bulk-user-edit
# without args, you'll be prompted to paste lines which contain the user
# to edit.
# bulk-user-edit [dev|stage]
# if you specify "dev" or "stage" as an arg, the development or staging
# servers will be used instead of production.
# bulk-user-edit [email protected]
# passing one or more address on the command line will work on just those
# addresses.
use JSON;
use List::MoreUtils 'uniq';
use LWP::UserAgent;
use HTTP::Request;
use URI::Escape;
use Sys::Hostname;
my $url_base = 'https://bugzilla.mozilla.org';
$url_base = 'https://bugzilla-dev.allizom.org' if grep { /\bdev\b/ } @ARGV;
$url_base = 'https://bugzilla.allizom.org' if grep { /\bstage\b/ } @ARGV;
my @list;
foreach my $arg (@ARGV) {
push @list, $arg if $arg =~ /\@/;
}
if (!@list) {
undef @ARGV;
print "paste lines with email addresses, ^D to finish\n";
@list = <>;
chomp(@list);
}
my @tests = (
'<([^>]+)>',
'\(([^\)]+)\)',
'\[([^\]]+)\]',
'\b(\S+\@.+\..+)$',
);
my @logins;
foreach (@list) {
s/(^\s+|\s+$)//g;
next if $_ eq '';
next unless /\@/;
TEST: foreach my $test (@tests) {
while ($_ =~ /$test/g) {
my $login = $1;
$login =~ s/^(\S+)\s.*/$1/;
$login =~ s/^mailto://i;
$login =~ s/^[,\.\(\[\<]+//;
$login =~ s/[,\.\)\]\>]+$//;
next if $login !~ /^\S+\@\S+/ || $login =~ /\s/;
push @logins, $login;
print "<$login>\n";
last TEST;
}
}
}
die "no logins found\n" unless @logins;
@logins = map { lc } uniq sort @logins;
print "\nchecking " . scalar(@logins) . " user" . (scalar(@logins) == 1 ? '' : 's') . "\n";
my ($code, $users) = get_users(@logins);
if ($code == 400) {
foreach my $login (@logins) {
my (undef, $user) = get_users($login);
next unless $user;
push @$users, $user->[0];
}
}
my @urls;
foreach my $login (@logins) {
my $url;
foreach my $rh (@$users) {
next unless lc($rh->{name}) eq $login;
$url = "$url_base/editusers.cgi?action=edit&userid=" . $rh->{id};
last;
}
if (!$url) {
print "failed to find login $login\n";
} else {
push @urls, $url;
}
}
exit unless @urls;
print "\nopening url" . (scalar(@urls) == 1 ? '' : 's') . "..\n";
while (my $url = pop @urls) {
if (hostname() eq 'bz.glob.com.au') {
system qq#ssh byron\@mac "open '$url'"#;
} else {
system "open '$url'";
}
sleep(1) if @urls;
}
sub get_users {
my (@logins) = @_;
state $ua //= LWP::UserAgent->new();
my $request = HTTP::Request->new(GET => $url_base . '/rest/user?include_fields=id,name&names=' . join('&names=', map { uri_escape($_) } @logins));
my $response = $ua->request($request);
my $code = $response->code;
my $users;
my $data;
eval {
$data = decode_json($response->decoded_content);
};
die "malform response ($@):\n" . $response->message . "\n" if $@;
if ($code == 200) {
$users = $data->{users};
}
return ($code, $users);
}