summaryrefslogtreecommitdiff
path: root/lib/api/release/links.rb
blob: 0e83d086a6eccf6cf29cf512e807fcae8a4c5f6c (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# frozen_string_literal: true

module API
  module Release
    class Links < ::API::Base
      include PaginationParams

      release_links_tags = %w[release_links]

      RELEASE_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS
        .merge(tag_name: API::NO_SLASH_URL_PART_REGEX)

      after_validation { authorize! :read_release, user_project }

      feature_category :release_orchestration
      urgency :low

      params do
        requires :id, types: [String, Integer], desc: 'The ID or URL-encoded path of the project'
      end
      resource 'projects/:id', requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
        params do
          requires :tag_name, type: String, desc: 'The tag associated with the release', as: :tag
        end
        resource 'releases/:tag_name', requirements: RELEASE_ENDPOINT_REQUIREMENTS do
          resource :assets do
            desc 'List links of a release' do
              detail 'Get assets as links from a release. This feature was introduced in GitLab 11.7.'
              success Entities::Releases::Link
              failure [
                { code: 401, message: 'Unauthorized' },
                { code: 404, message: 'Not found' }
              ]
              is_array true
              tags release_links_tags
            end
            params do
              use :pagination
            end
            route_setting :authentication, job_token_allowed: true
            get 'links' do
              authorize! :read_release, release

              present paginate(release.links.sorted), with: Entities::Releases::Link
            end

            desc 'Create a release link' do
              detail 'Create an asset as a link from a release. This feature was introduced in GitLab 11.7.'
              success Entities::Releases::Link
              failure [
                { code: 400, message: 'Bad request' },
                { code: 401, message: 'Unauthorized' }
              ]
              tags release_links_tags
            end
            params do
              requires :name, type: String, desc: 'The name of the link. Link names must be unique in the release'
              requires :url, type: String, desc: 'The URL of the link. Link URLs must be unique in the release.'
              optional :filepath, type: String, desc: 'Optional path for a direct asset link'
              optional :link_type,
                type: String,
                values: %w[other runbook image package],
                default: 'other',
                desc: 'The type of the link: `other`, `runbook`, `image`, or `package`. Defaults to `other`'
            end
            route_setting :authentication, job_token_allowed: true
            post 'links' do
              authorize! :create_release, release

              new_link = release.links.create(declared_params(include_missing: false))

              if new_link.persisted?
                present new_link, with: Entities::Releases::Link
              else
                render_api_error!(new_link.errors.messages, 400)
              end
            end

            params do
              requires :link_id, type: Integer, desc: 'The ID of the link'
            end
            resource 'links/:link_id' do
              desc 'Get a release link' do
                detail 'Get an asset as a link from a release. This feature was introduced in GitLab 11.7.'
                success Entities::Releases::Link
                failure [
                  { code: 401, message: 'Unauthorized' },
                  { code: 404, message: 'Not found' }
                ]
                tags release_links_tags
              end
              route_setting :authentication, job_token_allowed: true
              get do
                authorize! :read_release, release

                present link, with: Entities::Releases::Link
              end

              desc 'Update a release link' do
                detail 'Update an asset as a link from a release. This feature was introduced in GitLab 11.7.'
                success Entities::Releases::Link
                failure [
                  { code: 400, message: 'Bad request' },
                  { code: 401, message: 'Unauthorized' }
                ]
                tags release_links_tags
              end
              params do
                optional :name, type: String, desc: 'The name of the link'
                optional :url, type: String, desc: 'The URL of the link'
                optional :filepath, type: String, desc: 'Optional path for a direct asset link'
                optional :link_type,
                  type: String,
                  values: %w[other runbook image package],
                  default: 'other',
                  desc: 'The type of the link: `other`, `runbook`, `image`, or `package`. Defaults to `other`'

                at_least_one_of :name, :url
              end
              route_setting :authentication, job_token_allowed: true
              put do
                authorize! :update_release, release

                if link.update(declared_params(include_missing: false))
                  present link, with: Entities::Releases::Link
                else
                  render_api_error!(link.errors.messages, 400)
                end
              end

              desc 'Delete a release link' do
                detail 'Deletes an asset as a link from a release. This feature was introduced in GitLab 11.7.'
                success Entities::Releases::Link
                failure [
                  { code: 400, message: 'Bad request' },
                  { code: 401, message: 'Unauthorized' }
                ]
                tags release_links_tags
              end
              route_setting :authentication, job_token_allowed: true
              delete do
                authorize! :destroy_release, release

                if link.destroy
                  present link, with: Entities::Releases::Link
                else
                  render_api_error!(link.errors.messages, 400)
                end
              end
            end
          end
        end
      end

      helpers do
        def release
          @release ||= user_project.releases.find_by_tag!(params[:tag])
        end

        def link
          @link ||= release.links.find(params[:link_id])
        end
      end
    end
  end
end