Commit 6fdd9008272f268b05aa498a79dc200da52f7bbc
Exists in
spb-stable
and in
2 other branches
Merge pull request #7014 from asedge/api_create_tag
Add CreateTagService. Use new service to allow tag creation through API.
Showing
5 changed files
with
83 additions
and
5 deletions
Show diff stats
app/controllers/projects/tags_controller.rb
| ... | ... | @@ -13,11 +13,8 @@ class Projects::TagsController < Projects::ApplicationController |
| 13 | 13 | end |
| 14 | 14 | |
| 15 | 15 | def create |
| 16 | - @repository.add_tag(params[:tag_name], params[:ref]) | |
| 17 | - | |
| 18 | - if new_tag = @repository.find_tag(params[:tag_name]) | |
| 19 | - Event.create_ref_event(@project, current_user, new_tag, 'add', 'refs/tags') | |
| 20 | - end | |
| 16 | + @tag = CreateTagService.new.execute(@project, params[:tag_name], | |
| 17 | + params[:ref], current_user) | |
| 21 | 18 | |
| 22 | 19 | redirect_to project_tags_path(@project) |
| 23 | 20 | end | ... | ... |
| ... | ... | @@ -0,0 +1,13 @@ |
| 1 | +class CreateTagService | |
| 2 | + def execute(project, tag_name, ref, current_user) | |
| 3 | + repository = project.repository | |
| 4 | + repository.add_tag(tag_name, ref) | |
| 5 | + new_tag = repository.find_tag(tag_name) | |
| 6 | + | |
| 7 | + if new_tag | |
| 8 | + Event.create_ref_event(project, current_user, new_tag, 'add', 'refs/tags') | |
| 9 | + end | |
| 10 | + | |
| 11 | + new_tag | |
| 12 | + end | |
| 13 | +end | ... | ... |
doc/api/repositories.md
| ... | ... | @@ -37,6 +37,40 @@ Parameters: |
| 37 | 37 | ] |
| 38 | 38 | ``` |
| 39 | 39 | |
| 40 | +## Create a new tag | |
| 41 | + | |
| 42 | +Creates new tag in the repository that points to the supplied ref. | |
| 43 | + | |
| 44 | +``` | |
| 45 | +POST /projects/:id/repository/tags | |
| 46 | +``` | |
| 47 | + | |
| 48 | +Parameters: | |
| 49 | + | |
| 50 | ++ `id` (required) - The ID of a project | |
| 51 | ++ `tag_name` (required) - The name of a tag | |
| 52 | ++ `ref` (required) - Create tag using commit sha, another tag name, or branch name. | |
| 53 | + | |
| 54 | +```json | |
| 55 | +[ | |
| 56 | + { | |
| 57 | + "name": "v1.0.0", | |
| 58 | + "commit": { | |
| 59 | + "id": "2695effb5807a22ff3d138d593fd856244e155e7", | |
| 60 | + "parents": [], | |
| 61 | + "message": "Initial commit", | |
| 62 | + "authored_date": "2012-05-28T04:42:42-07:00", | |
| 63 | + "author_name": "John Smith", | |
| 64 | + "author email": "john@example.com", | |
| 65 | + "committer_name": "Jack Smith", | |
| 66 | + "committed_date": "2012-05-28T04:42:42-07:00", | |
| 67 | + "committer_email": "jack@example.com" | |
| 68 | + }, | |
| 69 | + "protected": false | |
| 70 | + } | |
| 71 | +] | |
| 72 | +``` | |
| 73 | + | |
| 40 | 74 | ## List repository tree |
| 41 | 75 | |
| 42 | 76 | Get a list of repository files and directories in a project. | ... | ... |
lib/api/repositories.rb
| ... | ... | @@ -26,6 +26,22 @@ module API |
| 26 | 26 | present user_project.repo.tags.sort_by(&:name).reverse, with: Entities::RepoObject, project: user_project |
| 27 | 27 | end |
| 28 | 28 | |
| 29 | + # Create tag | |
| 30 | + # | |
| 31 | + # Parameters: | |
| 32 | + # id (required) - The ID of a project | |
| 33 | + # tag_name (required) - The name of the tag | |
| 34 | + # ref (required) - Create tag from commit sha or branch | |
| 35 | + # Example Request: | |
| 36 | + # POST /projects/:id/repository/tags | |
| 37 | + post ':id/repository/tags' do | |
| 38 | + authorize_push_project | |
| 39 | + @tag = CreateTagService.new.execute(user_project, params[:tag_name], | |
| 40 | + params[:ref], current_user) | |
| 41 | + | |
| 42 | + present @tag, with: Entities::RepoObject, project: user_project | |
| 43 | + end | |
| 44 | + | |
| 29 | 45 | # Get a project repository tree |
| 30 | 46 | # |
| 31 | 47 | # Parameters: | ... | ... |
spec/requests/api/repositories_spec.rb
| ... | ... | @@ -23,6 +23,24 @@ describe API::API, api: true do |
| 23 | 23 | end |
| 24 | 24 | end |
| 25 | 25 | |
| 26 | + describe 'POST /projects/:id/repository/tags' do | |
| 27 | + it 'should create a new tag' do | |
| 28 | + post api("/projects/#{project.id}/repository/tags", user), | |
| 29 | + tag_name: 'v1.0.0', | |
| 30 | + ref: 'master' | |
| 31 | + | |
| 32 | + response.status.should == 201 | |
| 33 | + json_response['name'].should == 'v1.0.0' | |
| 34 | + end | |
| 35 | + it 'should deny for user without push access' do | |
| 36 | + post api("/projects/#{project.id}/repository/tags", user2), | |
| 37 | + tag_name: 'v1.0.0', | |
| 38 | + ref: '621491c677087aa243f165eab467bfdfbee00be1' | |
| 39 | + | |
| 40 | + response.status.should == 403 | |
| 41 | + end | |
| 42 | + end | |
| 43 | + | |
| 26 | 44 | describe "GET /projects/:id/repository/tree" do |
| 27 | 45 | context "authorized user" do |
| 28 | 46 | before { project.team << [user2, :reporter] } | ... | ... |