@@ -2,74 +2,87 @@ module API
2
2
# Projects API
3
3
class DeployKeys < Grape ::API
4
4
before { authenticate! }
5
- before { authorize_admin_project }
5
+
6
+ get "deploy_keys" do
7
+ authenticated_as_admin!
8
+
9
+ keys = DeployKey . all
10
+ present keys , with : Entities ::SSHKey
11
+ end
6
12
7
13
resource :projects do
8
- # Get a specific project's keys
9
- #
10
- # Example Request:
11
- # GET /projects/:id/keys
12
- get ":id/keys" do
13
- present user_project . deploy_keys , with : Entities ::SSHKey
14
- end
14
+ before { authorize_admin_project }
15
15
16
- # Get single key owned by currently authenticated user
16
+ # Routing "projects/:id/keys/..." is DEPRECATED and WILL BE REMOVED in version 9.0
17
+ # Use "projects/:id/deploy_keys/..." instead.
17
18
#
18
- # Example Request:
19
- # GET /projects/:id/keys/:id
20
- get ":id/keys/:key_id" do
21
- key = user_project . deploy_keys . find params [ :key_id ]
22
- present key , with : Entities ::SSHKey
23
- end
19
+ %w( keys deploy_keys ) . each do |path |
20
+ # Get a specific project's deploy keys
21
+ #
22
+ # Example Request:
23
+ # GET /projects/:id/deploy_keys
24
+ get ":id/#{ path } " do
25
+ present user_project . deploy_keys , with : Entities ::SSHKey
26
+ end
24
27
25
- # Add new ssh key to currently authenticated user
26
- # If deploy key already exists - it will be joined to project
27
- # but only if original one was is accessible by same user
28
- #
29
- # Parameters:
30
- # key (required) - New SSH Key
31
- # title (required) - New SSH Key's title
32
- # Example Request:
33
- # POST /projects/:id/keys
34
- post ":id/keys" do
35
- attrs = attributes_for_keys [ :title , :key ]
28
+ # Get single deploy key owned by currently authenticated user
29
+ #
30
+ # Example Request:
31
+ # GET /projects/:id/deploy_keys/:key_id
32
+ get ":id/#{ path } /:key_id" do
33
+ key = user_project . deploy_keys . find params [ :key_id ]
34
+ present key , with : Entities ::SSHKey
35
+ end
36
36
37
- if attrs [ :key ] . present?
38
- attrs [ :key ] . strip!
37
+ # Add new deploy key to currently authenticated user
38
+ # If deploy key already exists - it will be joined to project
39
+ # but only if original one was accessible by same user
40
+ #
41
+ # Parameters:
42
+ # key (required) - New deploy Key
43
+ # title (required) - New deploy Key's title
44
+ # Example Request:
45
+ # POST /projects/:id/deploy_keys
46
+ post ":id/#{ path } " do
47
+ attrs = attributes_for_keys [ :title , :key ]
39
48
40
- # check if key already exist in project
41
- key = user_project . deploy_keys . find_by ( key : attrs [ :key ] )
42
- if key
43
- present key , with : Entities ::SSHKey
44
- return
49
+ if attrs [ :key ] . present?
50
+ attrs [ :key ] . strip!
51
+
52
+ # check if key already exist in project
53
+ key = user_project . deploy_keys . find_by ( key : attrs [ :key ] )
54
+ if key
55
+ present key , with : Entities ::SSHKey
56
+ next
57
+ end
58
+
59
+ # Check for available deploy keys in other projects
60
+ key = current_user . accessible_deploy_keys . find_by ( key : attrs [ :key ] )
61
+ if key
62
+ user_project . deploy_keys << key
63
+ present key , with : Entities ::SSHKey
64
+ next
65
+ end
45
66
end
46
67
47
- # Check for available deploy keys in other projects
48
- key = current_user . accessible_deploy_keys . find_by ( key : attrs [ :key ] )
49
- if key
50
- user_project . deploy_keys << key
68
+ key = DeployKey . new attrs
69
+
70
+ if key . valid? && user_project . deploy_keys << key
51
71
present key , with : Entities ::SSHKey
52
- return
72
+ else
73
+ render_validation_error! ( key )
53
74
end
54
75
end
55
76
56
- key = DeployKey . new attrs
57
-
58
- if key . valid? && user_project . deploy_keys << key
59
- present key , with : Entities ::SSHKey
60
- else
61
- render_validation_error! ( key )
77
+ # Delete existing deploy key of currently authenticated user
78
+ #
79
+ # Example Request:
80
+ # DELETE /projects/:id/deploy_keys/:key_id
81
+ delete ":id/#{ path } /:key_id" do
82
+ key = user_project . deploy_keys . find params [ :key_id ]
83
+ key . destroy
62
84
end
63
85
end
64
-
65
- # Delete existed ssh key of currently authenticated user
66
- #
67
- # Example Request:
68
- # DELETE /projects/:id/keys/:id
69
- delete ":id/keys/:key_id" do
70
- key = user_project . deploy_keys . find params [ :key_id ]
71
- key . destroy
72
- end
73
86
end
74
87
end
75
88
end
0 commit comments