diff options
author | Robert Schilling <rschilling@student.tugraz.at> | 2015-11-21 20:08:45 +0300 |
---|---|---|
committer | Robert Schilling <rschilling@student.tugraz.at> | 2015-11-21 20:08:45 +0300 |
commit | faef95af1a07bdcfd02eead36d144f332b428f1f (patch) | |
tree | 7f34f351e7681036bbafda1980e1d403316ee7d5 | |
parent | 2cba93a0d2d12ee36bf98569e5c6c14ac7ea40e0 (diff) |
API: Return 404 if the tag for a release does not exist
-rw-r--r-- | app/services/create_release_service.rb | 25 | ||||
-rw-r--r-- | app/services/create_tag_service.rb | 10 | ||||
-rw-r--r-- | doc/api/tags.md | 3 | ||||
-rw-r--r-- | lib/api/tags.rb | 10 | ||||
-rw-r--r-- | spec/requests/api/tags_spec.rb | 8 |
5 files changed, 47 insertions, 9 deletions
diff --git a/app/services/create_release_service.rb b/app/services/create_release_service.rb new file mode 100644 index 00000000000..355374ce252 --- /dev/null +++ b/app/services/create_release_service.rb @@ -0,0 +1,25 @@ +require_relative 'base_service' + +class CreateReleaseService < BaseService + def execute(tag_name, release_description) + + repository = project.repository + existing_tag = repository.find_tag(tag_name) + + # Only create a release if the tag exists + if existing_tag + release = project.releases.find_or_initialize_by(tag: tag_name) + release.update_attributes(description: release_description) + + success(release) + else + error('Tag does not exist') + end + end + + def success(release) + out = super() + out[:release] = release + out + end +end diff --git a/app/services/create_tag_service.rb b/app/services/create_tag_service.rb index 9917119fce2..2452999382a 100644 --- a/app/services/create_tag_service.rb +++ b/app/services/create_tag_service.rb @@ -19,16 +19,16 @@ class CreateTagService < BaseService new_tag = repository.find_tag(tag_name) if new_tag - if release_description - release = project.releases.find_or_initialize_by(tag: tag_name) - release.update_attributes(description: release_description) - end - push_data = create_push_data(project, current_user, new_tag) EventCreateService.new.push(project, current_user, push_data) project.execute_hooks(push_data.dup, :tag_push_hooks) project.execute_services(push_data.dup, :tag_push_hooks) + if release_description + CreateReleaseService.new(@project, @current_user). + execute(tag_name, release_description) + end + success(new_tag) else error('Invalid reference name') diff --git a/doc/api/tags.md b/doc/api/tags.md index cf95f87dc3e..ca9e2cef651 100644 --- a/doc/api/tags.md +++ b/doc/api/tags.md @@ -86,7 +86,8 @@ It returns 200 if the operation succeed. In case of an error, ## New release -Add release notes to the existing git tag +Add release notes to the existing git tag. It returns 200 if the release is +created successfully. If the tag does not exist, 404 is returned. ``` PUT /projects/:id/repository/tags/:tag_name/release diff --git a/lib/api/tags.rb b/lib/api/tags.rb index 2c6c73da08e..0721b9cc844 100644 --- a/lib/api/tags.rb +++ b/lib/api/tags.rb @@ -51,10 +51,14 @@ module API put ':id/repository/tags/:tag_name/release', requirements: { tag_name: /.*/ } do authorize_push_project required_attributes! [:description] - release = user_project.releases.find_or_initialize_by(tag: params[:tag_name]) - release.update_attributes(description: params[:description]) + result = CreateReleaseService.new(user_project, current_user). + execute(params[:tag_name], params[:description]) - present release, with: Entities::Release + if result[:status] == :success + present result[:release], with: Entities::Release + else + render_api_error!(result[:message], 404) + end end end end diff --git a/spec/requests/api/tags_spec.rb b/spec/requests/api/tags_spec.rb index e3fd2d547c4..4dac3eec84e 100644 --- a/spec/requests/api/tags_spec.rb +++ b/spec/requests/api/tags_spec.rb @@ -131,5 +131,13 @@ describe API::API, api: true do expect(json_response['tag_name']).to eq(tag_name) expect(json_response['description']).to eq(description) end + + it 'should return 404 if the tag does not exist' do + put api("/projects/#{project.id}/repository/tags/foobar/release", user), + description: description + + expect(response.status).to eq(404) + expect(json_response['message']).to eq('Tag does not exist') + end end end |