Include BuildMonitorDB.pm in the spec file.
[services/obs-event-plugin.git] / notify_jenkins.pm
1 # Copyright (C) 2012, Intel Corporation.
2 #
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.
6
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
10 # for more details.
11
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.
15
16 ################################################################
17 #
18 # Module to talk to Jenkins
19 #
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:.*");
30
31 package notify_jenkins;
32
33 use BSRPC;
34 use BSConfig;
35 use MIME::Base64;
36 use strict;
37 use JSON::XS;
38
39 sub new {
40   my $self = {};
41   bless $self, shift;
42   return $self;
43 }
44
45 sub pattern_match
46 {
47   my ($event_str, $config_str) = @_;
48   my $p;
49
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/) {
54       return 1;
55     }
56   }
57   return 0;
58 }
59
60 sub notify() {
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;
65
66   my $args = {};
67
68   $type = "UNKNOWN" unless $type;
69
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;
75   if ($paramRef) {
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};
82     }
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";
88       return;
89     }
90     if (@BSConfig::incl_patterns &&
91       !pattern_match($joined_event_str, \@BSConfig::incl_patterns)) {
92       print "notify: incl_pattern returns FALSE, return\n";
93       return;
94     }
95   }
96   print "notify: ok by filters to trigger Jenkins\n";
97   my $jenkinsuri = "$BSConfig::jenkinsserver/$BSConfig::jenkinsjob";
98   my $param = {
99     'request' => 'POST',
100     'uri' => $jenkinsuri,
101     'timeout' => 60,
102     'maxredirects' => 1,
103   };
104
105   my $project;
106   if (defined $args->{'project'}){
107       $project = $args->{'project'};
108   }
109   elsif (defined $args->{'targetproject'}) {
110       $project = $args->{'targetproject'};
111   }else{
112       warn("No project name found in events");
113       return;
114   }
115
116   my @para = ("project=$project",
117               "event_type=$type",
118               "para=" . encode_base64(encode_json($args),''));
119   print "notify: trigger Jenkins uri=[$jenkinsuri] para=[@para]\n";
120   eval {
121     BSRPC::rpc( $param, undef, @para );
122   };
123   warn("Jenkins: $@") if $@;
124 }
125
126 1;