Commit faef95af authored by Robert Schilling's avatar Robert Schilling

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

parent 2cba93a0
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
......@@ -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')
......
......@@ -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
......
......@@ -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
......
......@@ -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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment