From 96e91f857a2915a2618cce910aef87a5e8aaa017 Mon Sep 17 00:00:00 2001 From: John Smith/VC Date: Mon, 2 Oct 2023 16:01:35 -0400 Subject: [PATCH 1/2] Add tests for create_runners --- lib/gitlab/client/runners.rb | 72 +++++++++++++++++++ .../create_group_runner_response.json | 5 ++ .../create_instance_runner_response.json | 5 ++ .../create_project_runner_response.json | 5 ++ spec/gitlab/client/runners_spec.rb | 43 +++++++++++ 5 files changed, 130 insertions(+) create mode 100644 spec/fixtures/create_group_runner_response.json create mode 100644 spec/fixtures/create_instance_runner_response.json create mode 100644 spec/fixtures/create_project_runner_response.json diff --git a/lib/gitlab/client/runners.rb b/lib/gitlab/client/runners.rb index 653d69427..9ab08f098 100644 --- a/lib/gitlab/client/runners.rb +++ b/lib/gitlab/client/runners.rb @@ -207,5 +207,77 @@ def verify_auth_registered_runner(token) body = { token: token } post('/runners/verify', body: body) end + + # Creates a new group runner with the new Gitlab approach (v16.0+) and returns the id/token information + # https://docs.gitlab.com/ee/api/users.html#create-a-runner + # You must use an access token with the create_runner scope + # + # @example + # Gitlab.create_group_runner(9, tag_list: ['one', 'two']) + # Gitlab.create_group_runner(9, paused: false, description: 'A note', run_untagged: true) + # + # @param [String] group(required) Group ID. + # @param [Hash] options A customizable set of options. + # @return Response against runner registration + def create_group_runner(group, options={}) + create_runner({ runner_type: 'group_type', group_id: group }.merge(options)) + end + + + # Creates a new project runner with the new Gitlab approach (v16.0+) and returns the id/token information + # https://docs.gitlab.com/ee/api/users.html#create-a-runner + # You must use an access token with the create_runner scope + # + # @example + # Gitlab.create_project_runner(12, tag_list: ['one', 'two']) + # Gitlab.create_project_runner(12, paused: false, description: 'A note', run_untagged: true) + # + # @param [String] project(required) Project ID. + # @param [Hash] options A customizable set of options. + # @return Response against runner registration + def create_project_runner(project, options={}) + create_runner({ runner_type: 'project_type', project_id: project }.merge(options)) + end + + + # Creates a new instance runner with the new Gitlab approach (v16.0+) and returns the id/token information + # You must be an administrator of the GitLab instance + # You must use an access token with the create_runner scope + # https://docs.gitlab.com/ee/api/users.html#create-a-runner + # + # @example + # Gitlab.create_instance_runner(tag_list: ['one', 'two']) + # Gitlab.create_instance_runner(paused: false, description: 'A note', run_untagged: true) + # + # @param [String] group(required) Project ID. + # @param [Hash] options A customizable set of options. + # @return Response against runner registration + def create_instance_runner(options={}) + create_runner({ runner_type: 'instance_type' }.merge(options)) + end + + + private + + # Creates a runner linked to the current user. + # You must use an access token with the create_runner scope + # https://docs.gitlab.com/ee/api/users.html#create-a-runner + # + # @param [Hash] options(required) A customizable set of options. + # @option options [String] :description(optional) Runner description. + # @option options [Hash] :info(optional) Runner metadata. + # @option options [Boolean] :paused(optional) Whether the Runner ignores new jobs. + # @option options [Boolean] :locked(optional) Whether the Runner should be locked for current project. + # @option options [Boolean] :run_untagged(optional) Whether the Runner should handle untagged jobs. + # @option options [Array] :tag_list(optional) List of Runner tags. + # @option options [String] :access_level(optional) Access level of the runner; not_protected or ref_protected. + # @option options [Integer] :maximum_timeout(optional) Maximum timeout set when this Runner will handle the job. + # @option options [String] :maintenance_note(optional) Free-form maintenance notes for the runner (1024 characters). + # @return Response against runner registration {"id": 1, "token": foo "token_expires_at": null} + def create_runner(options) + post('/user/runners', body: options) + end + + end end diff --git a/spec/fixtures/create_group_runner_response.json b/spec/fixtures/create_group_runner_response.json new file mode 100644 index 000000000..7637ff99c --- /dev/null +++ b/spec/fixtures/create_group_runner_response.json @@ -0,0 +1,5 @@ +{ + "id": 12345, + "token": "glrt-kyahzxLaj4Dc1jQf4xjX", + "token_expires_at": null +} diff --git a/spec/fixtures/create_instance_runner_response.json b/spec/fixtures/create_instance_runner_response.json new file mode 100644 index 000000000..6f3320c5c --- /dev/null +++ b/spec/fixtures/create_instance_runner_response.json @@ -0,0 +1,5 @@ +{ + "id": 9171, + "token": "glrt-kyahzxLaj4Dc1jQf4xjX", + "token_expires_at": null +} diff --git a/spec/fixtures/create_project_runner_response.json b/spec/fixtures/create_project_runner_response.json new file mode 100644 index 000000000..1dbbcbc75 --- /dev/null +++ b/spec/fixtures/create_project_runner_response.json @@ -0,0 +1,5 @@ +{ + "id": 56789, + "token": "glrt-kyahzxLaj4Dc1jQf4xjX", + "token_expires_at": null +} diff --git a/spec/gitlab/client/runners_spec.rb b/spec/gitlab/client/runners_spec.rb index 3b9f8cec0..2f8e0c4e3 100644 --- a/spec/gitlab/client/runners_spec.rb +++ b/spec/gitlab/client/runners_spec.rb @@ -243,6 +243,49 @@ end end + + describe 'create_runner' do + it "creates the correct group runner call" do + stub_post('/user/runners', 'create_group_runner_response.json') + .with(body: { runner_type: 'group_type', group_id: 12345, tag_list: ['foo','bar'], description: "desc", locked: false }) + + @runner_response = Gitlab.create_group_runner(12345, tag_list: ['foo', 'bar'], description: "desc", locked: false) + + expect(a_post('/user/runners') + .with(body: { runner_type: 'group_type', group_id: 12345, tag_list: ['foo','bar'], description: "desc", locked: false })) + .to have_been_made + + expect(@runner_response.to_h).to eq({"id" => 12345, "token" => "glrt-kyahzxLaj4Dc1jQf4xjX", "token_expires_at" => nil}) + end + + it "creates the correct project runner call" do + stub_post('/user/runners', 'create_project_runner_response.json') + .with(body: { runner_type: 'project_type', project_id: 56789, tag_list: ['foo','bar'], paused: true, maximum_timeout: 60 }) + + @runner_response = Gitlab.create_project_runner(56789, tag_list: ['foo', 'bar'], paused: true, maximum_timeout: 60) + + expect(a_post('/user/runners') + .with(body: { runner_type: 'project_type', project_id: 56789, tag_list: ['foo','bar'], paused: true, maximum_timeout: 60 })) + .to have_been_made + + expect(@runner_response.to_h).to eq({"id" => 56789, "token" => "glrt-kyahzxLaj4Dc1jQf4xjX", "token_expires_at" => nil}) + end + + it "creates the correct instance runner call" do + stub_post('/user/runners', 'create_instance_runner_response.json') + .with(body: { runner_type: 'instance_type', tag_list: ['foo','bar'], maintenance_note: "note", run_untagged: false, access_level: "ref_protected" }) + + @runner_response = Gitlab.create_instance_runner(tag_list: ['foo', 'bar'], maintenance_note: "note", run_untagged: false, access_level: "ref_protected") + + expect(a_post('/user/runners') + .with(body: { runner_type: 'instance_type', tag_list: ['foo','bar'], maintenance_note: "note", run_untagged: false, access_level: "ref_protected" })) + .to have_been_made + + expect(@runner_response.to_h).to eq({"id" => 9171, "token" => "glrt-kyahzxLaj4Dc1jQf4xjX", "token_expires_at" => nil}) + end + end + + describe '.delete_registered_runner' do before do stub_delete('/runners', 'empty').with(body: { token: '6337ff461c94fd3fa32ba3b1ff4125' }) From 51fdbccc2155286eac20f2b8d5a4c401dd24f12a Mon Sep 17 00:00:00 2001 From: John Smith/VC Date: Tue, 3 Oct 2023 09:00:45 -0400 Subject: [PATCH 2/2] Add fixes for rubocop; However, I disagree with the "too many lines" rule for the specs. To "fix" we had to make extremly long lines which is not as readable. --- lib/gitlab/client/runners.rb | 11 +++----- spec/gitlab/client/runners_spec.rb | 41 +++++++++++------------------- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/lib/gitlab/client/runners.rb b/lib/gitlab/client/runners.rb index 9ab08f098..24435bdc5 100644 --- a/lib/gitlab/client/runners.rb +++ b/lib/gitlab/client/runners.rb @@ -219,11 +219,10 @@ def verify_auth_registered_runner(token) # @param [String] group(required) Group ID. # @param [Hash] options A customizable set of options. # @return Response against runner registration - def create_group_runner(group, options={}) + def create_group_runner(group, options = {}) create_runner({ runner_type: 'group_type', group_id: group }.merge(options)) end - # Creates a new project runner with the new Gitlab approach (v16.0+) and returns the id/token information # https://docs.gitlab.com/ee/api/users.html#create-a-runner # You must use an access token with the create_runner scope @@ -235,11 +234,10 @@ def create_group_runner(group, options={}) # @param [String] project(required) Project ID. # @param [Hash] options A customizable set of options. # @return Response against runner registration - def create_project_runner(project, options={}) + def create_project_runner(project, options = {}) create_runner({ runner_type: 'project_type', project_id: project }.merge(options)) end - # Creates a new instance runner with the new Gitlab approach (v16.0+) and returns the id/token information # You must be an administrator of the GitLab instance # You must use an access token with the create_runner scope @@ -252,11 +250,10 @@ def create_project_runner(project, options={}) # @param [String] group(required) Project ID. # @param [Hash] options A customizable set of options. # @return Response against runner registration - def create_instance_runner(options={}) + def create_instance_runner(options = {}) create_runner({ runner_type: 'instance_type' }.merge(options)) end - private # Creates a runner linked to the current user. @@ -277,7 +274,5 @@ def create_instance_runner(options={}) def create_runner(options) post('/user/runners', body: options) end - - end end diff --git a/spec/gitlab/client/runners_spec.rb b/spec/gitlab/client/runners_spec.rb index 2f8e0c4e3..5127688eb 100644 --- a/spec/gitlab/client/runners_spec.rb +++ b/spec/gitlab/client/runners_spec.rb @@ -243,49 +243,38 @@ end end - describe 'create_runner' do - it "creates the correct group runner call" do - stub_post('/user/runners', 'create_group_runner_response.json') - .with(body: { runner_type: 'group_type', group_id: 12345, tag_list: ['foo','bar'], description: "desc", locked: false }) + it 'creates the correct group runner call' do + stub_post('/user/runners', 'create_group_runner_response.json').with(body: { runner_type: 'group_type', group_id: 12_345, tag_list: %w[foo bar], description: 'desc', locked: false }) - @runner_response = Gitlab.create_group_runner(12345, tag_list: ['foo', 'bar'], description: "desc", locked: false) + @runner_response = Gitlab.create_group_runner(12_345, tag_list: %w[foo bar], description: 'desc', locked: false) - expect(a_post('/user/runners') - .with(body: { runner_type: 'group_type', group_id: 12345, tag_list: ['foo','bar'], description: "desc", locked: false })) - .to have_been_made + expect(a_post('/user/runners').with(body: { runner_type: 'group_type', group_id: 12_345, tag_list: %w[foo bar], description: 'desc', locked: false })).to have_been_made - expect(@runner_response.to_h).to eq({"id" => 12345, "token" => "glrt-kyahzxLaj4Dc1jQf4xjX", "token_expires_at" => nil}) + expect(@runner_response.to_h).to eq({ 'id' => 12_345, 'token' => 'glrt-kyahzxLaj4Dc1jQf4xjX', 'token_expires_at' => nil }) end - it "creates the correct project runner call" do - stub_post('/user/runners', 'create_project_runner_response.json') - .with(body: { runner_type: 'project_type', project_id: 56789, tag_list: ['foo','bar'], paused: true, maximum_timeout: 60 }) + it 'creates the correct project runner call' do + stub_post('/user/runners', 'create_project_runner_response.json').with(body: { runner_type: 'project_type', project_id: 56_789, tag_list: %w[foo bar], paused: true, maximum_timeout: 60 }) - @runner_response = Gitlab.create_project_runner(56789, tag_list: ['foo', 'bar'], paused: true, maximum_timeout: 60) + @runner_response = Gitlab.create_project_runner(56_789, tag_list: %w[foo bar], paused: true, maximum_timeout: 60) - expect(a_post('/user/runners') - .with(body: { runner_type: 'project_type', project_id: 56789, tag_list: ['foo','bar'], paused: true, maximum_timeout: 60 })) - .to have_been_made + expect(a_post('/user/runners').with(body: { runner_type: 'project_type', project_id: 56_789, tag_list: %w[foo bar], paused: true, maximum_timeout: 60 })).to have_been_made - expect(@runner_response.to_h).to eq({"id" => 56789, "token" => "glrt-kyahzxLaj4Dc1jQf4xjX", "token_expires_at" => nil}) + expect(@runner_response.to_h).to eq({ 'id' => 56_789, 'token' => 'glrt-kyahzxLaj4Dc1jQf4xjX', 'token_expires_at' => nil }) end - it "creates the correct instance runner call" do - stub_post('/user/runners', 'create_instance_runner_response.json') - .with(body: { runner_type: 'instance_type', tag_list: ['foo','bar'], maintenance_note: "note", run_untagged: false, access_level: "ref_protected" }) + it 'creates the correct instance runner call' do + stub_post('/user/runners', 'create_instance_runner_response.json').with(body: { runner_type: 'instance_type', tag_list: %w[foo bar], maintenance_note: 'note', run_untagged: false, access_level: 'ref_protected' }) - @runner_response = Gitlab.create_instance_runner(tag_list: ['foo', 'bar'], maintenance_note: "note", run_untagged: false, access_level: "ref_protected") + @runner_response = Gitlab.create_instance_runner(tag_list: %w[foo bar], maintenance_note: 'note', run_untagged: false, access_level: 'ref_protected') - expect(a_post('/user/runners') - .with(body: { runner_type: 'instance_type', tag_list: ['foo','bar'], maintenance_note: "note", run_untagged: false, access_level: "ref_protected" })) - .to have_been_made + expect(a_post('/user/runners').with(body: { runner_type: 'instance_type', tag_list: %w[foo bar], maintenance_note: 'note', run_untagged: false, access_level: 'ref_protected' })).to have_been_made - expect(@runner_response.to_h).to eq({"id" => 9171, "token" => "glrt-kyahzxLaj4Dc1jQf4xjX", "token_expires_at" => nil}) + expect(@runner_response.to_h).to eq({ 'id' => 9171, 'token' => 'glrt-kyahzxLaj4Dc1jQf4xjX', 'token_expires_at' => nil }) end end - describe '.delete_registered_runner' do before do stub_delete('/runners', 'empty').with(body: { token: '6337ff461c94fd3fa32ba3b1ff4125' })