From 0f273b256af65da7973e23c874a7d7bab8675b27 Mon Sep 17 00:00:00 2001 From: David Dieulivol Date: Fri, 27 Jul 2018 10:56:21 +0200 Subject: [PATCH] Allow to pass options for deploy keys --- lib/gitlab/client/projects.rb | 7 +++--- spec/fixtures/project_key.json | 3 ++- spec/fixtures/project_keys.json | 3 ++- spec/gitlab/client/projects_spec.rb | 34 +++++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/gitlab/client/projects.rb b/lib/gitlab/client/projects.rb index 99275e9ac..9d7ef2b78 100644 --- a/lib/gitlab/client/projects.rb +++ b/lib/gitlab/client/projects.rb @@ -340,14 +340,15 @@ def deploy_key(project, id) # Creates a new deploy key. # # @example - # Gitlab.create_deploy_key(42, 'My Key', 'Key contents') + # Gitlab.create_deploy_key(42, 'My Key', 'Key contents', can_push: true) # # @param [Integer, String] project The ID or path of a project. # @param [String] title The title of a deploy key. # @param [String] key The content of a deploy key. + # @param [Hash] options A customizable set of options. # @return [Gitlab::ObjectifiedHash] Information about created deploy key. - def create_deploy_key(project, title, key) - post("/projects/#{url_encode project}/deploy_keys", body: { title: title, key: key }) + def create_deploy_key(project, title, key, options = {}) + post("/projects/#{url_encode project}/deploy_keys", body: { title: title, key: key }.merge(options)) end # Enables a deploy key at the project. diff --git a/spec/fixtures/project_key.json b/spec/fixtures/project_key.json index d917f9466..7563fcd9d 100644 --- a/spec/fixtures/project_key.json +++ b/spec/fixtures/project_key.json @@ -2,5 +2,6 @@ "id": 2, "title": "Key Title", "key": "ssh-rsa ...", + "can_push": false, "created_at": "2013-09-22T18:34:32Z" -} \ No newline at end of file +} diff --git a/spec/fixtures/project_keys.json b/spec/fixtures/project_keys.json index dd22f9668..02ba4b024 100644 --- a/spec/fixtures/project_keys.json +++ b/spec/fixtures/project_keys.json @@ -2,5 +2,6 @@ "id": 2, "title": "Key Title", "key": "ssh-rsa ...", + "can_push": false, "created_at": "2013-09-22T18:34:32Z" -}] \ No newline at end of file +}] diff --git a/spec/gitlab/client/projects_spec.rb b/spec/gitlab/client/projects_spec.rb index 9faae44f1..dcfe227f3 100644 --- a/spec/gitlab/client/projects_spec.rb +++ b/spec/gitlab/client/projects_spec.rb @@ -515,6 +515,40 @@ end end + describe ".create_deploy_key" do + context 'no options' do + before do + stub_post("/projects/42/deploy_keys", "project_key") + @deploy_key = Gitlab.create_deploy_key(42, 'My Key', 'Key contents') + end + + it "gets the correct resource" do + expect(a_post("/projects/42/deploy_keys"). + with(body: { title: 'My Key', key: 'Key contents' })).to have_been_made + end + + it "returns information about a created key" do + expect(@deploy_key.id).to eq(2) + end + end + + context 'some options' do + before do + stub_post("/projects/42/deploy_keys", "project_key") + @deploy_key = Gitlab.create_deploy_key(42, 'My Key', 'Key contents', can_push: true) + end + + it "gets the correct resource" do + expect(a_post("/projects/42/deploy_keys"). + with(body: { title: 'My Key', key: 'Key contents', can_push: true })).to have_been_made + end + + it "returns information about a created key" do + expect(@deploy_key.id).to eq(2) + end + end + end + describe ".delete_deploy_key" do before do stub_delete("/projects/42/deploy_keys/2", "project_key")