Skip to content

Commit d798c90

Browse files
committed
Mods to support project and issues statistics (#424).
1 parent 6d1aa4c commit d798c90

File tree

7 files changed

+139
-4
lines changed

7 files changed

+139
-4
lines changed

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.gitlab4j.api.models.Issue;
1414
import org.gitlab4j.api.models.IssueFilter;
1515
import org.gitlab4j.api.models.IssueLink;
16+
import org.gitlab4j.api.models.IssuesStatistics;
17+
import org.gitlab4j.api.models.IssuesStatisticsFilter;
1618
import org.gitlab4j.api.models.MergeRequest;
1719
import org.gitlab4j.api.models.Participant;
1820
import org.gitlab4j.api.models.TimeStats;
@@ -22,6 +24,7 @@
2224
* This class provides an entry point to all the GitLab API Issue calls.
2325
* @see <a href="https://docs.gitlab.com/ce/api/issues.html">Issues API at GitLab</a>
2426
* @see <a href="https://docs.gitlab.com/ce/api/issue_links.html">Issue Links API at GitLab</a>
27+
* @see <a href="https://docs.gitlab.com/ce/api/issues_statistics.html">Issues Statistics API at GitLab</a>
2528
*/
2629
public class IssuesApi extends AbstractApi implements Constants {
2730

@@ -839,4 +842,52 @@ public Pager<Participant> getParticipants(Object projectIdOrPath, Integer issueI
839842
public Stream<Participant> getParticipantsStream(Object projectIdOrPath, Integer issueIid) throws GitLabApiException {
840843
return (getParticipants(projectIdOrPath, issueIid, getDefaultPerPage()).stream());
841844
}
845+
846+
/**
847+
* Gets issues count statistics on all issues the authenticated user has access to. By default it returns
848+
* only issues created by the current user. To get all issues, use parameter scope=all.
849+
*
850+
* <pre><code>GitLab Endpoint: GET /issues_statistics</code></pre>
851+
*
852+
* @param filter {@link IssuesStatisticsFilter} a IssuesStatisticsFilter instance with the filter settings.
853+
* @return an IssuesStatistics instance with the statistics for the matched issues.
854+
* @throws GitLabApiException if any exception occurs
855+
*/
856+
public IssuesStatistics getIssuesStatistics(IssuesStatisticsFilter filter) throws GitLabApiException {
857+
GitLabApiForm formData = filter.getQueryParams();
858+
Response response = get(Response.Status.OK, formData.asMap(), "issues_statistics");
859+
return (response.readEntity(IssuesStatistics.class));
860+
}
861+
862+
/**
863+
* Gets issues count statistics for given group.
864+
*
865+
* <pre><code>GitLab Endpoint: GET /groups/:groupId/issues_statistics</code></pre>
866+
*
867+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
868+
* @param filter {@link IssuesStatisticsFilter} a IssuesStatisticsFilter instance with the filter settings
869+
* @return an IssuesStatistics instance with the statistics for the matched issues
870+
* @throws GitLabApiException if any exception occurs
871+
*/
872+
public IssuesStatistics getGroupIssuesStatistics(Object groupIdOrPath, IssuesStatisticsFilter filter) throws GitLabApiException {
873+
GitLabApiForm formData = filter.getQueryParams();
874+
Response response = get(Response.Status.OK, formData.asMap(), "groups", this.getGroupIdOrPath(groupIdOrPath), "issues_statistics");
875+
return (response.readEntity(IssuesStatistics.class));
876+
}
877+
878+
/**
879+
* Gets issues count statistics for given group.
880+
*
881+
* <pre><code>GitLab Endpoint: GET /projects/:projectId/issues_statistics</code></pre>
882+
*
883+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
884+
* @param filter {@link IssuesStatisticsFilter} a IssuesStatisticsFilter instance with the filter settings.
885+
* @return an IssuesStatistics instance with the statistics for the matched issues
886+
* @throws GitLabApiException if any exception occurs
887+
*/
888+
public IssuesStatistics geProjectIssuesStatistics(Object projectIdOrPath, IssuesStatisticsFilter filter) throws GitLabApiException {
889+
GitLabApiForm formData = filter.getQueryParams();
890+
Response response = get(Response.Status.OK, formData.asMap(), "projects", this.getProjectIdOrPath(projectIdOrPath), "issues_statistics");
891+
return (response.readEntity(IssuesStatistics.class));
892+
}
842893
}

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.gitlab4j.api.models.Member;
4747
import org.gitlab4j.api.models.Namespace;
4848
import org.gitlab4j.api.models.Project;
49+
import org.gitlab4j.api.models.ProjectFetches;
4950
import org.gitlab4j.api.models.ProjectFilter;
5051
import org.gitlab4j.api.models.ProjectHook;
5152
import org.gitlab4j.api.models.ProjectUser;
@@ -57,15 +58,50 @@
5758
/**
5859
* This class provides an entry point to all the GitLab API project calls.
5960
* @see <a href="https://docs.gitlab.com/ce/api/projects.html">Projects API at GitLab</a>
60-
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
61-
* @see <a href="https://docs.gitlab.com/ee/api/access_requests.html#group-and-project-access-requests-api">Group and project access requests API</a>
61+
* @see <a href="https://docs.gitlab.com/ce/api/project_statistics.html">Project statistics API</a>
62+
* @see <a href="https://docs.gitlab.com/ce/api/members.html">Group and project members API at GitLab</a>
63+
* @see <a href="https://docs.gitlab.com/ce/api/access_requests.html#group-and-project-access-requests-api">Group and project access requests API</a>
6264
*/
6365
public class ProjectApi extends AbstractApi implements Constants {
6466

6567
public ProjectApi(GitLabApi gitLabApi) {
6668
super(gitLabApi);
6769
}
6870

71+
/**
72+
* Get the project fetch statistics for the last 30 days. Retrieving the statistics requires
73+
* write access to the repository. Currently only HTTP fetches statistics are returned.
74+
* Fetches statistics includes both clones and pulls count and are HTTP only,
75+
* SSH fetches are not included.
76+
*
77+
* <pre><code>GitLab Endpoint: GET /project/:id/statistics</code></pre>
78+
*
79+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
80+
* @return a ProjectFetches instance with the project fetch statistics for the last 30 days
81+
* @throws GitLabApiException if any exception occurs during execution
82+
*/
83+
public ProjectFetches getProjectStatistics(Object projectIdOrPath) throws GitLabApiException {
84+
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "statistics");
85+
return (response.readEntity(ProjectFetches.class));
86+
}
87+
88+
/**
89+
* Get an Optional instance with the value for the project fetch statistics for the last 30 days.
90+
*
91+
* <pre><code>GitLab Endpoint: GET /project/:id/statistics</code></pre>
92+
*
93+
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
94+
* @return an Optional instance with the value for the project fetch statistics for the last 30 day
95+
* @throws GitLabApiException if any exception occurs during execution
96+
*/
97+
public Optional<ProjectFetches> getOptionalProjectStatistics(Object projectIdOrPath) throws GitLabApiException {
98+
try {
99+
return (Optional.ofNullable(getProjectStatistics(projectIdOrPath)));
100+
} catch (GitLabApiException glae) {
101+
return (GitLabApi.createOptionalFromException(glae));
102+
}
103+
}
104+
69105
/**
70106
* <p>Get a list of projects accessible by the authenticated user.</p>
71107
*

src/main/java/org/gitlab4j/api/models/IssueFilter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
import org.gitlab4j.api.utils.ISO8601;
1313

1414
/**
15-
* Created by zhengrenjie on 2018/09/11 12:31
16-
*
1715
* This class is used to filter issues when getting lists of them.
1816
*/
1917
public class IssueFilter {

src/main/java/org/gitlab4j/api/utils/ISO8601.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class ISO8601 {
2626
public static final String OUTPUT_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'";
2727
public static final String OUTPUT_MSEC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
2828
public static final String UTC_PATTERN = "yyyy-MM-dd HH:mm:ss 'UTC'";
29+
public static final String DATE_ONLY_PATTERN = "yyyy-MM-dd";
2930

3031
private static final DateTimeFormatter ODT_WITH_MSEC_PARSER = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd[['T'][ ]HH:mm:ss.SSS[ ][XXXXX][XXXX]]").toFormatter();
3132
private static final DateTimeFormatter ODT_PARSER = new DateTimeFormatterBuilder().appendPattern("yyyy-MM-dd[['T'][ ]HH:mm:ss[.SSS][ ][XXX][X]]")
@@ -116,6 +117,21 @@ public static String toString(Date date, boolean withMsec) {
116117
SafeDateFormatter.getDateFormat(OUTPUT_PATTERN).format(date));
117118
}
118119

120+
/**
121+
* Get a string that includes the date only in yyyy-mm-ss format.
122+
*
123+
* @param date the Date instance to get the date only formatted string for
124+
* @return a string that includes the date only in yyyy-mm-ss format, or null if date is null
125+
*/
126+
public static String dateOnly(Date date) {
127+
128+
if (date == null) {
129+
return (null);
130+
}
131+
132+
return SafeDateFormatter.getDateFormat(DATE_ONLY_PATTERN).format(date);
133+
}
134+
119135
/**
120136
* Get a ISO8601 formatted string for the provided Date instance.
121137
*

src/main/java/org/gitlab4j/api/utils/JacksonJson.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ public <T> String marshal(final T object) {
257257
return (results);
258258
}
259259

260+
/**
261+
* JsonSerializer for serializing dates s yyyy-mm-dd in UTC timezone.
262+
*/
263+
public static class DateOnlySerializer extends JsonSerializer<Date> {
264+
265+
@Override
266+
public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
267+
String dateString = ISO8601.dateOnly(date);
268+
gen.writeString(dateString);
269+
}
270+
}
271+
260272
/**
261273
* JsonSerializer for serializing ISO8601 formatted dates.
262274
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@
5252
import org.gitlab4j.api.models.EpicIssue;
5353
import org.gitlab4j.api.models.Event;
5454
import org.gitlab4j.api.models.ExportStatus;
55+
import org.gitlab4j.api.models.ProjectFetches;
5556
import org.gitlab4j.api.models.FileUpload;
5657
import org.gitlab4j.api.models.Group;
5758
import org.gitlab4j.api.models.HealthCheckInfo;
5859
import org.gitlab4j.api.models.ImpersonationToken;
5960
import org.gitlab4j.api.models.ImportStatus;
6061
import org.gitlab4j.api.models.Issue;
6162
import org.gitlab4j.api.models.IssueLink;
63+
import org.gitlab4j.api.models.IssuesStatistics;
6264
import org.gitlab4j.api.models.Job;
6365
import org.gitlab4j.api.models.Key;
6466
import org.gitlab4j.api.models.Label;
@@ -208,6 +210,18 @@ public void testFileUpload() throws Exception {
208210
assertTrue(compareJson(fileUpload, "file-upload.json"));
209211
}
210212

213+
@Test
214+
public void testIssuesStatistics() throws Exception {
215+
IssuesStatistics statistics = unmarshalResource(IssuesStatistics.class, "issues-statistics.json");
216+
assertTrue(compareJson(statistics, "issues-statistics.json"));
217+
}
218+
219+
@Test
220+
public void testProjectFetches() throws Exception {
221+
ProjectFetches fetches = unmarshalResource(ProjectFetches.class, "project-fetches.json");
222+
assertTrue(compareJson(fetches, "project-fetches.json"));
223+
}
224+
211225
@Test
212226
public void testGroup() throws Exception {
213227
Group group = unmarshalResource(Group.class, "group.json");

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.gitlab4j.api.models.Group;
4545
import org.gitlab4j.api.models.Member;
4646
import org.gitlab4j.api.models.Project;
47+
import org.gitlab4j.api.models.ProjectFetches;
4748
import org.gitlab4j.api.models.ProjectFilter;
4849
import org.gitlab4j.api.models.User;
4950
import org.gitlab4j.api.models.Variable;
@@ -847,4 +848,11 @@ public void testDenyRequestAccess() throws GitLabApiException {
847848
}
848849
}
849850
}
851+
852+
@Test
853+
public void testGetProjectStatistics() throws GitLabApiException {
854+
assertNotNull(testProject);
855+
Optional<ProjectFetches> statistics = gitLabApi.getProjectApi().getOptionalProjectStatistics(testProject);
856+
assertTrue(statistics.isPresent());
857+
}
850858
}

0 commit comments

Comments
 (0)