Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
java-gitlab-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
java-gitlab-api
Commits
3f4c7ead
Commit
3f4c7ead
authored
Aug 10, 2014
by
Tim Olshansky
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #21 from bitsofinfo/master
support for user CRUD
parents
37391356
e19011d1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
248 additions
and
4 deletions
+248
-4
GitlabAPI.java
src/main/java/org/gitlab/api/GitlabAPI.java
+117
-0
GitlabUser.java
src/main/java/org/gitlab/api/models/GitlabUser.java
+57
-0
GitlabAPITest.java
src/test/java/org/gitlab/api/GitlabAPITest.java
+74
-4
No files found.
src/main/java/org/gitlab/api/GitlabAPI.java
View file @
3f4c7ead
...
...
@@ -37,6 +37,7 @@ public class GitlabAPI {
private
final
String
_apiToken
;
private
boolean
_ignoreCertificateErrors
=
false
;
private
static
final
String
API_NAMESPACE
=
"/api/v3"
;
private
static
final
String
PARAM_SUDO
=
"sudo"
;
public
static
final
ObjectMapper
MAPPER
=
new
ObjectMapper
().
configure
(
DeserializationConfig
.
Feature
.
FAIL_ON_UNKNOWN_PROPERTIES
,
false
);
private
GitlabAPI
(
String
hostUrl
,
String
apiToken
)
{
...
...
@@ -101,6 +102,122 @@ public class GitlabAPI {
return
retrieve
().
to
(
tailUrl
,
GitlabUser
.
class
);
}
public
GitlabUser
getUserViaSudo
(
String
username
)
throws
IOException
{
String
tailUrl
=
GitlabUser
.
USER_URL
+
"?"
+
PARAM_SUDO
+
"="
+
username
;
return
retrieve
().
to
(
tailUrl
,
GitlabUser
.
class
);
}
/**
* Create a new User
*
* @see http://doc.gitlab.com/ce/api/users.html
*
* @param email
* @param password
* @param username
* @param fullName
* @param skype
* @param linkedIn
* @param twitter
* @param website_url
* @param projects_limit
* @param extern_uid
* @param extern_provider_name
* @param bio
* @param isAdmin
* @param can_create_group
* @return
* @throws IOException
*/
public
GitlabUser
createUser
(
String
email
,
String
password
,
String
username
,
String
fullName
,
String
skypeId
,
String
linkedIn
,
String
twitter
,
String
website_url
,
Integer
projects_limit
,
String
extern_uid
,
String
extern_provider_name
,
String
bio
,
Boolean
isAdmin
,
Boolean
can_create_group
)
throws
IOException
{
Query
query
=
new
Query
()
.
append
(
"email"
,
email
)
.
appendIf
(
"password"
,
password
)
.
appendIf
(
"username"
,
username
)
.
appendIf
(
"name"
,
fullName
)
.
appendIf
(
"skype"
,
skypeId
)
.
appendIf
(
"linkedin"
,
linkedIn
)
.
appendIf
(
"twitter"
,
twitter
)
.
appendIf
(
"website_url"
,
website_url
)
.
appendIf
(
"projects_limit"
,
projects_limit
)
.
appendIf
(
"extern_uid"
,
extern_uid
)
.
appendIf
(
"provider"
,
extern_provider_name
)
.
appendIf
(
"bio"
,
bio
)
.
appendIf
(
"admin"
,
isAdmin
)
.
appendIf
(
"can_create_group"
,
can_create_group
);
String
tailUrl
=
GitlabUser
.
USERS_URL
+
query
.
toString
();
return
dispatch
().
to
(
tailUrl
,
GitlabUser
.
class
);
}
/**
* Update a user
*
* @param targetUserId
* @param email
* @param password
* @param username
* @param fullName
* @param skypeId
* @param linkedIn
* @param twitter
* @param website_url
* @param projects_limit
* @param extern_uid
* @param extern_provider_name
* @param bio
* @param isAdmin
* @param can_create_group
* @return
* @throws IOException
*/
public
GitlabUser
updateUser
(
Integer
targetUserId
,
String
email
,
String
password
,
String
username
,
String
fullName
,
String
skypeId
,
String
linkedIn
,
String
twitter
,
String
website_url
,
Integer
projects_limit
,
String
extern_uid
,
String
extern_provider_name
,
String
bio
,
Boolean
isAdmin
,
Boolean
can_create_group
)
throws
IOException
{
Query
query
=
new
Query
()
.
append
(
"email"
,
email
)
.
appendIf
(
"password"
,
password
)
.
appendIf
(
"username"
,
username
)
.
appendIf
(
"name"
,
fullName
)
.
appendIf
(
"skype"
,
skypeId
)
.
appendIf
(
"linkedin"
,
linkedIn
)
.
appendIf
(
"twitter"
,
twitter
)
.
appendIf
(
"website_url"
,
website_url
)
.
appendIf
(
"projects_limit"
,
projects_limit
)
.
appendIf
(
"extern_uid"
,
extern_uid
)
.
appendIf
(
"provider"
,
extern_provider_name
)
.
appendIf
(
"bio"
,
bio
)
.
appendIf
(
"admin"
,
isAdmin
)
.
appendIf
(
"can_create_group"
,
can_create_group
);
String
tailUrl
=
GitlabUser
.
USERS_URL
+
"/"
+
targetUserId
+
query
.
toString
();
return
retrieve
().
method
(
"PUT"
).
to
(
tailUrl
,
GitlabUser
.
class
);
}
/**
* Delete a user
*
* @param targetUserId
* @throws IOException
*/
public
void
deleteUser
(
Integer
targetUserId
)
throws
IOException
{
String
tailUrl
=
GitlabUser
.
USERS_URL
+
"/"
+
targetUserId
;
retrieve
().
method
(
"DELETE"
).
to
(
tailUrl
,
Void
.
class
);
}
public
GitlabGroup
getGroup
(
Integer
groupId
)
throws
IOException
{
String
tailUrl
=
GitlabGroup
.
URL
+
"/"
+
groupId
;
return
retrieve
().
to
(
tailUrl
,
GitlabGroup
.
class
);
...
...
src/main/java/org/gitlab/api/models/GitlabUser.java
View file @
3f4c7ead
...
...
@@ -5,6 +5,8 @@ import org.codehaus.jackson.annotate.JsonProperty;
public
class
GitlabUser
{
public
static
String
URL
=
"/users"
;
public
static
String
USERS_URL
=
"/users"
;
public
static
String
USER_URL
=
"/user"
;
// for sudo based ops
private
Integer
_id
;
private
String
_username
;
...
...
@@ -17,6 +19,18 @@ public class GitlabUser {
private
String
_state
;
private
boolean
_blocked
;
@JsonProperty
(
"private_token"
)
private
String
_privateToken
;
@JsonProperty
(
"color_scheme_id"
)
private
Integer
_colorSchemeId
;
@JsonProperty
(
"provider"
)
private
String
_externProviderName
;
@JsonProperty
(
"website_url"
)
private
String
_websiteUrl
;
@JsonProperty
(
"created_at"
)
private
Date
_createdAt
;
...
...
@@ -44,6 +58,9 @@ public class GitlabUser {
@JsonProperty
(
"can_create_team"
)
private
boolean
_canCreateTeam
;
@JsonProperty
(
"avatar_url"
)
private
String
_avatarUrl
;
public
Integer
getId
()
{
return
_id
;
}
...
...
@@ -164,6 +181,22 @@ public class GitlabUser {
_state
=
state
;
}
public
String
getExternProviderName
()
{
return
_externProviderName
;
}
public
void
setExternProviderName
(
String
externProviderName
)
{
_externProviderName
=
externProviderName
;
}
public
String
getWebsiteUrl
()
{
return
_websiteUrl
;
}
public
void
setWebsiteUrl
(
String
websiteUrl
)
{
_websiteUrl
=
websiteUrl
;
}
public
boolean
isAdmin
()
{
return
_isAdmin
;
}
...
...
@@ -195,4 +228,28 @@ public class GitlabUser {
public
void
setCanCreateTeam
(
boolean
canCreateTeam
)
{
_canCreateTeam
=
canCreateTeam
;
}
public
String
getAvatarUrl
()
{
return
_avatarUrl
;
}
public
void
setAvatarUrl
(
String
avatarUrl
)
{
this
.
_avatarUrl
=
avatarUrl
;
}
public
Integer
getColorSchemeId
()
{
return
_colorSchemeId
;
}
public
void
setColorSchemeId
(
Integer
colorSchemeId
)
{
this
.
_colorSchemeId
=
colorSchemeId
;
}
public
String
getPrivateToken
()
{
return
_privateToken
;
}
public
void
setPrivateToken
(
String
privateToken
)
{
this
.
_privateToken
=
privateToken
;
}
}
src/test/java/org/gitlab/api/GitlabAPITest.java
View file @
3f4c7ead
package
org
.
gitlab
.
api
;
import
org.gitlab.api.models.GitlabUser
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.util.UUID
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
...
...
@@ -12,9 +16,16 @@ public class GitlabAPITest {
GitlabAPI
_api
;
private
static
final
String
TEST_URL
=
"http://localhost"
;
private
static
final
String
TEST_TOKEN
=
"y0E5b9761b7y4qk"
;
String
rand
=
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
).
substring
(
0
,
8
);
@Before
public
void
setup
()
{
_api
=
GitlabAPI
.
connect
(
"http://localhost"
,
"token"
);
_api
=
GitlabAPI
.
connect
(
TEST_URL
,
TEST_TOKEN
);
}
@Test
...
...
@@ -24,13 +35,72 @@ public class GitlabAPITest {
@Test
public
void
testGetAPIUrl
()
throws
IOException
{
URL
expected
=
new
URL
(
"http://localhost/api/v3/?private_token=token"
);
URL
expected
=
new
URL
(
TEST_URL
+
"/api/v3/?private_token="
+
TEST_TOKEN
);
assertEquals
(
expected
,
_api
.
getAPIUrl
(
""
));
}
@Test
public
void
testGetUrl
()
throws
IOException
{
URL
expected
=
new
URL
(
"http://localhost/"
);
assertEquals
(
expected
,
_api
.
getUrl
(
""
));
URL
expected
=
new
URL
(
TEST_URL
);
assertEquals
(
expected
+
"/"
,
_api
.
getUrl
(
""
).
toString
());
}
@Test
public
void
testCreateUpdateDeleteUser
()
throws
IOException
{
String
password
=
randVal
(
"$%password"
);
GitlabUser
gitUser
=
_api
.
createUser
(
randVal
(
"testEmail@gitlabapitest.com"
),
password
,
randVal
(
"userName"
),
randVal
(
"fullName"
),
randVal
(
"skypeId"
),
randVal
(
"linledin"
),
randVal
(
"twitter"
),
"http://"
+
randVal
(
"url.com"
),
10
,
randVal
(
"externuid"
),
randVal
(
"externprovidername"
),
randVal
(
"bio"
),
false
,
false
);
Assert
.
assertNotNull
(
gitUser
);
GitlabUser
refetched
=
_api
.
getUserViaSudo
(
gitUser
.
getUsername
());
Assert
.
assertNotNull
(
refetched
);
Assert
.
assertEquals
(
refetched
.
getUsername
(),
gitUser
.
getUsername
());
_api
.
updateUser
(
gitUser
.
getId
(),
gitUser
.
getEmail
(),
password
,
gitUser
.
getUsername
(),
gitUser
.
getName
(),
"newSkypeId"
,
gitUser
.
getLinkedin
(),
gitUser
.
getTwitter
(),
gitUser
.
getWebsiteUrl
(),
10
/* project limit does not come back on GET */
,
gitUser
.
getExternUid
(),
gitUser
.
getExternProviderName
(),
gitUser
.
getBio
(),
gitUser
.
isAdmin
(),
gitUser
.
isCanCreateGroup
());
GitlabUser
postUpdate
=
_api
.
getUserViaSudo
(
gitUser
.
getUsername
());
Assert
.
assertNotNull
(
postUpdate
);
Assert
.
assertEquals
(
postUpdate
.
getSkype
(),
"newSkypeId"
);
_api
.
deleteUser
(
postUpdate
.
getId
());
// expect a 404, but we have no access to it
try
{
GitlabUser
shouldNotExist
=
_api
.
getUser
(
postUpdate
.
getId
());
Assert
.
assertNull
(
shouldNotExist
);
// should never even get here
}
catch
(
FileNotFoundException
thisIsSoOddForAnRESTApiClient
)
{
Assert
.
assertTrue
(
true
);
// expected
}
}
private
String
randVal
(
String
postfix
)
{
return
rand
+
"-"
+
postfix
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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