Skip to content

Commit

Permalink
Add repository submodules api (#956)
Browse files Browse the repository at this point in the history
* Add repository submodules api

Fixes #921

* Fix unit test
  • Loading branch information
jmini authored Apr 26, 2023
1 parent 384dcfa commit 6e248bc
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/main/java/org/gitlab4j/api/RepositorySubmodulesApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.gitlab4j.api;

import javax.ws.rs.core.Response;

import org.gitlab4j.api.models.Commit;

/**
* <p>This class provides an entry point to all the GitLab API repository submodules calls.
* For more information on the repository APIs see:</p>
*
* @see <a href="https://docs.gitlab.com/ee/api/repository_submodules.html">Repository Submodules API</a>
*/
public class RepositorySubmodulesApi extends AbstractApi {

public RepositorySubmodulesApi(GitLabApi gitLabApi) {
super(gitLabApi);
}

/**
* Update existing submodule reference in repository.
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/repository/submodules/:submodule</code></pre>
*
* @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));
}

}
48 changes: 48 additions & 0 deletions src/test/java/org/gitlab4j/api/TestRepositorySubmodulesApi.java
Original file line number Diff line number Diff line change
@@ -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<MultivaluedMap<String, String>> 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.<Object>any())).thenReturn(response);
}
}

0 comments on commit 6e248bc

Please sign in to comment.