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;
47 my ($event_str, $config_str) = @_;
50 print "pattern_match: event_str=[$event_str]\n";
51 for $p (@{$config_str}) {
52 print " config_str=[$p]...\n";
53 if ($event_str =~ /$p/) {
61 my ($self, $type, $paramRef ) = @_;
62 my (@event_str, @sorted_event_str, $joined_event_str);
63 die('No jenkinssever configured in BSConfig!') unless $BSConfig::jenkinsserver;
64 die('No jenkinsjob configured in BSConfig!' ) unless $BSConfig::jenkinsjob;
68 $type = "UNKNOWN" unless $type;
70 @event_str = "type:$type";
71 # prepend something BS specific
72 my $prefix = $BSConfig::jenkinsnamespace|| "OBS";
73 $type = "${prefix}_$type";
74 $args->{'event_type'} = $type;
76 print "notify: type=[$type]\n";
77 for my $key (sort keys %$paramRef) {
78 next if ref $paramRef->{$key};
79 @event_str = (@event_str , "$key:$paramRef->{$key}");
80 print " key=[$key] value=[$paramRef->{$key}]\n";
81 $args->{$key} = $paramRef->{$key} if defined $paramRef->{$key};
83 @sorted_event_str = sort @event_str;
84 $joined_event_str = join(' ', @sorted_event_str);
85 if (@BSConfig::excl_patterns &&
86 pattern_match($joined_event_str, \@BSConfig::excl_patterns)) {
87 print "notify: excl_pattern returns TRUE, return\n";
90 if (@BSConfig::incl_patterns &&
91 !pattern_match($joined_event_str, \@BSConfig::incl_patterns)) {
92 print "notify: incl_pattern returns FALSE, return\n";
96 print "notify: ok by filters to trigger Jenkins\n";
97 my $jenkinsuri = "$BSConfig::jenkinsserver/$BSConfig::jenkinsjob";
100 'uri' => $jenkinsuri,
106 if (defined $args->{'project'}){
107 $project = $args->{'project'};
109 elsif (defined $args->{'targetproject'}) {
110 $project = $args->{'targetproject'};
112 warn("No project name found in events");
116 my @para = ("project=$project",
118 "para=" . encode_base64(encode_json($args),''));
119 print "notify: trigger Jenkins uri=[$jenkinsuri] para=[@para]\n";
121 BSRPC::rpc( $param, undef, @para );
123 warn("Jenkins: $@") if $@;