API: Return 404 if the tag for a release does not exist

This commit is contained in:
Robert Schilling
2015-11-21 18:08:45 +01:00
parent 2cba93a0d2
commit faef95af1a
5 changed files with 47 additions and 9 deletions
+25
View File
@@ -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
+5 -5
View File
@@ -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')
+2 -1
View File
@@ -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
+7 -3
View File
@@ -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
+8
View File
@@ -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