Commit 3ea05c5b authored by Robert Schilling's avatar Robert Schilling

Only allow to create a release if it does not exist yet

parent 6f7e90f6
...@@ -8,12 +8,18 @@ class CreateReleaseService < BaseService ...@@ -8,12 +8,18 @@ class CreateReleaseService < BaseService
# Only create a release if the tag exists # Only create a release if the tag exists
if existing_tag if existing_tag
release = project.releases.find_or_initialize_by(tag: tag_name) release = project.releases.find_by(tag: tag_name)
release.update_attributes(description: release_description)
if(release)
error('Release already exists', 409)
else
release = project.releases.new({ tag: tag_name, description: release_description })
release.save
success(release) success(release)
end
else else
error('Tag does not exist') error('Tag does not exist', 404)
end end
end end
......
...@@ -87,7 +87,8 @@ It returns 200 if the operation succeed. In case of an error, ...@@ -87,7 +87,8 @@ It returns 200 if the operation succeed. In case of an error,
## Create a new release ## Create a new release
Add release notes to the existing git tag. It returns 201 if the release is Add release notes to the existing git tag. It returns 201 if the release is
created successfully. If the tag does not exist, 404 is returned. created successfully. If the tag does not exist, 404 is returned. If there
already exists a release for the given tag, 409 is returned.
``` ```
POST /projects/:id/repository/tags/:tag_name/release POST /projects/:id/repository/tags/:tag_name/release
......
...@@ -47,7 +47,7 @@ module API ...@@ -47,7 +47,7 @@ module API
# tag_name (required) - The name of the tag # tag_name (required) - The name of the tag
# description (required) - Release notes with markdown support # description (required) - Release notes with markdown support
# Example Request: # Example Request:
# PUT /projects/:id/repository/tags/:tag_name/release # POST /projects/:id/repository/tags/:tag_name/release
post ':id/repository/tags/:tag_name/release', requirements: { tag_name: /.*/ } do post ':id/repository/tags/:tag_name/release', requirements: { tag_name: /.*/ } do
authorize_push_project authorize_push_project
required_attributes! [:description] required_attributes! [:description]
...@@ -57,7 +57,7 @@ module API ...@@ -57,7 +57,7 @@ module API
if result[:status] == :success if result[:status] == :success
present result[:release], with: Entities::Release present result[:release], with: Entities::Release
else else
render_api_error!(result[:message], 404) render_api_error!(result[:message], result[:http_status])
end end
end end
end end
......
...@@ -28,10 +28,10 @@ describe API::API, api: true do ...@@ -28,10 +28,10 @@ describe API::API, api: true do
before do before do
release = project.releases.find_or_initialize_by(tag: tag_name) release = project.releases.find_or_initialize_by(tag: tag_name)
release.update_attributes(description: description) release.update_attributes(description: description)
get api("/projects/#{project.id}/repository/tags", user)
end end
it "should return an array of project tags with release info" do it "should return an array of project tags with release info" do
get api("/projects/#{project.id}/repository/tags", user)
expect(response.status).to eq(200) expect(response.status).to eq(200)
expect(json_response).to be_an Array expect(json_response).to be_an Array
expect(json_response.first['name']).to eq(tag_name) expect(json_response.first['name']).to eq(tag_name)
...@@ -139,5 +139,20 @@ describe API::API, api: true do ...@@ -139,5 +139,20 @@ describe API::API, api: true do
expect(response.status).to eq(404) expect(response.status).to eq(404)
expect(json_response['message']).to eq('Tag does not exist') expect(json_response['message']).to eq('Tag does not exist')
end end
context 'on tag with existing release' do
before do
release = project.releases.find_or_initialize_by(tag: tag_name)
release.update_attributes(description: description)
end
it 'should return 409 if there is already a release' do
post api("/projects/#{project.id}/repository/tags/#{tag_name}/release", user),
description: description
expect(response.status).to eq(409)
expect(json_response['message']).to eq('Release already exists')
end
end
end 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