diff options
author | Robert Schilling <rschilling@student.tugraz.at> | 2015-11-22 00:24:34 +0300 |
---|---|---|
committer | Robert Schilling <rschilling@student.tugraz.at> | 2015-11-22 00:24:34 +0300 |
commit | 3ea05c5b5b253de33d8bf8d615c66e2935b940ef (patch) | |
tree | 486d420d9c21ebe85b5b296685a9328179cb41dd | |
parent | 6f7e90f6dba300591281aba08ffbe30ce3cc5c90 (diff) |
Only allow to create a release if it does not exist yet
-rw-r--r-- | app/services/create_release_service.rb | 14 | ||||
-rw-r--r-- | doc/api/tags.md | 3 | ||||
-rw-r--r-- | lib/api/tags.rb | 4 | ||||
-rw-r--r-- | spec/requests/api/tags_spec.rb | 17 |
4 files changed, 30 insertions, 8 deletions
diff --git a/app/services/create_release_service.rb b/app/services/create_release_service.rb index 355374ce252..54e87478e64 100644 --- a/app/services/create_release_service.rb +++ b/app/services/create_release_service.rb @@ -8,12 +8,18 @@ class CreateReleaseService < BaseService # 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) + release = project.releases.find_by(tag: tag_name) - success(release) + if(release) + error('Release already exists', 409) + else + release = project.releases.new({ tag: tag_name, description: release_description }) + release.save + + success(release) + end else - error('Tag does not exist') + error('Tag does not exist', 404) end end diff --git a/doc/api/tags.md b/doc/api/tags.md index ab135117250..bc61aa1118f 100644 --- a/doc/api/tags.md +++ b/doc/api/tags.md @@ -87,7 +87,8 @@ It returns 200 if the operation succeed. In case of an error, ## Create a new release 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 diff --git a/lib/api/tags.rb b/lib/api/tags.rb index 48f630d58e5..e46d9bb96f0 100644 --- a/lib/api/tags.rb +++ b/lib/api/tags.rb @@ -47,7 +47,7 @@ module API # tag_name (required) - The name of the tag # description (required) - Release notes with markdown support # 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 authorize_push_project required_attributes! [:description] @@ -57,7 +57,7 @@ module API if result[:status] == :success present result[:release], with: Entities::Release else - render_api_error!(result[:message], 404) + render_api_error!(result[:message], result[:http_status]) end end end diff --git a/spec/requests/api/tags_spec.rb b/spec/requests/api/tags_spec.rb index 0ee819ae445..0a456fc3b5d 100644 --- a/spec/requests/api/tags_spec.rb +++ b/spec/requests/api/tags_spec.rb @@ -28,10 +28,10 @@ describe API::API, api: true do before do release = project.releases.find_or_initialize_by(tag: tag_name) release.update_attributes(description: description) - get api("/projects/#{project.id}/repository/tags", user) end 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(json_response).to be_an Array expect(json_response.first['name']).to eq(tag_name) @@ -139,5 +139,20 @@ describe API::API, api: true do expect(response.status).to eq(404) expect(json_response['message']).to eq('Tag does not exist') 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 |