summaryrefslogtreecommitdiff
path: root/lib/api/tags.rb
blob: 731a68082ba863520907a631bc47d6e805ee9b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
module API
  # Git Tags API
  class Tags < Grape::API
    before { authenticate! }
    before { authorize! :download_code, user_project }

    resource :projects do
      # Get a project repository tags
      #
      # Parameters:
      #   id (required) - The ID of a project
      # Example Request:
      #   GET /projects/:id/repository/tags
      get ":id/repository/tags" do
        present user_project.repo.tags.sort_by(&:name).reverse,
                with: Entities::RepoTag, project: user_project
      end

      # Get a single repository tag
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   tag_name (required) - The name of the tag
      # Example Request:
      #   GET /projects/:id/repository/tags/:tag_name
      get ":id/repository/tags/:tag_name", requirements: { tag_name: /.*/ } do
        tag = user_project.repository.find_tag(params[:tag_name])
        not_found!('Tag') unless tag

        present tag,  with: Entities::RepoTag, project: user_project
      end

      # Create tag
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   tag_name (required) - The name of the tag
      #   ref (required) - Create tag from commit sha or branch
      #   message (optional) - Specifying a message creates an annotated tag.
      # Example Request:
      #   POST /projects/:id/repository/tags
      post ':id/repository/tags' do
        authorize_push_project
        message = params[:message] || nil
        result = CreateTagService.new(user_project, current_user).
          execute(params[:tag_name], params[:ref], message, params[:release_description])

        if result[:status] == :success
          present result[:tag],
                  with: Entities::RepoTag,
                  project: user_project
        else
          render_api_error!(result[:message], 400)
        end
      end

      # Delete tag
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   tag_name (required) - The name of the tag
      # Example Request:
      #   DELETE /projects/:id/repository/tags/:tag
      delete ":id/repository/tags/:tag_name", requirements: { tag_name: /.*/ } do
        authorize_push_project
        result = DeleteTagService.new(user_project, current_user).
          execute(params[:tag_name])

        if result[:status] == :success
          {
            tag_name: params[:tag_name]
          }
        else
          render_api_error!(result[:message], result[:return_code])
        end
      end

      # Add release notes to tag
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   tag_name (required) - The name of the tag
      #   description (required) - Release notes with markdown support
      # Example Request:
      #   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]
        result = CreateReleaseService.new(user_project, current_user).
          execute(params[:tag_name], params[:description])

        if result[:status] == :success
          present result[:release], with: Entities::Release
        else
          render_api_error!(result[:message], result[:http_status])
        end
      end

      # Updates a release notes of a tag
      #
      # Parameters:
      #   id (required) - The ID of a project
      #   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
      put ':id/repository/tags/:tag_name/release', requirements: { tag_name: /.*/ } do
        authorize_push_project
        required_attributes! [:description]
        result = UpdateReleaseService.new(user_project, current_user).
          execute(params[:tag_name], params[:description])

        if result[:status] == :success
          present result[:release], with: Entities::Release
        else
          render_api_error!(result[:message], result[:http_status])
        end
      end
    end
  end
end