-
Notifications
You must be signed in to change notification settings - Fork 0
/
gerrit_to_bugzilla.pl
151 lines (121 loc) · 4.29 KB
/
gerrit_to_bugzilla.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/perl -w
#*******************************************************************************
#* Copyright (c) 2014-2015 Eclipse Foundation.
#* All rights reserved. This program and the accompanying materials
#* are made available under the terms of the Eclipse Public License v1.0
#* which accompanies this distribution, and is available at
#* http://www.eclipse.org/legal/epl-v10.html
#*
#* Contributors:
#* Denis Roy (Eclipse Foundation) - Initial implementation
#*******************************************************************************/
# Use this script to process incoming Gerrit change emails, post to Bugzilla
# Your alias file could look like this:
# gerrit: root,"|/path/to/gerrit_to_bugzilla.pl"
#
#
# The output will be a file compatible with Bugzilla's email_in.pl interface
# If your mail and Bugzilla servers are on the same host, you can probably
# omit the creation of temp files and instead pipe directly to email_in.pl
$hostname = `hostname`;
$hostname =~ s/\n//;
my $basepath = "/path/to/temp/gerrit_to_bugzilla";
my $logfile = $basepath . "/gerrit_to_bugzilla.log";
my $cgit_base = "http://git.eclipse.org/c/";
umask '002';
# Gerrit fields
my $message_type = "";
my $subject = "";
my $change_id = "";
my $change_url = "";
my $commit_id = "";
my $gerrit_project = "";
my $gerrit_branch = "";
my $bug_id = 0;
## subs
sub getLogHeader() {
return getNow() . " " . $hostname . "[" . $$ . "] ";
}
1;
sub getNow() {
my $rValue = "";
# Build local date
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if (length($min) == 1) {
$min = "0" . $min;
}
if (length($mon) == 1) {
$mon = "0" . $mon;
}
if (length($mday) == 1) {
$mday = "0" . $mday;
}
if (length($sec) == 1) {
$sec = "0" . $sec;
}
$mon++;
$year = $year + 1900;
$rValue = sprintf("%s-%s-%s %s:%s:%s", $year, $mon, $mday, $hour, $min, $sec);
return $rValue;
}
1;
## main
# Read incoming message.
while (<STDIN>) {
if($_ =~ m/^X-Gerrit-MessageType: ([a-zA-Z0-9]+)/) {
$message_type = $1;
}
if($_ =~ m/^Subject: (.*)/) {
$subject = $1;
}
if($_ =~ m/^X-Gerrit-Change-Id: ([a-zA-Z0-9]+)/) {
$change_id = $1;
}
if($_ =~ m/^X-Gerrit-ChangeURL: <(.*)>/) {
$change_url = $1;
}
if($_ =~ m/^X-Gerrit-Commit: ([a-zA-Z0-9]+)/) {
$commit_id = $1;
}
if($_ =~ m/^Gerrit-Project: ([a-zA-Z0-9\/\._-]+)/) {
$gerrit_project = $1;
}
if($subject =~ m/([Bb]ug:?\s*#?)(\d+)/) {
$bug_id = $2;
}
if($_ =~ m/^Gerrit-Branch: ([a-zA-Z0-9\/\._-]+)/) {
$gerrit_branch = $1;
}
}
if($bug_id > 0 && ($message_type eq "newchange" || $message_type eq "merged" || $message_type eq "newpatchset")) {
# fabricate email_in
my $bug_email = "From: genie\@eclipse.org\n";
$bug_email .= "Subject: [Bug $bug_id]\n";
$bug_email .= "\n";
$bug_email .= "\@id = $bug_id\n";
$bug_email .= "\@see_also = $change_url\n";
if($message_type eq "newchange") {
$bug_email .= "\n";
$bug_email .= "New Gerrit change created: $change_url\n";
}
if($message_type eq "merged") {
my $cgit_url = $cgit_base . $gerrit_project . ".git/commit/?id=" . $commit_id;
$bug_email .= "\@see_also = $cgit_url\n";
$bug_email .= "\n";
$bug_email .= "Gerrit change $change_url was merged to [$gerrit_branch].\n";
$bug_email .= "Commit: $cgit_url\n";
}
if($message_type eq "newpatchset") {
# do nothing. We just want to re-add the "see-also" field, which will do nothing if it's already there
}
$bug_email .= "";
# Send to STDOUT if you with to pipe it directly to email_in.pl
my $bugzilla_email_file = $basepath . "/bugmail.bug$bug_id.$$.txt";
open(FILE,">$bugzilla_email_file") || die("Cannot Open bugzilla email file");
print FILE $bug_email;
close(FILE);
}
open(FILE,">>$logfile") || die("Cannot Open File");
print FILE getLogHeader() . "Change [$change_id] type [$message_type] at url [$change_url] commit_id [$commit_id] with subject [$subject] and bug_id [$bug_id] for gerrit project [$gerrit_project]\n";
close(FILE);
exit 0;