Skip to content

Commit a5ec26f

Browse files
committed
Added starProject() and unstarProject() (#194).
1 parent 8a456a4 commit a5ec26f

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2097,4 +2097,34 @@ public List<Project> getForks(Integer projectId, int page, int perPage) throws G
20972097
public Pager<Project> getForks(Integer projectId, int itemsPerPage) throws GitLabApiException {
20982098
return new Pager<Project>(this, Project.class, itemsPerPage, null, "projects", projectId, "forks");
20992099
}
2100+
2101+
/**
2102+
* Star a project.
2103+
*
2104+
* POST /projects/:id/star
2105+
*
2106+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
2107+
* @return a Project instance with the new project info
2108+
* @throws GitLabApiException if any exception occurs
2109+
*/
2110+
public Project starProject(Object projectIdOrPath) throws GitLabApiException {
2111+
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.CREATED);
2112+
Response response = post(expectedStatus, (Form) null, "projects", getProjectIdOrPath(projectIdOrPath), "star");
2113+
return (response.readEntity(Project.class));
2114+
}
2115+
2116+
/**
2117+
* Unstar a project.
2118+
*
2119+
* POST /projects/:id/unstar
2120+
*
2121+
* @param projectIdOrPath id, path of the project, or a Project instance holding the project ID or path
2122+
* @return a Project instance with the new project info
2123+
* @throws GitLabApiException if any exception occurs
2124+
*/
2125+
public Project unstarProject(Object projectIdOrPath) throws GitLabApiException {
2126+
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.CREATED);
2127+
Response response = post(expectedStatus, (Form) null, "projects", getProjectIdOrPath(projectIdOrPath), "unstar");
2128+
return (response.readEntity(Project.class));
2129+
}
21002130
}

src/test/java/org/gitlab4j/api/TestProjectApi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,4 +495,24 @@ public void testGetOptionalProject() throws GitLabApiException {
495495
assertFalse(optional.isPresent());
496496
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
497497
}
498+
499+
@Test
500+
public void testStarAndUnstarProject() throws GitLabApiException {
501+
502+
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
503+
assertNotNull(project);
504+
505+
try {
506+
gitLabApi.getProjectApi().unstarProject(project);
507+
} catch (Exception ignore) {
508+
}
509+
510+
Project starredProject = gitLabApi.getProjectApi().starProject(project);
511+
assertNotNull(starredProject);
512+
assertEquals(1, (int)starredProject.getStarCount());
513+
514+
Project unstarredProject = gitLabApi.getProjectApi().unstarProject(project);
515+
assertNotNull(unstarredProject);
516+
assertEquals(0, (int)unstarredProject.getStarCount());
517+
}
498518
}

0 commit comments

Comments
 (0)