Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
7d0a4a89
Commit
7d0a4a89
authored
Jun 30, 2017
by
Greg Messner
Browse files
Added SSH key support to UserApi (#37).
parent
ba4cd1f5
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/UserApi.java
View file @
7d0a4a89
...
...
@@ -7,6 +7,7 @@ import javax.ws.rs.core.GenericType;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.SshKey
;
import
org.gitlab4j.api.models.User
;
/**
...
...
@@ -214,10 +215,134 @@ public class UserApi extends AbstractApi {
deleteUser
(
user
.
getId
());
}
/**
* Get a list of currently authenticated user's SSH keys.
*
* GET /user/keys
*
* @return a list of currently authenticated user's SSH keys
* @throws GitLabApiException if any exception occurs
*/
public
List
<
SshKey
>
getSshKeys
()
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"user"
,
"keys"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
SshKey
>>()
{}));
}
/**
* Get a list of a specified user's SSH keys. Available only for admin users.
*
* GET /users/:id/keys
*
* @param userId the ID of the user to get SSH keys for
* @return a list of a specified user's SSH keys
* @throws GitLabApiException if any exception occurs
*/
public
List
<
SshKey
>
getSshKeys
(
Integer
userId
)
throws
GitLabApiException
{
if
(
userId
==
null
)
{
throw
new
RuntimeException
(
"userId cannot be null"
);
}
Response
response
=
get
(
Response
.
Status
.
OK
,
getDefaultPerPageParam
(),
"users"
,
userId
,
"keys"
);
return
(
response
.
readEntity
(
new
GenericType
<
List
<
SshKey
>>()
{}));
}
/**
* Get a single SSH Key.
*
* GET /user/keys/:key_id
*
* @param keyId the ID of the SSH key.
* @return an SshKey instance holding the info on the SSH key specified by keyId
* @throws GitLabApiException if any exception occurs
*/
public
SshKey
getSshKey
(
Integer
keyId
)
throws
GitLabApiException
{
Response
response
=
get
(
Response
.
Status
.
OK
,
null
,
"user"
,
"keys"
,
keyId
);
return
(
response
.
readEntity
(
SshKey
.
class
));
}
/**
* Creates a new key owned by the currently authenticated user.
*
* POST /user/keys
*
* @param title the new SSH Key's title
* @param key the new SSH key
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public
SshKey
addSshKey
(
String
title
,
String
key
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"title"
,
title
).
withParam
(
"key"
,
key
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"user"
,
"keys"
);
return
(
response
.
readEntity
(
SshKey
.
class
));
}
/**
* Create new key owned by specified user. Available only for admin users.
*
* POST /users/:id/keys
*
* @param userId the ID of the user to add the SSH key for
* @param title the new SSH Key's title
* @param key the new SSH key
* @return an SshKey instance with info on the added SSH key
* @throws GitLabApiException if any exception occurs
*/
public
SshKey
addSshKey
(
Integer
userId
,
String
title
,
String
key
)
throws
GitLabApiException
{
if
(
userId
==
null
)
{
throw
new
RuntimeException
(
"userId cannot be null"
);
}
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"title"
,
title
).
withParam
(
"key"
,
key
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"users"
,
userId
,
"keys"
);
return
(
response
.
readEntity
(
SshKey
.
class
));
}
/**
* Deletes key owned by currently authenticated user. This is an idempotent function and calling it
* on a key that is already deleted or not available results in success.
*
* DELETE /user/keys/:key_id
*
* @param keyId the key ID to delete
* @throws GitLabApiException if any exception occurs
*/
public
void
deleteSshKey
(
Integer
keyId
)
throws
GitLabApiException
{
if
(
keyId
==
null
)
{
throw
new
RuntimeException
(
"keyId cannot be null"
);
}
delete
(
Response
.
Status
.
OK
,
null
,
"user"
,
"keys"
,
keyId
);
}
/**
* Deletes key owned by a specified user. Available only for admin users.
*
* DELETE /users/:id/keys/:key_id
*
* @param userId the user ID of the user to delete the key for
* @param keyId the key ID to delete
* @throws GitLabApiException if any exception occurs
*/
public
void
deleteSshKey
(
Integer
userId
,
Integer
keyId
)
throws
GitLabApiException
{
if
(
userId
==
null
)
{
throw
new
RuntimeException
(
"userId cannot be null"
);
}
if
(
keyId
==
null
)
{
throw
new
RuntimeException
(
"keyId cannot be null"
);
}
delete
(
Response
.
Status
.
OK
,
null
,
"users"
,
userId
,
"keys"
,
keyId
);
}
/**
* Populate the REST form with data from the User instance.
*
* @param user the User i
u
nstance to populate the Form instance with
* @param user the User instance to populate the Form instance with
* @param projectsLimit the maximum number of projects the user is allowed (optional)
* @param password the password, required when creating a new user
* @param create whether the form is being populated to create a new user
...
...
src/main/java/org/gitlab4j/api/models/SshKey.java
0 → 100644
View file @
7d0a4a89
package
org.gitlab4j.api.models
;
import
java.util.Date
;
import
javax.xml.bind.annotation.XmlAccessType
;
import
javax.xml.bind.annotation.XmlAccessorType
;
@XmlAccessorType
(
XmlAccessType
.
FIELD
)
public
class
SshKey
{
private
Integer
id
;
private
String
title
;
private
String
key
;
private
Date
createdAt
;
public
Integer
getId
()
{
return
id
;
}
public
void
setId
(
Integer
id
)
{
this
.
id
=
id
;
}
public
String
getTitle
()
{
return
title
;
}
public
void
setTitle
(
String
title
)
{
this
.
title
=
title
;
}
public
String
getKey
()
{
return
key
;
}
public
void
setKey
(
String
key
)
{
this
.
key
=
key
;
}
public
Date
getCreatedAt
()
{
return
createdAt
;
}
public
void
setCreatedAt
(
Date
createdAt
)
{
this
.
createdAt
=
createdAt
;
}
}
src/test/java/org/gitlab4j/api/TestGitLabApiBeans.java
View file @
7d0a4a89
...
...
@@ -24,6 +24,7 @@ import org.gitlab4j.api.models.Project;
import
org.gitlab4j.api.models.ProjectHook
;
import
org.gitlab4j.api.models.ProjectSnippet
;
import
org.gitlab4j.api.models.Session
;
import
org.gitlab4j.api.models.SshKey
;
import
org.gitlab4j.api.models.SystemHook
;
import
org.gitlab4j.api.models.Tag
;
import
org.gitlab4j.api.models.TreeItem
;
...
...
@@ -277,6 +278,17 @@ public class TestGitLabApiBeans {
}
}
@Test
public
void
testSshKey
()
{
try
{
SshKey
sshKey
=
makeFakeApiCall
(
SshKey
.
class
,
"sshkey"
);
assertTrue
(
compareJson
(
sshKey
,
"sshkey"
));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
@Test
public
void
testTree
()
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment