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
b4b55b0f
Commit
b4b55b0f
authored
Jun 10, 2019
by
Greg Messner
Browse files
Mods to support creating a project in a namespace (#380).
parent
0fd6f264
Changes
5
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
b4b55b0f
...
...
@@ -12,7 +12,7 @@ To utilize GitLab4J™ API in your Java project, simply add the following de
```
java
dependencies
{
...
compile
group:
'
org
.
gitlab4j
'
,
name:
'
gitlab4j
-
api
'
,
version:
'
4.11
.
7
'
compile
group:
'
org
.
gitlab4j
'
,
name:
'
gitlab4j
-
api
'
,
version:
'
4.11
.
8
'
}
```
...
...
@@ -23,7 +23,7 @@ dependencies {
<dependency>
<groupId>
org.gitlab4j
</groupId>
<artifactId>
gitlab4j-api
</artifactId>
<version>
4.11.
7
</version>
<version>
4.11.
8
</version>
</dependency>
```
...
...
src/main/java/org/gitlab4j/api/ProjectApi.java
View file @
b4b55b0f
...
...
@@ -44,6 +44,7 @@ import org.gitlab4j.api.models.Event;
import
org.gitlab4j.api.models.FileUpload
;
import
org.gitlab4j.api.models.Issue
;
import
org.gitlab4j.api.models.Member
;
import
org.gitlab4j.api.models.Namespace
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.ProjectFilter
;
import
org.gitlab4j.api.models.ProjectHook
;
...
...
@@ -767,19 +768,38 @@ public class ProjectApi extends AbstractApi implements Constants {
}
/**
* Create a new project
in the specified
group.
* Create a new project
belonging to the namespace ID. A namespace ID is either a user or
group
ID
.
*
* @param
groupId the group
ID to create the project under
* @param
namespaceId the namespace
ID to create the project under
* @param projectName the name of the project top create
* @return the created project
* @throws GitLabApiException if any exception occurs
*/
public
Project
createProject
(
Integer
group
Id
,
String
projectName
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"namespace_id"
,
group
Id
).
withParam
(
"name"
,
projectName
,
true
);
public
Project
createProject
(
Integer
namespace
Id
,
String
projectName
)
throws
GitLabApiException
{
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"namespace_id"
,
namespace
Id
).
withParam
(
"name"
,
projectName
,
true
);
Response
response
=
post
(
Response
.
Status
.
CREATED
,
formData
,
"projects"
);
return
(
response
.
readEntity
(
Project
.
class
));
}
/**
* Create a new project belonging to the namespace ID and project configuration. A namespace ID is either a user or group ID.
*
* @param namespaceId the namespace ID to create the project under
* @param project the Project instance holding the new project configuration
* @return the created project
* @throws GitLabApiException if any exception occurs
*/
public
Project
createProject
(
Integer
namespaceId
,
Project
project
)
throws
GitLabApiException
{
if
(
project
==
null
)
{
throw
new
RuntimeException
(
"Project instance cannot be null."
);
}
Namespace
namespace
=
new
Namespace
().
withId
(
namespaceId
);
project
.
setNamespace
(
namespace
);
return
(
createProject
(
project
));
}
/**
* Create a new project with the current user's namespace.
*
...
...
@@ -879,6 +899,11 @@ public class ProjectApi extends AbstractApi implements Constants {
.
withParam
(
"initialize_with_readme"
,
project
.
getInitializeWithReadme
())
.
withParam
(
"packages_enabled"
,
project
.
getPackagesEnabled
());
Namespace
namespace
=
project
.
getNamespace
();
if
(
namespace
!=
null
&&
namespace
.
getId
()
!=
null
)
{
formData
.
withParam
(
"namespace_id"
,
namespace
.
getId
());
}
if
(
isApiVersion
(
ApiVersion
.
V3
))
{
boolean
isPublic
=
(
project
.
getPublic
()
!=
null
?
project
.
getPublic
()
:
project
.
getVisibility
()
==
Visibility
.
PUBLIC
);
formData
.
withParam
(
"public"
,
isPublic
);
...
...
src/test/java/org/gitlab4j/api/AbstractIntegrationTest.java
View file @
b4b55b0f
...
...
@@ -6,6 +6,7 @@ import java.util.WeakHashMap;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.User
;
/**
* In order for the integration tests to run you must set the following properties in test-gitlab4j.properties
...
...
@@ -120,4 +121,28 @@ public class AbstractIntegrationTest implements PropertyConstants {
return
(
null
);
}
}
/**
* Get the current user (the testing user).
*
* @return the user that is being used for testing
*/
protected
static
User
getCurrentUser
()
{
Throwable
t
=
new
Throwable
();
StackTraceElement
directCaller
=
t
.
getStackTrace
()[
1
];
String
callingClassName
=
directCaller
.
getClassName
();
BaseTestResources
baseResources
=
baseTestResourcesMap
.
get
(
callingClassName
);
if
(
baseResources
==
null
||
baseResources
.
gitLabApi
==
null
)
{
System
.
err
.
println
(
"Problems fetching current user: GitLabApi instance is null"
);
return
(
null
);
}
try
{
return
(
baseResources
.
gitLabApi
.
getUserApi
().
getCurrentUser
());
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
"Problems fetching current user: "
+
e
.
getMessage
());
return
(
null
);
}
}
}
src/test/java/org/gitlab4j/api/IntegrationTestSuite.java
View file @
b4b55b0f
...
...
@@ -259,8 +259,8 @@ public class IntegrationTestSuite implements PropertyConstants {
.
withDefaultBranch
(
"master"
)
.
withPublic
(
true
)
.
withInitializeWithReadme
(
true
);
Project
groupProject
=
gitLabApi
.
getProjectApi
().
createProject
(
projectSettings
);
System
.
out
.
format
(
"Created %s project%n"
,
projectSettings
.
getNam
e
());
Project
groupProject
=
gitLabApi
.
getProjectApi
().
createProject
(
testGroup
.
getId
(),
projectSettings
);
System
.
out
.
format
(
"Created %s project%n"
,
groupProject
.
getNameWithNamespac
e
());
// Update the contents of README.md, so we have at minimum 2 commits
RepositoryFile
repoFile
=
new
RepositoryFile
();
...
...
@@ -269,9 +269,6 @@ public class IntegrationTestSuite implements PropertyConstants {
gitLabApi
.
getRepositoryFileApi
().
updateFile
(
groupProject
,
repoFile
,
"master"
,
"Updated contents"
);
System
.
out
.
format
(
"Updated content of %s repository file%n"
,
repoFile
.
getFilePath
());
gitLabApi
.
getGroupApi
().
transferProject
(
testGroup
,
groupProject
);
System
.
out
.
format
(
"Transfered %s project to %s group%n"
,
TEST_GROUP_PROJECT_NAME
,
TEST_GROUP
);
}
else
if
(!
gitLabApi
.
getRepositoryFileApi
().
getOptionalFile
(
testProject
,
"README.md"
,
"master"
).
isPresent
())
{
// Create the README.md file since it does not exists
...
...
src/test/java/org/gitlab4j/api/TestProjectApi.java
View file @
b4b55b0f
...
...
@@ -82,12 +82,14 @@ public class TestProjectApi extends AbstractIntegrationTest {
private
static
final
String
TEST_PROJECT_NAME_1
=
"test-gitlab4j-create-project"
;
private
static
final
String
TEST_PROJECT_NAME_2
=
"test-gitlab4j-create-project-2"
;
private
static
final
String
TEST_NAMESPACE_PROJECT_NAME
=
"test-gitlab4j-create-namespace-project"
;
private
static
final
String
TEST_PROJECT_NAME_UPDATE
=
"test-gitlab4j-create-project-update"
;
private
static
final
String
TEST_XFER_PROJECT_NAME
=
"test-gitlab4j-xfer-project"
;
private
static
final
String
TEST_VARIABLE_KEY_PREFIX
=
"TEST_VARIABLE_KEY_"
;
private
static
GitLabApi
gitLabApi
;
private
static
Project
testProject
;
private
static
User
currentUser
;
public
TestProjectApi
()
{
super
();
...
...
@@ -99,6 +101,7 @@ public class TestProjectApi extends AbstractIntegrationTest {
// Must setup the connection to the GitLab test server
gitLabApi
=
baseTestSetup
();
testProject
=
getTestProject
();
currentUser
=
getCurrentUser
();
deleteAllTestProjects
();
}
...
...
@@ -109,28 +112,29 @@ public class TestProjectApi extends AbstractIntegrationTest {
}
private
static
void
deleteAllTestProjects
()
{
if
(
gitLabApi
==
null
)
{
return
;
}
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_1
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_1
));
}
catch
(
GitLabApiException
ignore
)
{}
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_2
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_2
));
}
catch
(
GitLabApiException
ignore
)
{}
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_UPDATE
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME_UPDATE
));
}
catch
(
GitLabApiException
ignore
)
{}
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_XFER_PROJECT_NAME
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_NAMESPACE
,
TEST_XFER_PROJECT_NAME
));
}
catch
(
GitLabApiException
ignore
)
{}
try
{
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_NAMESPACE
,
TEST_NAMESPACE_PROJECT_NAME
));
}
catch
(
GitLabApiException
ignore
)
{}
if
(
TEST_GROUP
!=
null
&&
TEST_PROJECT_NAME
!=
null
)
{
...
...
@@ -153,15 +157,13 @@ public class TestProjectApi extends AbstractIntegrationTest {
if
(
TEST_GROUP
!=
null
&&
TEST_GROUP_PROJECT
!=
null
)
{
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_GROUP_PROJECT
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_NAMESPACE
,
TEST_GROUP_PROJECT
));
}
catch
(
GitLabApiException
ignore
)
{}
}
if
(
TEST_XFER_NAMESPACE
!=
null
)
{
try
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_XFER_NAMESPACE
,
TEST_XFER_PROJECT_NAME
);
gitLabApi
.
getProjectApi
().
deleteProject
(
project
);
gitLabApi
.
getProjectApi
().
deleteProject
(
Project
.
getPathWithNammespace
(
TEST_XFER_NAMESPACE
,
TEST_XFER_PROJECT_NAME
));
}
catch
(
GitLabApiException
ignore
)
{}
}
...
...
@@ -536,6 +538,24 @@ public class TestProjectApi extends AbstractIntegrationTest {
assertNotNull
(
projectLanguages
);
}
@Test
public
void
testCreateProjectInNamespace
()
throws
GitLabApiException
{
assumeNotNull
(
currentUser
);
Project
namespaceProject
=
null
;
try
{
namespaceProject
=
gitLabApi
.
getProjectApi
().
createProject
(
currentUser
.
getId
(),
TEST_NAMESPACE_PROJECT_NAME
);
assertNotNull
(
namespaceProject
);
}
finally
{
if
(
namespaceProject
!=
null
)
{
try
{
gitLabApi
.
getProjectApi
().
deleteProject
(
namespaceProject
);
}
catch
(
Exception
ignore
)
{}
}
}
}
@Test
public
void
testForkProject
()
throws
GitLabApiException
{
...
...
@@ -544,8 +564,18 @@ public class TestProjectApi extends AbstractIntegrationTest {
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_GROUP
,
TEST_GROUP_PROJECT
);
assertNotNull
(
project
);
Project
forkedProject
=
gitLabApi
.
getProjectApi
().
forkProject
(
project
.
getId
(),
TEST_NAMESPACE
);
assertNotNull
(
forkedProject
);
Project
forkedProject
=
null
;
try
{
forkedProject
=
gitLabApi
.
getProjectApi
().
forkProject
(
project
.
getId
(),
TEST_NAMESPACE
);
assertNotNull
(
forkedProject
);
}
finally
{
if
(
forkedProject
!=
null
)
{
try
{
gitLabApi
.
getProjectApi
().
deleteProject
(
forkedProject
);
}
catch
(
Exception
ignore
)
{}
}
}
}
@Test
...
...
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