Skip to content

Commit 5a887c9

Browse files
lamdavgmessner
authored andcommitted
Project Branches API (#153)
* added new protected branch API * updated README
1 parent 78c69df commit 5a887c9

File tree

10 files changed

+361
-19
lines changed

10 files changed

+361
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ The API has been broken up into sub APIs classes to make it easier to learn and
149149
&nbsp;&nbsp;[NotificationSettingsApi](#notificationsettingsapi)<br/>
150150
&nbsp;&nbsp;[PipelineApi](#pipelineapi)<br/>
151151
&nbsp;&nbsp;[ProjectApi](#projectapi)<br/>
152+
&nbsp;&nbsp;[ProtectedBranchesApi](#protectedbranchesapi) <br/>
152153
&nbsp;&nbsp;[RepositoryApi](#repositoryapi)<br/>
153154
&nbsp;&nbsp;[RepositoryFileApi](#repositoryfileapi)<br/>
154155
&nbsp;&nbsp;[ServicesApi](#servicesapi)<br/>
@@ -261,6 +262,11 @@ Project projectSpec = new Project()
261262
Project newProject = gitLabApi.getProjectApi().createProject(projectSpec);
262263
```
263264

265+
#### ProtectedBranchesApi
266+
```java
267+
List<ProtectedBranch> branches = gitLabApi.getProtectedBranchesApi().getProtectedBranches(project.getId());
268+
```
269+
264270
#### RepositoryApi
265271
```java
266272
// Get a list of repository branches from a project, sorted by name alphabetically

example-test-gitlab4j.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Rename this file to test-gitlab4j.properties with proper property values.
2+
TEST_NAMESPACE=some-namespace
3+
TEST_PROJECT_NAME=some-project-name
4+
TEST_HOST_URL=some-gitlab-url
5+
TEST_PRIVATE_TOKEN=some-private-token

src/main/java/org/gitlab4j/api/GitLabApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public String getApiNamespace() {
6262
private LabelsApi labelsApi;
6363
private NotesApi notesApi;
6464
private EventsApi eventsApi;
65+
private ProtectedBranchesApi protectedBranchesApi;
6566

6667
/**
6768
* Create a new GitLabApi instance that is logically a duplicate of this instance, with the exception off sudo state.
@@ -1020,6 +1021,25 @@ public UserApi getUserApi() {
10201021
return (userApi);
10211022
}
10221023

1024+
/**
1025+
* Gets the ProtectedBranchesApi instance owned by this GitLabApi instance. The ProtectedBranchesApi is used
1026+
* to perform all protection related actions on a branch of a project.
1027+
*
1028+
* @return the ProtectedBranchesApi instance owned by this GitLabApi instance
1029+
*/
1030+
public ProtectedBranchesApi getProtectedBranchesApi() {
1031+
1032+
if (this.protectedBranchesApi == null) {
1033+
synchronized (this) {
1034+
if (this.protectedBranchesApi == null) {
1035+
this.protectedBranchesApi = new ProtectedBranchesApi(this);
1036+
}
1037+
}
1038+
}
1039+
1040+
return (this.protectedBranchesApi);
1041+
}
1042+
10231043
/**
10241044
* Create and return an Optional instance associated with a GitLabApiException.
10251045
*
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.gitlab4j.api;
2+
3+
import org.gitlab4j.api.models.AccessLevel;
4+
import org.gitlab4j.api.models.ProtectedBranch;
5+
6+
import javax.ws.rs.core.Form;
7+
import javax.ws.rs.core.GenericType;
8+
import javax.ws.rs.core.Response;
9+
import java.util.List;
10+
11+
public class ProtectedBranchesApi extends AbstractApi {
12+
public ProtectedBranchesApi(GitLabApi gitLabApi) {
13+
super(gitLabApi);
14+
}
15+
16+
/**
17+
* Gets a list of protected branches from a project.
18+
*
19+
* GET /projects/:id/protected_branches
20+
*
21+
* @param projectId the ID of the project to protect
22+
* @return the list of protected branches for the project
23+
* @throws GitLabApiException if any exception occurs
24+
*/
25+
public List<ProtectedBranch> getProtectedBranches(Integer projectId) throws GitLabApiException {
26+
Response response = get(Response.Status.OK, null, "projects", projectId, "protected_branches");
27+
return (response.readEntity(new GenericType<List<ProtectedBranch>>() {
28+
}));
29+
}
30+
31+
/**
32+
* Unprotects the given protected branch or wildcard protected branch.
33+
*
34+
* DELETE /projects/:id/protected_branches/:name
35+
*
36+
* @param projectId the ID of the project to un-protect
37+
* @param branchName the name of the branch to un-protect
38+
* @throws GitLabApiException if any exception occurs
39+
*/
40+
public void unprotectBranch(Integer projectId, String branchName) throws GitLabApiException {
41+
delete(Response.Status.NO_CONTENT, null, "projects", projectId, "protected_branches", urlEncode(branchName));
42+
}
43+
44+
/**
45+
* Protects a single repository branch or several project repository branches using a wildcard protected branch.
46+
*
47+
* POST /projects/:id/protected_branches
48+
*
49+
* @param projectId the ID of the project to protect
50+
* @param branchName the name of the branch to protect
51+
* @return the branch info for the protected branch
52+
* @throws GitLabApiException if any exception occurs
53+
*/
54+
public ProtectedBranch protectBranch(Integer projectId, String branchName) throws GitLabApiException {
55+
return protectBranch(projectId, branchName, AccessLevel.MASTER, AccessLevel.MASTER);
56+
}
57+
58+
/**
59+
* Protects a single repository branch or several project repository branches using a wildcard protected branch.
60+
*
61+
* POST /projects/:id/protected_branches
62+
*
63+
* @param projectId the ID of the project to protect
64+
* @param branchName the name of the branch to protect
65+
* @param pushAccessLevel Access levels allowed to push (defaults: 40, master access level)
66+
* @param mergeAccessLevel Access levels allowed to merge (defaults: 40, master access level)
67+
* @return the branch info for the protected branch
68+
* @throws GitLabApiException if any exception occurs
69+
*/
70+
public ProtectedBranch protectBranch(Integer projectId, String branchName, AccessLevel pushAccessLevel, AccessLevel mergeAccessLevel) throws GitLabApiException {
71+
Form formData = new GitLabApiForm()
72+
.withParam("id", projectId, true)
73+
.withParam("name", branchName, true)
74+
.withParam("push_access_level", pushAccessLevel.toValue(), false)
75+
.withParam("merge_access_level", mergeAccessLevel.toValue(), false);
76+
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "protected_branches");
77+
return (response.readEntity(ProtectedBranch.class));
78+
}
79+
}

src/main/java/org/gitlab4j/api/RepositoryApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import javax.ws.rs.core.Response;
1616

1717
import org.gitlab4j.api.GitLabApi.ApiVersion;
18+
import org.gitlab4j.api.models.AccessLevel;
1819
import org.gitlab4j.api.models.Branch;
1920
import org.gitlab4j.api.models.CompareResults;
2021
import org.gitlab4j.api.models.Tag;
@@ -110,7 +111,6 @@ public Branch createBranch(Integer projectId, String branchName, String ref) thr
110111
return (response.readEntity(Branch.class));
111112
}
112113

113-
114114
/**
115115
* Delete a single project repository branch.
116116
*
@@ -463,7 +463,7 @@ public File getRepositoryArchive(Integer projectId, String sha, File directory)
463463
/**
464464
* Compare branches, tags or commits. This can be accessed without authentication
465465
* if the repository is publicly accessible.
466-
*
466+
*
467467
* @param projectId the ID of the project owned by the authenticated user
468468
* @param from the commit SHA or branch name
469469
* @param to the commit SHA or branch name
@@ -479,7 +479,7 @@ public CompareResults compare(Integer projectId, String from, String to) throws
479479
/**
480480
* Compare branches, tags or commits. This can be accessed without authentication
481481
* if the repository is publicly accessible.
482-
*
482+
*
483483
* @param projectPath the path of the project owned by the authenticated user
484484
* @param from the commit SHA or branch name
485485
* @param to the commit SHA or branch name
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.gitlab4j.api.models;
2+
3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlRootElement;
6+
7+
@XmlRootElement
8+
@XmlAccessorType(XmlAccessType.FIELD)
9+
public class BranchAccessLevelDetail {
10+
private String accessLevel;
11+
private String accessLevelDescription;
12+
13+
public String getAccessLevel() {
14+
return this.accessLevel;
15+
}
16+
17+
public void setAccessLevel(String accessLevel) {
18+
this.accessLevel = accessLevel;
19+
}
20+
21+
public String getAccessLevelDescription() {
22+
return this.accessLevelDescription;
23+
}
24+
25+
public void setAccessLevelDescription(String accessLevelDescription) {
26+
this.accessLevelDescription = accessLevelDescription;
27+
}
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
package org.gitlab4j.api.models;
3+
4+
import javax.xml.bind.annotation.XmlAccessType;
5+
import javax.xml.bind.annotation.XmlAccessorType;
6+
import javax.xml.bind.annotation.XmlRootElement;
7+
import java.util.List;
8+
9+
@XmlRootElement
10+
@XmlAccessorType(XmlAccessType.FIELD)
11+
public class ProtectedBranch {
12+
private String name;
13+
private List<BranchAccessLevelDetail> pushAccessLevels;
14+
private List<BranchAccessLevelDetail> mergeAccessLevels;
15+
16+
public String getName() {
17+
return this.name;
18+
}
19+
20+
public void setName(String name) {
21+
this.name = name;
22+
}
23+
24+
public List<BranchAccessLevelDetail> getPushAccessLevels() {
25+
return this.pushAccessLevels;
26+
}
27+
28+
public void setPushAccessLevels(List<BranchAccessLevelDetail> pushAccessLevels) {
29+
this.pushAccessLevels = pushAccessLevels;
30+
}
31+
32+
public List<BranchAccessLevelDetail> getMergeAccessLevels() {
33+
return this.mergeAccessLevels;
34+
}
35+
36+
public void setMergeAccessLevels(List<BranchAccessLevelDetail> mergeAccessLevels) {
37+
this.mergeAccessLevels = mergeAccessLevels;
38+
}
39+
40+
public static final boolean isValid(ProtectedBranch branch) {
41+
return (branch != null && branch.getName() != null);
42+
}
43+
}

0 commit comments

Comments
 (0)