]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2412 add support of project deletion from the Java web service client
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 15 Aug 2011 15:13:50 +0000 (17:13 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 15 Aug 2011 15:13:50 +0000 (17:13 +0200)
12 files changed:
sonar-server/src/main/webapp/WEB-INF/app/models/project.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/Sonar.java
sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/Connector.java
sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient3Connector.java
sonar-ws-client/src/main/java/org/sonar/wsclient/connectors/HttpClient4Connector.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/DeleteQuery.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/FavouriteDeleteQuery.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ManualMeasureDeleteQuery.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java [new file with mode: 0644]
sonar-ws-client/src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java
sonar-ws-client/src/main/java/org/sonar/wsclient/services/UserPropertyDeleteQuery.java
sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java [new file with mode: 0644]

index 5fd8dde72097e9f24092b53a79e16ce44e8b5635..01b358e17ce747bb149394bfe98cc263fc1f91f4 100644 (file)
@@ -20,7 +20,7 @@
 class Project < ActiveRecord::Base
   include Comparable
   include Resourceable
-  
+
   has_many :snapshots
   has_many :processed_snapshots, :class_name => 'Snapshot', :conditions => "status='#{Snapshot::STATUS_PROCESSED}' AND qualifier<>'LIB'", :order => 'created_at asc'
   has_many :events, :foreign_key => 'resource_id', :order => 'event_date DESC'
@@ -41,36 +41,38 @@ class Project < ActiveRecord::Base
   end
 
   def self.delete_project(project)
+    if project
       Snapshot.update_all(['islast=?', false], ['(root_project_id=? OR project_id=?) AND islast=?', project.id, project.id, true])
       Project.delete_all(['id=? OR root_id=? or copy_resource_id=?', project.id, project.id, project.id])
+    end
   end
-  
+
   def project
     root||self
   end
-  
+
   def root_project
-    @root_project ||= 
-      begin
-        parent_module(self)
-      end
+    @root_project ||=
+        begin
+          parent_module(self)
+        end
   end
-  
+
   def last_snapshot
     @last_snapshot ||=
-      begin
-        snapshot=Snapshot.find(:first, :conditions => {:islast => true, :project_id => id})
-        if snapshot
-          snapshot.project=self
+        begin
+          snapshot=Snapshot.find(:first, :conditions => {:islast => true, :project_id => id})
+          if snapshot
+            snapshot.project=self
+          end
+          snapshot
         end
-        snapshot
-      end
   end
-  
+
   def events_with_snapshot
-    events.select{|event| !event.snapshot_id.nil?}
+    events.select { |event| !event.snapshot_id.nil? }
   end
-  
+
   def key
     kee
   end
@@ -78,29 +80,29 @@ class Project < ActiveRecord::Base
   def links
     project_links
   end
-  
+
   def link(type)
     # to_a avoids conflicts with ActiveRecord:Base.find
-    links.to_a.find{ |l| l.link_type==type}
-  end 
-  
+    links.to_a.find { |l| l.link_type==type }
+  end
+
   def custom_links
-    links.select {|l| l.custom?}
+    links.select { |l| l.custom? }
   end
-  
+
   def standard_links
-    links.reject {|l| l.custom?}
+    links.reject { |l| l.custom? }
   end
 
   def chart_measures(metric_id)
     sql = Project.send(:sanitize_sql, ['select s.created_at as created_at, m.value as value ' +
-          ' from project_measures m, snapshots s ' +
-          ' where s.id=m.snapshot_id and ' +
-          " s.status='%s' and " +
-          ' s.project_id=%s and m.metric_id=%s ', Snapshot::STATUS_PROCESSED, self.id, metric_id] ) +
-      ' and m.rule_id IS NULL and m.rule_priority IS NULL' +
-      ' order by s.created_at'
-    create_chart_measures( Project.connection.select_all( sql ), 'created_at', 'value' )
+                                           ' from project_measures m, snapshots s ' +
+                                           ' where s.id=m.snapshot_id and ' +
+                                           " s.status='%s' and " +
+                                           ' s.project_id=%s and m.metric_id=%s ', Snapshot::STATUS_PROCESSED, self.id, metric_id]) +
+        ' and m.rule_id IS NULL and m.rule_priority IS NULL' +
+        ' order by s.created_at'
+    create_chart_measures(Project.connection.select_all(sql), 'created_at', 'value')
   end
 
   def <=>(other)
@@ -118,13 +120,13 @@ class Project < ActiveRecord::Base
   def fullname
     name
   end
-  
+
   def branch
     if project? || module?
       s=kee.split(':')
       if s.size>=3
         return s[2]
-      end 
+      end
     end
     nil
   end
@@ -156,19 +158,19 @@ class Project < ActiveRecord::Base
       # when regular active record impl return string typed objects
       if results.first[date_column_name].class == Time
         results.each do |hash|
-          chart_measures << ChartMeasure.new( hash[date_column_name], hash[value_column_name] )
+          chart_measures << ChartMeasure.new(hash[date_column_name], hash[value_column_name])
         end
       else
         results.each do |hash|
-          chart_measures << ChartMeasure.new( Time.parse( hash[date_column_name] ), hash[value_column_name].to_d )
+          chart_measures << ChartMeasure.new(Time.parse(hash[date_column_name]), hash[value_column_name].to_d)
         end
       end
     end
     chart_measures
   end
-  
+
   def parent_module(current_module)
     current_module.root ? parent_module(current_module.root) : current_module
   end
-  
+
 end
index e6a2dc855b62f1136ede3db7c48c15e8e7eeef8e..c19254963c0ac8eddcbd0b910e212666af8329bb 100644 (file)
@@ -93,7 +93,7 @@ public class Sonar {
     connector.execute(query);
   }
 
