Skip to content

Commit 71e3cce

Browse files
committed
Added support Wikis API attachemnt upload (#290).
1 parent e33eb88 commit 71e3cce

File tree

4 files changed

+122
-5
lines changed

4 files changed

+122
-5
lines changed

src/main/java/org/gitlab4j/api/AbstractApi.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,28 @@ protected Response upload(Response.Status expectedStatus, String name, File file
380380
}
381381
}
382382

383+
/**
384+
* Perform a file upload with the specified File instance and path objects, returning
385+
* a ClientResponse instance with the data returned from the endpoint.
386+
*
387+
* @param expectedStatus the HTTP status that should be returned from the server
388+
* @param name the name for the form field that contains the file name
389+
* @param fileToUpload a File instance pointing to the file to upload
390+
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
391+
* @param formData the Form containing the name/value pairs
392+
* @param url the fully formed path to the GitLab API endpoint
393+
* @return a ClientResponse instance with the data returned from the endpoint
394+
* @throws GitLabApiException if any exception occurs during execution
395+
*/
396+
protected Response upload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, Form formData, URL url) throws GitLabApiException {
397+
398+
try {
399+
return validate(getApiClient().upload(name, fileToUpload, mediaType, formData, url), expectedStatus);
400+
} catch (Exception e) {
401+
throw handle(e);
402+
}
403+
}
404+
383405
/**
384406
* Perform an HTTP PUT call with the specified form data and path objects, returning
385407
* a ClientResponse instance with the data returned from the endpoint.

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.gitlab4j.api;
22

3-
import static javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA_TYPE;
4-
53
import java.io.File;
64
import java.io.IOException;
75
import java.net.Socket;
@@ -536,7 +534,24 @@ protected Response post(StreamingOutput stream, String mediaType, Object... path
536534
*/
537535
protected Response upload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException {
538536
URL url = getApiUrl(pathArgs);
539-
return (upload(name, fileToUpload, mediaTypeString, url));
537+
return (upload(name, fileToUpload, mediaTypeString, null, url));
538+
}
539+
540+
/**
541+
* Perform a file upload using the specified media type, returning
542+
* a ClientResponse instance with the data returned from the endpoint.
543+
*
544+
* @param name the name for the form field that contains the file name
545+
* @param fileToUpload a File instance pointing to the file to upload
546+
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
547+
* @param formData the Form containing the name/value pairs
548+
* @param pathArgs variable list of arguments used to build the URI
549+
* @return a ClientResponse instance with the data returned from the endpoint
550+
* @throws IOException if an error occurs while constructing the URL
551+
*/
552+
protected Response upload(String name, File fileToUpload, String mediaTypeString, Form formData, Object... pathArgs) throws IOException {
553+
URL url = getApiUrl(pathArgs);
554+
return (upload(name, fileToUpload, mediaTypeString, formData, url));
540555
}
541556

