Skip to content

Commit 85ad0d8

Browse files
committed
Fixed bug with GitLab where 1mo = 30d (#114).
1 parent 036fa56 commit 85ad0d8

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.gitlab4j.api.models.Duration;
3434
import org.gitlab4j.api.models.Issue;
3535
import org.gitlab4j.api.models.TimeStats;
36+
import org.gitlab4j.api.utils.DurationUtils;
3637

3738
/**
3839
* This class provides an entry point to all the GitLab API Issue calls.
@@ -347,7 +348,8 @@ public TimeStats estimateTime(Integer projectId, Integer issueIid, Duration dura
347348
throw new RuntimeException("issue IID cannot be null");
348349
}
349350

350-
GitLabApiForm formData = new GitLabApiForm().withParam("duration", duration, true);
351+
String durationString = (duration != null ? DurationUtils.toString(duration.getSeconds(), false) : null);
352+
GitLabApiForm formData = new GitLabApiForm().withParam("duration", durationString, true);
351353

352354
Response response = post(Response.Status.OK, formData.asMap(), "projects", projectId, "issues", issueIid, "time_estimate");
353355
return (response.readEntity(TimeStats.class));
@@ -428,7 +430,8 @@ public TimeStats addSpentTime(Integer projectId, Integer issueIid, Duration dura
428430
throw new RuntimeException("issue IID cannot be null");
429431
}
430432

431-
GitLabApiForm formData = new GitLabApiForm().withParam("duration", duration, true);
433+
String durationString = (duration != null ? DurationUtils.toString(duration.getSeconds(), false) : null);
434+
GitLabApiForm formData = new GitLabApiForm().withParam("duration", durationString, true);
432435

433436
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "issues", issueIid, "add_spent_time");
434437
return (response.readEntity(TimeStats.class));

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,28 @@ public class DurationUtils {
2323
* @return a human readable string representing the duration
2424
*/
2525
public static final String toString(int durationSeconds) {
26+
return DurationUtils.toString(durationSeconds, true);
27+
}
2628

27-
int months = durationSeconds / TIME_UNIT_MULTIPLIERS[0];
28-
int weeks = (durationSeconds - months * TIME_UNIT_MULTIPLIERS[0]) / TIME_UNIT_MULTIPLIERS[1];
29-
int days = (durationSeconds - months * TIME_UNIT_MULTIPLIERS[0] - weeks * TIME_UNIT_MULTIPLIERS[1]) / TIME_UNIT_MULTIPLIERS[2];
30-
int seconds = durationSeconds - (months * TIME_UNIT_MULTIPLIERS[0]) - (weeks * TIME_UNIT_MULTIPLIERS[1]) - (days * TIME_UNIT_MULTIPLIERS[2]);
29+
/**
30+
* Create a human readable duration string from seconds.
31+
*
32+
* @param durationSeconds the total number of seconds in the duration
33+
* @return a human readable string representing the duration
34+
*/
35+
public static final String toString(int durationSeconds, boolean includeMonths) {
36+
37+
int seconds = durationSeconds;
38+
int months = (includeMonths ? seconds / TIME_UNIT_MULTIPLIERS[0] : 0);
39+
seconds -= months * TIME_UNIT_MULTIPLIERS[0];
40+
int weeks = seconds / TIME_UNIT_MULTIPLIERS[1];
41+
seconds -= weeks * TIME_UNIT_MULTIPLIERS[1];
42+
int days = seconds / TIME_UNIT_MULTIPLIERS[2];
43+
seconds -= days * TIME_UNIT_MULTIPLIERS[2];
3144
int hours = seconds / 3600;
32-
int minutes = (seconds % 3600) / 60;
33-
seconds = seconds % 60;
45+
seconds -= hours * 3600;
46+
int minutes = seconds / 60;
47+
seconds -= minutes * 60;
3448

3549
StringBuilder buf = new StringBuilder();
3650
if (months > 0) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,11 @@ public void testToString() {
102102

103103
duration = DurationUtils.toString(60 * 60 * 8 * 5 * 4 * 3 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 6 + 8);
104104
assertEquals("3mo2d3h6m8s", duration);
105+
106+
duration = DurationUtils.toString(60 * 60 * 8 * 5 * 5 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 4 + 5, false);
107+
assertEquals("5w2d3h4m5s", duration);
108+
109+
duration = DurationUtils.toString(60 * 60 * 8 * 5 * 4 * 3 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 6 + 8, false);
110+
assertEquals("12w2d3h6m8s", duration);
105111
}
106112
}

0 commit comments

Comments
 (0)