diff --git a/app/controllers/public_keys_controller.rb b/app/controllers/public_keys_controller.rb index 07d60c490..fe4b4047c 100644 --- a/app/controllers/public_keys_controller.rb +++ b/app/controllers/public_keys_controller.rb @@ -18,11 +18,11 @@ class PublicKeysController < ApplicationController if @gitea_response[0] == 201 @public_key = @gitea_response[2] else - return render_error("创建ssh key失败") if @gitea_response[2]["message"].nil? + return render_error("创建ssh key失败") if @gitea_response[2].blank? return render_ok({status: 10002, message: "密钥格式不正确"}) if @gitea_response[2]["message"].starts_with?("Invalid key content") exist_public_key = Gitea::PublicKey.find_by(content: public_key_params[:key]) - return render_ok({status: 10002, message: "密钥已存在,请勿重复添加"}) if @gitea_response[2]["message"].starts_with?("Key content has been used as non-deploy key") && exist_public_key.owner_id == current_user.gitea_uid - return render_ok({status: 10002, message: "密钥已被占用"}) if @gitea_response[2]["message"].starts_with?("Key content has been used as non-deploy key") && exist_public_key.present? + return render_ok({status: 10002, message: "密钥已被占用"}) if @gitea_response[2]["message"].starts_with?("Key content has been used as non-deploy key") && exist_public_key.present? && exist_public_key&.owner_id != current_user.gitea_uid + return render_ok({status: 10002, message: "密钥已存在,请勿重复添加"}) if @gitea_response[2]["message"].starts_with?("Key content has been used as non-deploy key") @public_key = nil end rescue Exception => e diff --git a/app/views/public_keys/create.json.jbuilder b/app/views/public_keys/create.json.jbuilder index bcbcb9451..9296413ab 100644 --- a/app/views/public_keys/create.json.jbuilder +++ b/app/views/public_keys/create.json.jbuilder @@ -1,5 +1,8 @@ -json.id @public_key["id"] -json.name @public_key["title"] -json.content @public_key["key"] -json.fingerprint @public_key["fingerprint"] -json.created_time @public_key["created_at"].to_time.strftime("%Y/%m/%d %H:%M") \ No newline at end of file +if @public_key.present? + json.status 0 + json.id @public_key["id"] + json.name @public_key["title"] + json.content @public_key["key"] + json.fingerprint @public_key["fingerprint"] + json.created_time @public_key["created_at"].to_time.strftime("%Y/%m/%d %H:%M") +end \ No newline at end of file