OSDN Git Service

Fix deploy key api 500 if key is empty
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Mon, 3 Jun 2013 18:01:07 +0000 (21:01 +0300)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Mon, 3 Jun 2013 18:01:07 +0000 (21:01 +0300)
lib/api/projects.rb

index 13aa42b..3711057 100644 (file)
@@ -428,21 +428,23 @@ module API
       post ":id/keys" do
         attrs = attributes_for_keys [:title, :key]
 
-        attrs[:key].strip!
-
-        # check if key already exist in project
-        key = user_project.deploy_keys.find_by_key(attrs[:key])
-        if key
-          present key, with: Entities::SSHKey
-          return
-        end
+        if attrs[:key].present?
+          attrs[:key].strip!
+
+          # check if key already exist in project
+          key = user_project.deploy_keys.find_by_key(attrs[:key])
+          if key
+            present key, with: Entities::SSHKey
+            return
+          end
 
-        # Check for available deploy keys in other projects
-        key = current_user.owned_deploy_keys.find_by_key(attrs[:key])
-        if key
-          user_project.deploy_keys << key
-          present key, with: Entities::SSHKey
-          return
+          # Check for available deploy keys in other projects
+          key = current_user.owned_deploy_keys.find_by_key(attrs[:key])
+          if key
+            user_project.deploy_keys << key
+            present key, with: Entities::SSHKey
+            return
+          end
         end
 
         key = DeployKey.new attrs