542557
/**
@@ -546,14 +561,25 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
546561
* @param name the name for the form field that contains the file name
547562
* @param fileToUpload a File instance pointing to the file to upload
548563
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
564+
* @param formData the Form containing the name/value pairs
549565
* @param url the fully formed path to the GitLab API endpoint
550566
* @return a ClientResponse instance with the data returned from the endpoint
551567
* @throws IOException if an error occurs while constructing the URL
552568
*/
553-
protected Response upload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException {
569+
protected Response upload(String name, File fileToUpload, String mediaTypeString, Form formData, URL url) throws IOException {
554570

555571
MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null);
556-
try (MultiPart multiPart = new FormDataMultiPart()) {
572+
try (FormDataMultiPart multiPart = new FormDataMultiPart()) {
573+
574+
if (formData != null) {
575+
MultivaluedMap<String, String> formParams = formData.asMap();
576+
formParams.forEach((key, values) -> {
577+
if (values != null) {
578+
values.forEach(value -> multiPart.field(key, value));
579+
}
580+
});
581+
}
582+
557583
FileDataBodyPart filePart = mediaType != null ?
558584
new FileDataBodyPart(name, fileToUpload, mediaType) :
559585
new FileDataBodyPart(name, fileToUpload);

src/main/java/org/gitlab4j/api/WikisApi.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@
2323

2424
package org.gitlab4j.api;
2525

26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.net.URL;
2629
import java.util.List;
2730
import java.util.Optional;
2831

2932
import javax.ws.rs.core.GenericType;
3033
import javax.ws.rs.core.Response;
3134

35+
import org.gitlab4j.api.models.WikiAttachment;
3236
import org.gitlab4j.api.models.WikiPage;
3337

3438
/**
@@ -163,4 +167,43 @@ public WikiPage updatePage(Object projectIdOrPath, String slug, String title, St
163167
public void deletePage(Object projectIdOrPath, String slug) throws GitLabApiException {
164168
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "wikis", slug);
165169
}
170+
171+
/**
172+
* Uploads a file to the attachment folder inside the wiki’s repository. The attachment folder is the uploads folder.
173+
*
174+
* <pre><code>POST /projects/:id/wikis/attachments</code></pre>
175+
*
176+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
177+
* @param fileToUpload the File instance of the file to upload, required
178+
* @return a FileUpload instance with information on the just uploaded file
179+
* @throws GitLabApiException if any exception occurs
180+
*/
181+
public WikiAttachment uploadAttachment(Object projectIdOrPath, File fileToUpload) throws GitLabApiException {
182+
return (uploadAttachment(projectIdOrPath, fileToUpload, null));
183+
}
184+
185+
/**
186+
* Uploads a file to the attachment folder inside the wiki’s repository. The attachment folder is the uploads folder.
187+
*
188+
* <pre><code>POST /projects/:id/wikis/attachments</code></pre>
189+
*
190+
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
191+
* @param fileToUpload the File instance of the file to upload, required
192+
* @param branch the name of the branch, defaults to the wiki repository default branch, optional
193+
* @return a FileUpload instance with information on the just uploaded file
194+
* @throws GitLabApiException if any exception occurs
195+
*/
196+
public WikiAttachment uploadAttachment(Object projectIdOrPath, File fileToUpload, String branch) throws GitLabApiException {
197+
198+
URL url;
199+
try {
200+
url = getApiClient().getApiUrl("projects", getProjectIdOrPath(projectIdOrPath), "wikis", "attachments");
201+
} catch (IOException ioe) {
202+
throw new GitLabApiException(ioe);
203+
}
204+
205+
GitLabApiForm formData = new GitLabApiForm().withParam("branch", branch);
206+
Response response = upload(Response.Status.CREATED, "file", fileToUpload, null, formData, url);
207+
return (response.readEntity(WikiAttachment.class));
208+
}
166209
}

src/test/java/org/gitlab4j/api/TestWikisApi.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
import org.gitlab4j.api.GitLabApi.ApiVersion;
2727
import org.gitlab4j.api.models.Project;
28+
import org.gitlab4j.api.models.WikiAttachment;
2829
import org.gitlab4j.api.models.WikiPage;
2930
import org.junit.AfterClass;
3031
import org.junit.Before;
3132
import org.junit.BeforeClass;
3233
import org.junit.Test;
3334

35+
import java.io.File;
3436
import java.util.List;
3537

3638
import static org.junit.Assert.*;
@@ -187,4 +189,28 @@ public void testDeleteWikiPage() throws GitLabApiException {
187189
}
188190
}
189191

192+
@Test
193+
public void testAttachment() throws GitLabApiException {
194+
String title = TEST_WIKI_TITLE_PREFIX + "Test createWikiPage()";
195+
WikiPage wikiPage = createWikiPage(title, testContent);
196+
assertNotNull(wikiPage);
197+
198+
File attachFile = new File("README.md");
199+
WikiAttachment attachment = gitLabApi.getWikisApi().uploadAttachment(testProjectId, attachFile);
200+
assertNotNull(attachment);
201+
assertEquals("README.md", attachment.getFileName());
202+
}
203+
204+
@Test
205+
public void testAttachmentWithBranch() throws GitLabApiException {
206+
String title = TEST_WIKI_TITLE_PREFIX + "Test createWikiPage()";
207+
WikiPage wikiPage = createWikiPage(title, testContent);
208+
assertNotNull(wikiPage);
209+
210+
File attachFile = new File("README.md");
211+
WikiAttachment attachment = gitLabApi.getWikisApi().uploadAttachment(testProjectId, attachFile, "master");
212+
assertNotNull(attachment);
213+
assertEquals("README.md", attachment.getFileName());
214+
assertEquals("master", attachment.getBranch());
215+
}
190216
}

0 commit comments

Comments
 (0)