diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-02-18 19:00:26 +0300 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-02-24 17:07:24 +0300 |
commit | 6979b3afd50f86550e523ed66ef22fd153e6cbc8 (patch) | |
tree | adf4ed031262b7986dc8e5325cdbb9271783eb5e /lib | |
parent | f2b37de54ba3cb0a375fb3a03e7ffd1f18444c39 (diff) |
Delete deploy key from Bitbucket after importing.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/bitbucket_import/client.rb | 19 | ||||
-rw-r--r-- | lib/gitlab/bitbucket_import/key_adder.rb | 2 | ||||
-rw-r--r-- | lib/gitlab/bitbucket_import/key_deleter.rb | 23 |
3 files changed, 38 insertions, 6 deletions
diff --git a/lib/gitlab/bitbucket_import/client.rb b/lib/gitlab/bitbucket_import/client.rb index 3d2ef78ee7d..5095e592ab7 100644 --- a/lib/gitlab/bitbucket_import/client.rb +++ b/lib/gitlab/bitbucket_import/client.rb @@ -61,17 +61,28 @@ module Gitlab JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}").body) end - def deploy_key(project_identifier) - JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}/deploy-keys").body).find { |key| key["label"] =~ /GitLab/ } + def find_deploy_key(project_identifier, key) + JSON.parse(api.get("/api/1.0/repositories/#{project_identifier}/deploy-keys").body).find { |deploy_key| + deploy_key["key"].chomp == key.chomp + } end def add_deploy_key(project_identifier, key) + deploy_key = find_deploy_key(project_identifier, key) + return if deploy_key + JSON.parse(api.post("/api/1.0/repositories/#{project_identifier}/deploy-keys", key: key, label: "GitLab import key").body) end + def delete_deploy_key(project_identifier, key) + deploy_key = find_deploy_key(project_identifier, key) + return unless deploy_key + + api.delete("/api/1.0/repositories/#{project_identifier}/deploy-keys/#{deploy_key["pk"]}").code == "204" + end + def projects - JSON.parse(api.get("/api/1.0/user/repositories").body). - select { |repo| repo["scm"] == "git" } + JSON.parse(api.get("/api/1.0/user/repositories").body).select { |repo| repo["scm"] == "git" } end private diff --git a/lib/gitlab/bitbucket_import/key_adder.rb b/lib/gitlab/bitbucket_import/key_adder.rb index 7d0b5fbc8ae..9931aa7e029 100644 --- a/lib/gitlab/bitbucket_import/key_adder.rb +++ b/lib/gitlab/bitbucket_import/key_adder.rb @@ -12,8 +12,6 @@ module Gitlab return false unless BitbucketImport.public_key.present? project_identifier = "#{repo["owner"]}/#{repo["slug"]}" - return true if client.deploy_key(project_identifier) - client.add_deploy_key(project_identifier, BitbucketImport.public_key) true diff --git a/lib/gitlab/bitbucket_import/key_deleter.rb b/lib/gitlab/bitbucket_import/key_deleter.rb new file mode 100644 index 00000000000..1a24a86fc37 --- /dev/null +++ b/lib/gitlab/bitbucket_import/key_deleter.rb @@ -0,0 +1,23 @@ +module Gitlab + module BitbucketImport + class KeyDeleter + attr_reader :project, :current_user, :client + + def initialize(project) + @project = project + @current_user = project.creator + @client = Client.new(current_user.bitbucket_access_token, current_user.bitbucket_access_token_secret) + end + + def execute + return false unless BitbucketImport.public_key.present? + + client.delete_deploy_key(project.import_source, BitbucketImport.public_key) + + true + rescue + false + end + end + end +end |