summaryrefslogtreecommitdiff
path: root/lib/api/applications.rb
blob: 048e7bc81f93a47156a8c05a7cd8b85a03e9f466 (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
# frozen_string_literal: true

module API
  # External applications API
  class Applications < Grape::API
    before { authenticated_as_admin! }

    resource :applications do
      desc 'Create a new application' do
        detail 'This feature was introduced in GitLab 10.5'
        success Entities::ApplicationWithSecret
      end
      params do
        requires :name, type: String, desc: 'Application name'
        requires :redirect_uri, type: String, desc: 'Application redirect URI'
        requires :scopes, type: String, desc: 'Application scopes'
      end
      post do
        application = Doorkeeper::Application.new(declared_params)

        if application.save
          present application, with: Entities::ApplicationWithSecret
        else
          render_validation_error! application
        end
      end

      # rubocop: disable CodeReuse/ActiveRecord
      desc 'Get applications' do
        success Entities::ApplicationWithSecret
      end
      get do
        applications = Doorkeeper::Application.where("owner_id IS NULL")
        present applications, with: Entities::Application
      end
      # rubocop: enable CodeReuse/ActiveRecord

      # rubocop: disable CodeReuse/ActiveRecord
      desc 'Delete an application'
      delete ':id' do
        Doorkeeper::Application.find_by(id: params[:id]).destroy

        status 204
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end