1 # Copyright (C) 2012, Intel Corporation.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the terms and conditions of the GNU General Public License,
5 # version 2, as published by the Free Software Foundation.
7 # This program is distributed in the hope it will be useful, but WITHOUT
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 59 Temple
14 # Place - Suite 330, Boston, MA 02111-1307 USA.
16 ################################################################
18 # Module to talk to Jenkins
20 # Add the following lines to BSConfig.pm to enable this plugin
21 # our $jenkinsserver = "http://you.jenkins.server.com";
22 # our $jenkinsjob = "job/JOB_NAME/buildWithParameters";
23 # our $jenkinsnamespace = "OBS";
24 # our $notification_plugin = "notify_jenkins";
25 # Exclude and include rules for filtering events which can trigger Jenkins.
26 # Regexp pattern fields (project, type etc.) must be alphabetically sorted
27 # because OBS event string with all attributes gets sorted and then matched.
28 # our @excl_patterns = ("project:home:tester:.* type:REPO_PUBLISHED", "project:AnotherProjWeDontWantTriggered:.*");
29 # our @incl_patterns = ("project:ImportantProject:.* type:REPO_PUBLISHED", "project:Tizen:.*");
31 package notify_jenkins;
48 my ($event_str, $config_str) = @_;
51 print "pattern_match: event_str=[$event_str]\n";
52 for $p (@{$config_str}) {
53 print " config_str=[$p]...\n";
54 if ($event_str =~ /$p/) {
62 my ($self, $type, $paramRef ) = @_;
63 my (@event_str, @sorted_event_str, $joined_event_str);
64 die('No jenkinssever configured in BSConfig!') unless $BSConfig::jenkinsserver;
65 die('No jenkinsjob configured in BSConfig!' ) unless $BSConfig::jenkinsjob;
69 $type = "UNKNOWN" unless $type;
71 @event_str = "type:$type";
72 # prepend something BS specific
73 my $prefix = $BSConfig::jenkinsnamespace|| "OBS";
74 $type = "${prefix}_$type";
75 $args->{'event_type'} = $type;
77 print "notify: type=[$type]\n";
78 for my $key (sort keys %$paramRef) {
79 next if ref $paramRef->{$key};
80 @event_str = (@event_str , "$key:$paramRef->{$key}");
81 print " key=[$key] value=[$paramRef->{$key}]\n";
82 $args->{$key} = $paramRef->{$key} if defined $paramRef->{$key};
84 @sorted_event_str = sort @event_str;
85 $joined_event_str = join(' ', @sorted_event_str);
86 if (@BSConfig::excl_patterns &&
87 pattern_match($joined_event_str, \@BSConfig::excl_patterns)) {
88 print "notify: excl_pattern returns TRUE, return\n";
91 if (@BSConfig::incl_patterns &&
92 !pattern_match($joined_event_str, \@BSConfig::incl_patterns)) {
93 print "notify: incl_pattern returns FALSE, return\n";
97 print "notify: ok by filters to trigger Jenkins\n";
98 my $jenkinsuri = "$BSConfig::jenkinsserver/$BSConfig::jenkinsjob";
101 'uri' => $jenkinsuri,
107 if (defined $args->{'project'}){
108 $project = $args->{'project'};
110 elsif (defined $args->{'targetproject'}) {
111 $project = $args->{'targetproject'};
112 }elsif (defined $args->{'number'}
113 && defined $args->{'oldstate'} && defined $args->{'state'}
114 && defined $args->{'comment'} && defined $args->{'description'}) {
115 print "FIX: Force trigger..."
117 warn("No project name found in events");
121 my @para = ("project=$project",
123 "para=" . encode_base64(compress(encode_json($args)),''));
124 print "notify: trigger Jenkins uri=[$jenkinsuri] para=[@para]\n";
126 BSRPC::rpc( $param, undef, @para );
128 warn("Jenkins: $@") if $@;