-
Notifications
You must be signed in to change notification settings - Fork 474
Project Branches API #153
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
gmessner
merged 13 commits into
gitlab4j:master
from
lamdav:feature/use-new-project-branch-api
Mar 11, 2018
Merged
Project Branches API #153
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9c7d1db
added new protected branch api
lamdav a4b0408
removed unused import
lamdav 888c006
removed extra new lines
lamdav 9e53c65
clean up
lamdav d6f1fe4
reordering import changes
lamdav ccb7a31
create ProtectedBranchesApi with Test
lamdav 94cf1f5
add comment to sample test properties
lamdav 06e3919
updated README
lamdav 14f780f
updated README
lamdav 97ea177
moved test properties
lamdav c0a6f11
improved tear down of test
lamdav c7f4e0e
updated README code snippet location
lamdav 02fa79b
rename BranchAccessLevelDetails to singular form
lamdav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.gitlab4j.api; | ||
|
||
import org.gitlab4j.api.models.AccessLevel; | ||
import org.gitlab4j.api.models.ProtectedBranch; | ||
|
||
import javax.ws.rs.core.Form; | ||
import javax.ws.rs.core.GenericType; | ||
import javax.ws.rs.core.Response; | ||
import java.util.List; | ||
|
||
public class ProtectedBranchesApi extends AbstractApi { | ||
public ProtectedBranchesApi(GitLabApi gitLabApi) { | ||
super(gitLabApi); | ||
} | ||
|
||
/** | ||
* Gets a list of protected branches from a project. | ||
* | ||
* GET /projects/:id/protected_branches | ||
* | ||
* @param projectId the ID of the project to protect | ||
* @return the list of protected branches for the project | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public List<ProtectedBranch> getProtectedBranches(Integer projectId) throws GitLabApiException { | ||
Response response = get(Response.Status.OK, null, "projects", projectId, "protected_branches"); | ||
return (response.readEntity(new GenericType<List<ProtectedBranch>>() { | ||
})); | ||
} | ||
|
||
/** | ||
* Unprotects the given protected branch or wildcard protected branch. | ||
* | ||
* DELETE /projects/:id/protected_branches/:name | ||
* | ||
* @param projectId the ID of the project to un-protect | ||
* @param branchName the name of the branch to un-protect | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public void unprotectBranch(Integer projectId, String branchName) throws GitLabApiException { | ||
delete(Response.Status.NO_CONTENT, null, "projects", projectId, "protected_branches", urlEncode(branchName)); | ||
} | ||
|
||
/** | ||
* Protects a single repository branch or several project repository branches using a wildcard protected branch. | ||
* | ||
* POST /projects/:id/protected_branches | ||
* | ||
* @param projectId the ID of the project to protect | ||
* @param branchName the name of the branch to protect | ||
* @return the branch info for the protected branch | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public ProtectedBranch protectBranch(Integer projectId, String branchName) throws GitLabApiException { | ||
return protectBranch(projectId, branchName, AccessLevel.MASTER, AccessLevel.MASTER); | ||
} | ||
|
||
/** | ||
* Protects a single repository branch or several project repository branches using a wildcard protected branch. | ||
* | ||
* POST /projects/:id/protected_branches | ||
* | ||
* @param projectId the ID of the project to protect | ||
* @param branchName the name of the branch to protect | ||
* @param pushAccessLevel Access levels allowed to push (defaults: 40, master access level) | ||
* @param mergeAccessLevel Access levels allowed to merge (defaults: 40, master access level) | ||
* @return the branch info for the protected branch | ||
* @throws GitLabApiException if any exception occurs | ||
*/ | ||
public ProtectedBranch protectBranch(Integer projectId, String branchName, AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel) throws GitLabApiException { | ||
Form formData = new GitLabApiForm() | ||
.withParam("id", projectId, true) | ||
.withParam("name", branchName, true) | ||
.withParam("push_access_level", pushAccessLevel.toValue(), false) | ||
.withParam("merge_access_level", mergeAccessLevel.toValue(), false); | ||
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "protected_branches"); | ||
return (response.readEntity(ProtectedBranch.class)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/org/gitlab4j/api/models/BranchAccessLevelDetails.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.gitlab4j.api.models; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public class BranchAccessLevelDetails { | ||
private String accessLevel; | ||
private String accessLevelDescription; | ||
|
||
public String getAccessLevel() { | ||
return this.accessLevel; | ||
} | ||
|
||
public void setAccessLevel(String accessLevel) { | ||
this.accessLevel = accessLevel; | ||
} | ||
|
||
public String getAccessLevelDescription() { | ||
return this.accessLevelDescription; | ||
} | ||
|
||
public void setAccessLevelDescription(String accessLevelDescription) { | ||
this.accessLevelDescription = accessLevelDescription; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/org/gitlab4j/api/models/ProtectedBranch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
package org.gitlab4j.api.models; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
import java.util.List; | ||
|
||
@XmlRootElement | ||
@XmlAccessorType(XmlAccessType.FIELD) | ||
public class ProtectedBranch { | ||
private String name; | ||
private List<BranchAccessLevelDetails> pushAccessLevels; | ||
private List<BranchAccessLevelDetails> mergeAccessLevels; | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public List<BranchAccessLevelDetails> getPushAccessLevels() { | ||
return this.pushAccessLevels; | ||
} | ||
|
||
public void setPushAccessLevels(List<BranchAccessLevelDetails> pushAccessLevels) { | ||
this.pushAccessLevels = pushAccessLevels; | ||
} | ||
|
||
public List<BranchAccessLevelDetails> getMergeAccessLevels() { | ||
return this.mergeAccessLevels; | ||
} | ||
|
||
public void setMergeAccessLevels(List<BranchAccessLevelDetails> mergeAccessLevels) { | ||
this.mergeAccessLevels = mergeAccessLevels; | ||
} | ||
|
||
public static final boolean isValid(ProtectedBranch branch) { | ||
return (branch != null && branch.getName() != null); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
src/test/java/org/gitlab4j/api/TestProtectedBranchesApi.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
package org.gitlab4j.api; | ||
|
||
import org.gitlab4j.api.models.Branch; | ||
import org.gitlab4j.api.models.Project; | ||
import org.gitlab4j.api.models.ProtectedBranch; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.FixMethodOrder; | ||
import org.junit.Test; | ||
import org.junit.runners.MethodSorters; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assume.assumeTrue; | ||
|
||
/** | ||
* In order for these tests to run you must set the following properties in test-gitlab4j.properties | ||
* | ||
* TEST_NAMESPACE | ||
* TEST_PROJECT_NAME | ||
* TEST_HOST_URL | ||
* TEST_PRIVATE_TOKEN | ||
* | ||
* If any of the above are NULL, all tests in this class will be skipped. | ||
* | ||
* NOTE: &FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that testCreate() is executed first. | ||
*/ | ||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) | ||
public class TestProtectedBranchesApi { | ||
// The following needs to be set to your test repository | ||
private static final String TEST_PROJECT_NAME; | ||
private static final String TEST_NAMESPACE; | ||
private static final String TEST_HOST_URL; | ||
private static final String TEST_PRIVATE_TOKEN; | ||
|
||
static { | ||
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE"); | ||
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME"); | ||
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL"); | ||
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN"); | ||
} | ||
|
||
private static GitLabApi gitLabApi; | ||
|
||
private static final String TEST_BRANCH_REF = "master"; | ||
private static final String TEST_BRANCH_NAME = "feature/test_branch"; | ||
private static final String TEST_PROTECT_BRANCH_NAME = "feature/protect_branch"; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
|
||
String problems = ""; | ||
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().length() == 0) { | ||
problems += "TEST_NAMESPACE cannot be empty\n"; | ||
} | ||
|
||
if (TEST_PROJECT_NAME == null || TEST_PROJECT_NAME.trim().length() == 0) { | ||
problems += "TEST_PROJECT_NAME cannot be empty\n"; | ||
} | ||
|
||
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().length() == 0) { | ||
problems += "TEST_HOST_URL cannot be empty\n"; | ||
} | ||
|
||
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().length() == 0) { | ||
problems += "TEST_PRIVATE_TOKEN cannot be empty\n"; | ||
} | ||
|
||
if (problems.isEmpty()) { | ||
gitLabApi = new GitLabApi(GitLabApi.ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN); | ||
} else { | ||
System.err.print(problems); | ||
} | ||
} | ||
|
||
@AfterClass | ||
public static void teardown() throws GitLabApiException { | ||
if (gitLabApi != null) { | ||
|
||
try { | ||
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); | ||
|
||
try { | ||
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_BRANCH_NAME); | ||
} catch (GitLabApiException ignore) { | ||
} | ||
|
||
try { | ||
gitLabApi.getRepositoryApi().deleteBranch(project.getId(), TEST_PROTECT_BRANCH_NAME); | ||
} catch (GitLabApiException ignore) { | ||
} | ||
} catch (GitLabApiException ignore) { | ||
} | ||
} | ||
} | ||
|
||
@Before | ||
public void beforeMethod() throws GitLabApiException { | ||
assumeTrue(gitLabApi != null); | ||
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); | ||
Branch protectedBranch; | ||
try { | ||
protectedBranch = gitLabApi.getRepositoryApi().getBranch(project.getId(), TEST_PROTECT_BRANCH_NAME); | ||
} catch (GitLabApiException e) { | ||
protectedBranch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_PROTECT_BRANCH_NAME, TEST_BRANCH_REF); | ||
} | ||
assertNotNull(protectedBranch); | ||
gitLabApi.getRepositoryApi().protectBranch(project.getId(), TEST_PROTECT_BRANCH_NAME); | ||
|
||
Branch branch; | ||
try { | ||
branch = gitLabApi.getRepositoryApi().getBranch(project.getId(), TEST_BRANCH_NAME); | ||
} catch (GitLabApiException e) { | ||
branch = gitLabApi.getRepositoryApi().createBranch(project.getId(), TEST_BRANCH_NAME, TEST_BRANCH_REF); | ||
} | ||
assertNotNull(branch); | ||
} | ||
|
||
@Test | ||
public void testGetProtectedBranches() throws GitLabApiException { | ||
|
||
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); | ||
assertNotNull(project); | ||
|
||
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId()); | ||
assertNotNull(branches); | ||
assertTrue(branches.stream() | ||
.anyMatch((branch) -> branch.getName().equals(TEST_PROTECT_BRANCH_NAME))); | ||
} | ||
|
||
@Test | ||
public void testUnprotectBranch() throws GitLabApiException { | ||
|
||
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); | ||
assertNotNull(project); | ||
|
||
gitLabApi.getProtectedBranchesApi().unprotectBranch(project.getId(), TEST_PROTECT_BRANCH_NAME); | ||
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId()); | ||
assertNotNull(branches); | ||
assertTrue(branches.stream() | ||
.noneMatch((branch) -> branch.getName().equals(TEST_PROTECT_BRANCH_NAME))); | ||
} | ||
|
||
@Test | ||
public void testProtectBranch() throws GitLabApiException { | ||
|
||
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); | ||
assertNotNull(project); | ||
|
||
ProtectedBranch branch = gitLabApi.getProtectedBranchesApi().protectBranch(project.getId(), TEST_BRANCH_NAME); | ||
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId()); | ||
assertNotNull(branches); | ||
assertTrue(branches.stream() | ||
.anyMatch((protectedBranch) -> protectedBranch.getName().equals(TEST_BRANCH_NAME))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to keep this list and the examples in alphabetical order.