Skip to content

Commit 6e248bc

Browse files
authored
Add repository submodules api (#956)
* Add repository submodules api Fixes #921 * Fix unit test
1 parent 384dcfa commit 6e248bc

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.gitlab4j.api;
2+
3+
import javax.ws.rs.core.Response;
4+
5+
import org.gitlab4j.api.models.Commit;
6+
7+
/**
8+
* <p>This class provides an entry point to all the GitLab API repository submodules calls.
9+
* For more information on the repository APIs see:</p>
10+
*
11+
* @see <a href="https://docs.gitlab.com/ee/api/repository_submodules.html">Repository Submodules API</a>
12+
*/
13+
public class RepositorySubmodulesApi extends AbstractApi {
14+
15+
public RepositorySubmodulesApi(GitLabApi gitLabApi) {
16+
super(gitLabApi);
17+
}
18+
19+
/**
20+
* Update existing submodule reference in repository.
21+
*
22+
* <pre><code>GitLab Endpoint: PUT /projects/:id/repository/submodules/:submodule</code></pre>
23+
*
24+
* @param projectIdOrPath the project in the form of an Long(ID), String(path), or Project instance
25+
* @param submodule full path to the submodule
26+
* @param branch name of the branch to commit into
27+
* @param commitSha full commit SHA to update the submodule to
28+
* @param commitMessage commit message (optional). If no message is provided, a default is set
29+
* @return the created commit
30+
* @throws GitLabApiException if any exception occurs
31+
*/
32+
public Commit updateExistingSubmoduleReference(Object projectIdOrPath, String submodule, String branch, String commitSha, String commitMessage) throws GitLabApiException {
33+
GitLabApiForm formData = new GitLabApiForm()
34+
.withParam("branch", branch, true)
35+
.withParam("commit_sha", commitSha, true)
36+
.withParam("commit_message", commitMessage);
37+
Response response = put(Response.Status.OK, formData.asMap(), "projects",
38+
getProjectIdOrPath(projectIdOrPath), "repository", "submodules", urlEncode(submodule));
39+
return (response.readEntity(Commit.class));
40+
}
41+
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.gitlab4j.api;
2+
3+
import static org.gitlab4j.api.JsonUtils.compareJson;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.when;
8+
import static org.mockito.MockitoAnnotations.openMocks;
9+
10+
import java.io.IOException;
11+
12+
import javax.ws.rs.core.MultivaluedMap;
13+
14+
import org.gitlab4j.api.models.Commit;
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
import org.mockito.ArgumentCaptor;
18+
import org.mockito.Captor;
19+
import org.mockito.Mock;
20+
import org.mockito.Mockito;
21+
22+
public class TestRepositorySubmodulesApi implements Constants {
23+
24+
@Mock private GitLabApi gitLabApi;
25+
@Mock private GitLabApiClient gitLabApiClient;
26+
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
27+
private MockResponse response;
28+
29+
@BeforeEach
30+
public void setUp() throws Exception {
31+
openMocks(this);
32+
}
33+
34+
@Test
35+
public void testUpdateExistingSubmoduleReference() throws Exception {
36+
init();
37+
Commit result = new RepositorySubmodulesApi(gitLabApi).updateExistingSubmoduleReference(6L, "my-sub", "patch-1", "33e2ee8579fda5bc36accc9c6fbd0b4fefda9e30", "message");
38+
assertNotNull(result);
39+
assertTrue(compareJson(result, "commit.json"));
40+
}
41+
42+
private void init() throws Exception, IOException {
43+
response = new MockResponse(Commit.class, "commit.json", null);
44+
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
45+
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
46+
when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>any())).thenReturn(response);
47+
}
48+
}

0 commit comments

Comments
 (0)