diff --git a/src/main/java/org/gitlab4j/api/RepositorySubmodulesApi.java b/src/main/java/org/gitlab4j/api/RepositorySubmodulesApi.java new file mode 100644 index 000000000..50ab16d5f --- /dev/null +++ b/src/main/java/org/gitlab4j/api/RepositorySubmodulesApi.java @@ -0,0 +1,42 @@ +package org.gitlab4j.api; + +import javax.ws.rs.core.Response; + +import org.gitlab4j.api.models.Commit; + +/** + *

This class provides an entry point to all the GitLab API repository submodules calls. + * For more information on the repository APIs see:

+ * + * @see Repository Submodules API + */ +public class RepositorySubmodulesApi extends AbstractApi { + + public RepositorySubmodulesApi(GitLabApi gitLabApi) { + super(gitLabApi); + } + + /** + * Update existing submodule reference in repository. + * + *
GitLab Endpoint: PUT /projects/:id/repository/submodules/:submodule
+ * + * @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance + * @param submodule full path to the submodule + * @param branch name of the branch to commit into + * @param commitSha full commit SHA to update the submodule to + * @param commitMessage commit message (optional). If no message is provided, a default is set + * @return the created commit + * @throws GitLabApiException if any exception occurs + */ + public Commit updateExistingSubmoduleReference(Object projectIdOrPath, String submodule, String branch, String commitSha, String commitMessage) throws GitLabApiException { + GitLabApiForm formData = new GitLabApiForm() + .withParam("branch", branch, true) + .withParam("commit_sha", commitSha, true) + .withParam("commit_message", commitMessage); + Response response = put(Response.Status.OK, formData.asMap(), "projects", + getProjectIdOrPath(projectIdOrPath), "repository", "submodules", urlEncode(submodule)); + return (response.readEntity(Commit.class)); + } + +} diff --git a/src/test/java/org/gitlab4j/api/TestRepositorySubmodulesApi.java b/src/test/java/org/gitlab4j/api/TestRepositorySubmodulesApi.java new file mode 100644 index 000000000..ef260a486 --- /dev/null +++ b/src/test/java/org/gitlab4j/api/TestRepositorySubmodulesApi.java @@ -0,0 +1,48 @@ +package org.gitlab4j.api; + +import static org.gitlab4j.api.JsonUtils.compareJson; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.openMocks; + +import java.io.IOException; + +import javax.ws.rs.core.MultivaluedMap; + +import org.gitlab4j.api.models.Commit; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.Mockito; + +public class TestRepositorySubmodulesApi implements Constants { + + @Mock private GitLabApi gitLabApi; + @Mock private GitLabApiClient gitLabApiClient; + @Captor private ArgumentCaptor> attributeCaptor; + private MockResponse response; + + @BeforeEach + public void setUp() throws Exception { + openMocks(this); + } + + @Test + public void testUpdateExistingSubmoduleReference() throws Exception { + init(); + Commit result = new RepositorySubmodulesApi(gitLabApi).updateExistingSubmoduleReference(6L, "my-sub", "patch-1", "33e2ee8579fda5bc36accc9c6fbd0b4fefda9e30", "message"); + assertNotNull(result); + assertTrue(compareJson(result, "commit.json")); + } + + private void init() throws Exception, IOException { + response = new MockResponse(Commit.class, "commit.json", null); + when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient); + when(gitLabApiClient.validateSecretToken(any())).thenReturn(true); + when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.any())).thenReturn(response); + } +}