releases.rb 4.51 KB
Newer Older
Alessio Caiazza's avatar
Alessio Caiazza committed
1 2 3 4 5 6
# frozen_string_literal: true

module API
  class Releases < Grape::API
    include PaginationParams

Shinya Maeda's avatar
Shinya Maeda committed
7 8
    RELEASE_ENDPOINT_REQUIREMETS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS
      .merge(tag_name: API::NO_SLASH_URL_PART_REGEX)
Alessio Caiazza's avatar
Alessio Caiazza committed
9 10

    before { error!('404 Not Found', 404) unless Feature.enabled?(:releases_page, user_project) }
Shinya Maeda's avatar
Shinya Maeda committed
11
    before { authorize_read_releases! }
Alessio Caiazza's avatar
Alessio Caiazza committed
12 13 14 15 16 17 18 19 20 21 22 23 24

    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
      desc 'Get a project releases' do
        detail 'This feature was introduced in GitLab 11.7.'
        success Entities::Release
      end
      params do
        use :pagination
      end
      get ':id/releases' do
25
        releases = ::ReleasesFinder.new(user_project, current_user).execute
Alessio Caiazza's avatar
Alessio Caiazza committed
26 27 28 29 30 31 32 33 34

        present paginate(releases), with: Entities::Release
      end

      desc 'Get a single project release' do
        detail 'This feature was introduced in GitLab 11.7.'
        success Entities::Release
      end
      params do
Shinya Maeda's avatar
Shinya Maeda committed
35
        requires :tag_name, type: String, desc: 'The name of the tag', as: :tag
Alessio Caiazza's avatar
Alessio Caiazza committed
36 37
      end
      get ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMETS do
Shinya Maeda's avatar
Shinya Maeda committed
38
        authorize_read_release!
Alessio Caiazza's avatar
Alessio Caiazza committed
39 40 41 42 43 44 45 46 47

        present release, with: Entities::Release
      end

      desc 'Create a new release' do
        detail 'This feature was introduced in GitLab 11.7.'
        success Entities::Release
      end
      params do
Shinya Maeda's avatar
Shinya Maeda committed
48 49 50 51
        requires :tag_name,    type: String, desc: 'The name of the tag', as: :tag
        requires :name,        type: String, desc: 'The name of the release'
        requires :description, type: String, desc: 'The release notes'
        optional :ref,         type: String, desc: 'The commit sha or branch name'
52 53 54 55 56
        optional :assets, type: Hash do
          optional :links, type: Array do
            requires :name, type: String
            requires :url, type: String
          end
57
        end
Alessio Caiazza's avatar
Alessio Caiazza committed
58 59 60 61
      end
      post ':id/releases' do
        authorize_create_release!

Shinya Maeda's avatar
Shinya Maeda committed
62 63 64
        result = ::Releases::CreateService
          .new(user_project, current_user, declared_params(include_missing: false))
          .execute
Alessio Caiazza's avatar
Alessio Caiazza committed
65 66 67 68

        if result[:status] == :success
          present result[:release], with: Entities::Release
        else
Shinya Maeda's avatar
Shinya Maeda committed
69
          render_api_error!(result[:message], result[:http_status])
Alessio Caiazza's avatar
Alessio Caiazza committed
70 71 72 73 74 75 76 77
        end
      end

      desc 'Update a release' do
        detail 'This feature was introduced in GitLab 11.7.'
        success Entities::Release
      end
      params do
78
        requires :tag_name,    type: String, desc: 'The name of the tag', as: :tag
Shinya Maeda's avatar
Shinya Maeda committed
79 80
        optional :name,        type: String, desc: 'The name of the release'
        optional :description, type: String, desc: 'Release notes with markdown support'
Alessio Caiazza's avatar
Alessio Caiazza committed
81 82 83 84
      end
      put ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMETS do
        authorize_update_release!

Shinya Maeda's avatar
Shinya Maeda committed
85 86 87
        result = ::Releases::UpdateService
          .new(user_project, current_user, declared_params(include_missing: false))
          .execute
Alessio Caiazza's avatar
Alessio Caiazza committed
88 89 90 91 92 93 94

        if result[:status] == :success
          present result[:release], with: Entities::Release
        else
          render_api_error!(result[:message], result[:http_status])
        end
      end
95 96 97 98 99 100 101 102 103

      desc 'Delete a release' do
        detail 'This feature was introduced in GitLab 11.7.'
        success Entities::Release
      end
      params do
        requires :tag_name,    type: String, desc: 'The name of the tag', as: :tag
      end
      delete ':id/releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMETS do
Shinya Maeda's avatar
Shinya Maeda committed
104
        authorize_destroy_release!
105

Shinya Maeda's avatar
Shinya Maeda committed
106 107 108
        result = ::Releases::DestroyService
          .new(user_project, current_user, declared_params(include_missing: false))
          .execute
109 110 111 112 113 114 115

        if result[:status] == :success
          present result[:release], with: Entities::Release
        else
          render_api_error!(result[:message], result[:http_status])
        end
      end
Alessio Caiazza's avatar
Alessio Caiazza committed
116
    end
Shinya Maeda's avatar
Shinya Maeda committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

    helpers do
      def authorize_create_release!
        authorize! :create_release, user_project
      end

      def authorize_read_releases!
        authorize! :read_release, user_project
      end

      def authorize_read_release!
        authorize! :read_release, release
      end

      def authorize_update_release!
        authorize! :update_release, release
      end

      def authorize_destroy_release!
        authorize! :destroy_release, release
      end

      def release
        @release ||= user_project.releases.find_by_tag(params[:tag])
      end
    end
Alessio Caiazza's avatar
Alessio Caiazza committed
143 144
  end
end