Remove build dependence with python-support
[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 use Compress::Zlib;
39
40 sub new {
41   my $self = {};
42   bless $self, shift;
43   return $self;
44 }
45
46 sub pattern_match
47 {
48   my ($event_str, $config_str) = @_;
49   my $p;
50
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/) {
55       return 1;
56     }
57   }
58   return 0;
59 }
60
61 sub notify() {
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;
66
67   my $args = {};
68
69   $type = "UNKNOWN" unless $type;
70
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;
76   if ($paramRef) {
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};
83     }
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";
89       return;
90     }
91     if (@BSConfig::incl_patterns &&
92       !pattern_match($joined_event_str, \@BSConfig::incl_patterns)) {
93       print "notify: incl_pattern returns FALSE, return\n";
94       return;
95     }
96   }
97   print "notify: ok by filters to trigger Jenkins\n";
98   my $jenkinsuri = "$BSConfig::jenkinsserver/$BSConfig::jenkinsjob";
99   my $param = {
100     'request' => 'POST',
101     'uri' => $jenkinsuri,
102     'timeout' => 60,
103     'maxredirects' => 1,
104   };
105
106   my $project;
107   if (defined $args->{'project'}){
108       $project = $args->{'project'};
109   }
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..."
116   }else{
117       warn("No project name found in events");
118       return;
119   }
120
121   my @para = ("project=$project",
122               "event_type=$type",
123               "para=" . encode_base64(compress(encode_json($args)),''));
124   print "notify: trigger Jenkins uri=[$jenkinsuri] para=[@para]\n";
125   eval {
126     BSRPC::rpc( $param, undef, @para );
127   };
128   warn("Jenkins: $@") if $@;
129 }
130
131 1;