-  public void delete(DeleteQuery<?> query) {
+  public void delete(DeleteQuery query) {
     connector.execute(query);
   }
 
index cf874703859023f51148cb7afacc358f0880bed7..711aa9726a876271f86981ed665ee6942db47d1d 100644 (file)
@@ -45,7 +45,7 @@ public abstract class Connector {
    * @return JSON response or null if 404 NOT FOUND error
    * @since 2.2
    */
-  public abstract String execute(DeleteQuery<?> query);
+  public abstract String execute(DeleteQuery query);
 
   /**
    * @return JSON response or null if 404 NOT FOUND error
index cb720320d7cbc1923d009448afb0c3b2f5d75ca4..7632eedd000dc4dfc63271984fbe31a068b52986 100644 (file)
@@ -81,7 +81,7 @@ public class HttpClient3Connector extends Connector {
     return executeRequest(newPutRequest(query));
   }
 
-  public String execute(DeleteQuery<?> query) {
+  public String execute(DeleteQuery query) {
     return executeRequest(newDeleteRequest(query));
   }
 
@@ -117,7 +117,7 @@ public class HttpClient3Connector extends Connector {
     return method;
   }
 
-  private HttpMethodBase newDeleteRequest(DeleteQuery<?> query) {
+  private HttpMethodBase newDeleteRequest(DeleteQuery query) {
     HttpMethodBase method = new DeleteMethod(server.getHost() + query.getUrl());
     initRequest(method, query);
     return method;
index e1aebfd175e7ce07d49db81cce5c96e03f3db090..cd1a31cb96985d68e2c3dca5979a684a491340cd 100644 (file)
@@ -82,7 +82,7 @@ public class HttpClient4Connector extends Connector {
     return executeRequest(newPutMethod(query));
   }
 
-  public String execute(DeleteQuery<?> query) {
+  public String execute(DeleteQuery query) {
     return executeRequest(newDeleteMethod(query));
   }
 
@@ -146,7 +146,7 @@ public class HttpClient4Connector extends Connector {
     return get;
   }
 
-  private HttpDelete newDeleteMethod(DeleteQuery<?> query) {
+  private HttpDelete newDeleteMethod(DeleteQuery query) {
     HttpDelete delete = new HttpDelete(server.getHost() + query.getUrl());
     initRequest(delete, query);
     return delete;
index 5a3df918436a5dcd223366587a7cd282dc2fec57..91249db4ae69fc1d78f31f644f7153c44b0cc08a 100644 (file)
@@ -24,6 +24,6 @@ package org.sonar.wsclient.services;
  *
  * @since 2.2
  */
-public abstract class DeleteQuery<MODEL extends Model> extends AbstractQuery<MODEL> {
+public abstract class DeleteQuery extends AbstractQuery {
 
 }
index 40ac6d21e7989eabf0f8316f24c7670523261555..99065b5e3f426086b4a346cb7495f8e072c822ff 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.wsclient.services;
 
-public class FavouriteDeleteQuery extends DeleteQuery<Favourite> {
+public class FavouriteDeleteQuery extends DeleteQuery {
 
   private String idOrKey;
 
index 74b3e591136b22b8b800d87a7838e3d8ce9dcfa5..eb7e50349b2a540efde3dab4fb1452b63dc24db9 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.wsclient.services;
 /**
  * @since 2.10
  */
-public final class ManualMeasureDeleteQuery extends DeleteQuery<ManualMeasure> {
+public final class ManualMeasureDeleteQuery extends DeleteQuery {
 
   private String resourceKey;
   private String metricKey;
diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ProjectDeleteQuery.java
new file mode 100644 (file)
index 0000000..47c1127
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.services;
+
+/**
+ * @since 2.11
+ */
+public final class ProjectDeleteQuery extends DeleteQuery {
+
+  private String key;
+  private static final String BASE_URL = "/api/projects/";
+
+  private ProjectDeleteQuery(String key) {
+    this.key = key;
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  @Override
+  public String getUrl() {
+    StringBuilder url = new StringBuilder();
+    url.append(BASE_URL);
+    url.append(encode(key));
+    return url.toString();
+  }
+
+  public static ProjectDeleteQuery create(String projectKeyOrId) {
+    return new ProjectDeleteQuery(projectKeyOrId);
+  }
+}
index 8e6b7b119dd530dcabcfe73a4b0c449fe1fea0da..1775bcf2360dc347d68fd51dbdb48c7bf2345fb5 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.wsclient.services;
 /**
  * @since 2.6
  */
-public class PropertyDeleteQuery extends DeleteQuery<Property> {
+public class PropertyDeleteQuery extends DeleteQuery {
 
   private String key;
   private String resourceKeyOrId;
index 81f1e182134c9141f3404afa8785de8b62591234..cebcf9f1a8b52713284240a4b7b487d081f91402 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.wsclient.services;
 /**
  * @since 2.2
  */
-public class UserPropertyDeleteQuery extends DeleteQuery<Property> {
+public class UserPropertyDeleteQuery extends DeleteQuery {
 
   private String key;
 
diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ProjectDeleteQueryTest.java
new file mode 100644 (file)
index 0000000..8957381
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.wsclient.services;
+
+import org.junit.Test;
+import org.sonar.wsclient.Sonar;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+public class ProjectDeleteQueryTest extends QueryTestCase {
+
+  @Test
+  public void testUrl() {
+    ProjectDeleteQuery query = ProjectDeleteQuery.create("my:project");
+    assertThat(query.getUrl(), is("/api/projects/my%3Aproject"));
+  }
+
+}
\ No newline at end of file