patch proposition to add some functionnality to the Bugzilla 5.0.1 REST API

Laurent Facq laurent.facq at math.u-bordeaux1.fr
Fri Nov 20 14:01:53 UTC 2015


Hi !

i'm new to bugzilla dev.

i'm trying to hack bugzilla REST API to implement some features i need
like removing group or product, modifying rights groups have on a product.
i know my code is a bit quick & dirty... but may be anyway it can be a
step further :)

here's my patch against bugzilla 5.0.1 and an example in Ruby of how i
begin to use it.

L.

-- 
Laurent FACQ - +(33)5 4000 6956
Institut de Mathématiques de Bordeaux - Mathrice GDS 2754
-------------- next part --------------
diff --git a/Bugzilla/Group.pm b/Bugzilla/Group.pm
index 07b78e3..c7e2ebd 100644
--- a/Bugzilla/Group.pm
+++ b/Bugzilla/Group.pm
@@ -193,6 +193,8 @@ sub set_name        { $_[0]->set('name', $_[1]);        }
 sub set_user_regexp { $_[0]->set('userregexp', $_[1]);  }
 sub set_icon_url    { $_[0]->set('icon_url', $_[1]);    }
 
+sub set_action_remove { $_[0]->remove_from_db(); }
+
 sub update {
     my $self = shift;
     my $dbh = Bugzilla->dbh;
@@ -204,10 +206,13 @@ sub update {
         my $update_params;
         foreach my $group (GROUP_PARAMS) {
             if ($old_name eq Bugzilla->params->{$group}) {
+		if (Bugzilla->params->{$group}) ## not sure if this test is ok, but should check for empty values ? right ?
+		{
 		    SetParam($group, $new_name);
 		    $update_params = 1;
 		}		
             }
+        }
         write_params() if $update_params;
     }
 
diff --git a/Bugzilla/Product.pm b/Bugzilla/Product.pm
index 30ebc7c..a3da6b9 100644
--- a/Bugzilla/Product.pm
+++ b/Bugzilla/Product.pm
@@ -485,9 +485,39 @@ sub set_default_milestone { $_[0]->set('defaultmilestone', $_[1]); }
 sub set_is_active { $_[0]->set('isactive', $_[1]); }
 sub set_allows_unconfirmed { $_[0]->set('allows_unconfirmed', $_[1]); }
 
+sub set_remove { $_[0]->remove_from_db(); }
+
+##
+## here we hav the choice : an new method "set_group_controls_update" or a fix of the arguments of the existnig "set_group_controls" method 
+##
+
+sub set_group_controls_update {
+    my ($self, $group, $settings) = @_;
+    
+    if (ref($group) eq 'ARRAY')
+    {
+	# this is a WebService Request, need to fix the arguments (extract $group and $settings from the first $group argument)
+	$settings= $group->[1];
+	$group= Bugzilla::Group->new({name => $group->[0]});
+
+	return $self->set_group_controls($group,$settings);
+    }
+    else
+    {
+	ThrowUserError('product_illegal_group', "set_group_controls_update called with bad arguments"); ### FIXLE bad error type - just to do an error...
+    }    
+}
+
 sub set_group_controls {
     my ($self, $group, $settings) = @_;
 
+    if (ref($group) eq 'ARRAY')
+    {
+	# this is a WebService Request, need to fix the arguments (extract $group and $settings from the first $group argument)
+	$settings= $group->[1];
+	$group= Bugzilla::Group->new({name => $group->[0]});
+    }
+
     $group->is_active_bug_group
       || ThrowUserError('product_illegal_group', {group => $group});
 
-------------- next part --------------
#! /usr/bin/ruby

require 'rest-client'

@bz_api_key = 'YOU_API_KEY'
@bz_root_url= 'https://bugzilla.YOUR.DOMAIN/rest.cgi'

def bugzirq (verb='get', method='version', params={ })
  
  begin
    #    res= RestClient.get "#{@bz_root_url}#{method}",  :accept => :json, :content_type => :json , :params => { 'api_key' => @api_key }.merge!(params)                                                                                                   
    if    (verb=='get')
      res= RestClient.get  "#{@bz_root_url}/#{method}",  :accept => :json, :content_type => :json , :params => { 'api_key' => @bz_api_key }.merge!(params)
    elsif (verb=='post')
      #      res= RestClient.post "https://tracker.math.cnrs.fr/rest.cgi/#{method}",  :accept => :json, :content_type => :json , :params => { 'api_key' => @bz_api_key }.merge!(params)                                                                      
      res= RestClient.post "#{@bz_root_url}/#{method}",  { :accept => :json, :content_type => :json , 'api_key' => @bz_api_key }.merge!(params)
    elsif (verb=='put')
      #      res= RestClient.post "https://tracker.math.cnrs.fr/rest.cgi/#{method}",  :accept => :json, :content_type => :json , :params => { 'api_key' => @bz_api_key }.merge!(params)                                                                      
      #res= RestClient.put "https://tracker.math.cnrs.fr/rest.cgi/#{method}",  { :accept => :json, :content_type => :json , 'api_key' => @bz_api_key }.merge!(params)                                                                                  
      res= RestClient.put "#{@bz_root_url}/#{method}",  { 'api_key' => @bz_api_key }.merge!(params).to_json, :content_type => :json, :accept => :json
    elsif (verb=='delete')
      #      res= RestClient.post "https://tracker.math.cnrs.fr/rest.cgi/#{method}",  :accept => :json, :content_type => :json , :params => { 'api_key' => @bz_api_key }.merge!(params)                                                                      
      res= RestClient.delete "#{@bz_root_url}/#{method}",  { :accept => :json, :content_type => :json , 'api_key' => @bz_api_key }.merge!(params)
    end
  rescue RestClient::ResourceNotFound => e
    puts e.response+"resource not found\n"
  rescue => e
    puts e.response+"error\n"
  end
  
  res.to_s
end

#%w( user parameters version product_selectable product_enterable product_accessible  ).each{ |m|                                                                                                                                                      
#  puts bugzirq('get',m,{:match => 'depo'}).to_s                                                                                                                                                                                                       
#}                                                                                                                                                                                                                                                     

puts "== Product CREATE\n"

puts bugzirq('post','product',
             { :name => 'bz_api_test',
               :description => 'the marvelous bz_api',
               :version => '0.1'
             }).to_s

puts "== Group CREATE\n"
puts bugzirq('post','group',
             { :name => 'bz_api_test',
               :description => 'the marvelous bz_api',
               'is_active' => 'true'
             }).to_s

# update product    (/rest/product/NAME)                                                                                                                                                                                                               
#    { add => [$group_name] }                                                                                                                                                                                                                          

puts "== Product UPDATE - add group rights on product\n"

puts bugzirq('put','product/bz_api_test',
             #puts bugzirq('put','product',                                                                                                                                                                                                            
             { :name => 'bz_api_test',
               :names => [ 'bz_api_test' ],
               :description => 'new desc ',
               :group_controls => [ 'bz_api_test',
                                    {
                                      'editcomponents' => 1,
                                      'canedit' => 0,
                                    }
                                  ]
             }).to_s

puts "== Product UPDATE (alternate) - add group rights on product\n"

puts bugzirq('put','product/bz_api_test',
             {
               :description => 'new desc',
               :group_controls_update => [ 'bz_api_test',
                                           {
                                             'editcomponents' => 1,
                                             'canedit' => 0,
                                           }
                                         ]
             }).to_s

puts "== Groups UPDATE - add dumbo\n"

puts bugzirq('put','user/dumbo at bugzilla.plm',
             {
               :groups => { 'add' => ['bz_api_test' ] }
             }).to_s


puts "== Product UPDATE (alternate) - add group rights on product\n"

puts bugzirq('put','product/bz_api_test',
             {
               :description => 'new desc',
               :group_controls_update => [ 'bz_api_test',
                                           {
                                             'editcomponents' => 1,
                                             'canedit' => 0,
                                           }
                                         ]
             }).to_s



puts "== Products UPDATE - remove product\n"

puts bugzirq('put','product/bz_api_test',
             {
               :remove => 1
             }).to_s


puts "== Groups UPDATE - remove dumbo\n"

puts bugzirq('put','user/dumbo at bugzilla.plm',
             {
               :groups => { 'remove' => ['bz_api_test' ] }
             }).to_s

puts "== Group GET - get info\n"
puts bugzirq('get','group', {:match => 'aaaabz_api_test'} ).to_s

puts "== Group UPDATE - remove group\n"
#puts bugzirq('put','group/bz_api_test',                                                                                                                                                                                                               
puts bugzirq('put','group/bz_api_test',
             {
               :description => 'bz_api_test new desc',
               :remove => 'bz_api_test'
               #                                                                                                                                                                                                                                                      
             })

end


#puts bugzirq('delete','component',                                                                                                                                                                                                                    
#             { :name => 'bz_api_test',                                                                                                                                                                                                                
#               :description => 'the marvelous bz_api',                                                                                                                                                                                                
#               :version => '0.1'                                                                                                                                                                                                                      
#             }).to_s                                                                                                                                                                                                                                  



puts "== Group CREATE\n"
puts bugzirq('post','group',
             { :name => 'zztoshiba',
               :description => 'the toshiba grp',
               'is_active' => 'true'
             }).to_s

puts "== Group DELETE - remove group\n"
#puts bugzirq('put','group/bz_api_test',                                                                                                                                                                                                               
puts bugzirq('put','group/zztoshiba',
             {
               :action_remove => 1
             })















More information about the developers mailing list