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
71e3cce3
Commit
71e3cce3
authored
Jan 20, 2019
by
Greg Messner
Browse files
Added support Wikis API attachemnt upload (#290).
parent
e33eb883
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/AbstractApi.java
View file @
71e3cce3
...
...
@@ -380,6 +380,28 @@ public abstract class AbstractApi implements Constants {
}
}
/**
* Perform a file upload with the specified File instance and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param expectedStatus the HTTP status that should be returned from the server
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
* @param formData the Form containing the name/value pairs
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected
Response
upload
(
Response
.
Status
expectedStatus
,
String
name
,
File
fileToUpload
,
String
mediaType
,
Form
formData
,
URL
url
)
throws
GitLabApiException
{
try
{
return
validate
(
getApiClient
().
upload
(
name
,
fileToUpload
,
mediaType
,
formData
,
url
),
expectedStatus
);
}
catch
(
Exception
e
)
{
throw
handle
(
e
);
}
}
/**
* Perform an HTTP PUT call with the specified form data and path objects, returning
* a ClientResponse instance with the data returned from the endpoint.
...
...
src/main/java/org/gitlab4j/api/GitLabApiClient.java
View file @
71e3cce3
package
org.gitlab4j.api
;
import
static
javax
.
ws
.
rs
.
core
.
MediaType
.
MULTIPART_FORM_DATA_TYPE
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.Socket
;
...
...
@@ -536,7 +534,24 @@ public class GitLabApiClient {
*/
protected
Response
upload
(
String
name
,
File
fileToUpload
,
String
mediaTypeString
,
Object
...
pathArgs
)
throws
IOException
{
URL
url
=
getApiUrl
(
pathArgs
);
return
(
upload
(
name
,
fileToUpload
,
mediaTypeString
,
url
));
return
(
upload
(
name
,
fileToUpload
,
mediaTypeString
,
null
,
url
));
}
/**
* Perform a file upload using the specified media type, returning
* a ClientResponse instance with the data returned from the endpoint.
*
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
* @param formData the Form containing the name/value pairs
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected
Response
upload
(
String
name
,
File
fileToUpload
,
String
mediaTypeString
,
Form
formData
,
Object
...
pathArgs
)
throws
IOException
{
URL
url
=
getApiUrl
(
pathArgs
);
return
(
upload
(
name
,
fileToUpload
,
mediaTypeString
,
formData
,
url
));
}
/**
...
...
@@ -546,14 +561,25 @@ public class GitLabApiClient {
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
* @param formData the Form containing the name/value pairs
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected
Response
upload
(
String
name
,
File
fileToUpload
,
String
mediaTypeString
,
URL
url
)
throws
IOException
{
protected
Response
upload
(
String
name
,
File
fileToUpload
,
String
mediaTypeString
,
Form
formData
,
URL
url
)
throws
IOException
{
MediaType
mediaType
=
(
mediaTypeString
!=
null
?
MediaType
.
valueOf
(
mediaTypeString
)
:
null
);
try
(
MultiPart
multiPart
=
new
FormDataMultiPart
())
{
try
(
FormDataMultiPart
multiPart
=
new
FormDataMultiPart
())
{
if
(
formData
!=
null
)
{
MultivaluedMap
<
String
,
String
>
formParams
=
formData
.
asMap
();
formParams
.
forEach
((
key
,
values
)
->
{
if
(
values
!=
null
)
{
values
.
forEach
(
value
->
multiPart
.
field
(
key
,
value
));
}
});
}
FileDataBodyPart
filePart
=
mediaType
!=
null
?
new
FileDataBodyPart
(
name
,
fileToUpload
,
mediaType
)
:
new
FileDataBodyPart
(
name
,
fileToUpload
);
...
...
src/main/java/org/gitlab4j/api/WikisApi.java
View file @
71e3cce3
...
...
@@ -23,12 +23,16 @@
package
org.gitlab4j.api
;
import
java.io.File
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.util.List
;
import
java.util.Optional
;
import
javax.ws.rs.core.GenericType
;
import
javax.ws.rs.core.Response
;
import
org.gitlab4j.api.models.WikiAttachment
;
import
org.gitlab4j.api.models.WikiPage
;
/**
...
...
@@ -163,4 +167,43 @@ public class WikisApi extends AbstractApi {
public
void
deletePage
(
Object
projectIdOrPath
,
String
slug
)
throws
GitLabApiException
{
delete
(
Response
.
Status
.
NO_CONTENT
,
null
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"wikis"
,
slug
);
}
/**
* Uploads a file to the attachment folder inside the wiki’s repository. The attachment folder is the uploads folder.
*
* <pre><code>POST /projects/:id/wikis/attachments</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param fileToUpload the File instance of the file to upload, required
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public
WikiAttachment
uploadAttachment
(
Object
projectIdOrPath
,
File
fileToUpload
)
throws
GitLabApiException
{
return
(
uploadAttachment
(
projectIdOrPath
,
fileToUpload
,
null
));
}
/**
* Uploads a file to the attachment folder inside the wiki’s repository. The attachment folder is the uploads folder.
*
* <pre><code>POST /projects/:id/wikis/attachments</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param fileToUpload the File instance of the file to upload, required
* @param branch the name of the branch, defaults to the wiki repository default branch, optional
* @return a FileUpload instance with information on the just uploaded file
* @throws GitLabApiException if any exception occurs
*/
public
WikiAttachment
uploadAttachment
(
Object
projectIdOrPath
,
File
fileToUpload
,
String
branch
)
throws
GitLabApiException
{
URL
url
;
try
{
url
=
getApiClient
().
getApiUrl
(
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
),
"wikis"
,
"attachments"
);
}
catch
(
IOException
ioe
)
{
throw
new
GitLabApiException
(
ioe
);
}
GitLabApiForm
formData
=
new
GitLabApiForm
().
withParam
(
"branch"
,
branch
);
Response
response
=
upload
(
Response
.
Status
.
CREATED
,
"file"
,
fileToUpload
,
null
,
formData
,
url
);
return
(
response
.
readEntity
(
WikiAttachment
.
class
));
}
}
src/test/java/org/gitlab4j/api/TestWikisApi.java
View file @
71e3cce3
...
...
@@ -25,12 +25,14 @@ package org.gitlab4j.api;
import
org.gitlab4j.api.GitLabApi.ApiVersion
;
import
org.gitlab4j.api.models.Project
;
import
org.gitlab4j.api.models.WikiAttachment
;
import
org.gitlab4j.api.models.WikiPage
;
import
org.junit.AfterClass
;
import
org.junit.Before
;
import
org.junit.BeforeClass
;
import
org.junit.Test
;
import
java.io.File
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.*;
...
...
@@ -187,4 +189,28 @@ public class TestWikisApi {
}
}
@Test
public
void
testAttachment
()
throws
GitLabApiException
{
String
title
=
TEST_WIKI_TITLE_PREFIX
+
"Test createWikiPage()"
;
WikiPage
wikiPage
=
createWikiPage
(
title
,
testContent
);
assertNotNull
(
wikiPage
);
File
attachFile
=
new
File
(
"README.md"
);
WikiAttachment
attachment
=
gitLabApi
.
getWikisApi
().
uploadAttachment
(
testProjectId
,
attachFile
);
assertNotNull
(
attachment
);
assertEquals
(
"README.md"
,
attachment
.
getFileName
());
}
@Test
public
void
testAttachmentWithBranch
()
throws
GitLabApiException
{
String
title
=
TEST_WIKI_TITLE_PREFIX
+
"Test createWikiPage()"
;
WikiPage
wikiPage
=
createWikiPage
(
title
,
testContent
);
assertNotNull
(
wikiPage
);
File
attachFile
=
new
File
(
"README.md"
);
WikiAttachment
attachment
=
gitLabApi
.
getWikisApi
().
uploadAttachment
(
testProjectId
,
attachFile
,
"master"
);
assertNotNull
(
attachment
);
assertEquals
(
"README.md"
,
attachment
.
getFileName
());
assertEquals
(
"master"
,
attachment
.
getBranch
());
}
}
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