Skip to content

Add repository submodules api #956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}