Commit 85ad0d84 authored by Greg Messner's avatar Greg Messner
Browse files

Fixed bug with GitLab where 1mo = 30d (#114).

parent 036fa56d
...@@ -33,6 +33,7 @@ import org.gitlab4j.api.GitLabApi.ApiVersion; ...@@ -33,6 +33,7 @@ import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Duration; import org.gitlab4j.api.models.Duration;
import org.gitlab4j.api.models.Issue; import org.gitlab4j.api.models.Issue;
import org.gitlab4j.api.models.TimeStats; import org.gitlab4j.api.models.TimeStats;
import org.gitlab4j.api.utils.DurationUtils;
/** /**
* This class provides an entry point to all the GitLab API Issue calls. * This class provides an entry point to all the GitLab API Issue calls.
...@@ -347,7 +348,8 @@ public class IssuesApi extends AbstractApi implements Constants { ...@@ -347,7 +348,8 @@ public class IssuesApi extends AbstractApi implements Constants {
throw new RuntimeException("issue IID cannot be null"); throw new RuntimeException("issue IID cannot be null");
} }
GitLabApiForm formData = new GitLabApiForm().withParam("duration", duration, true); String durationString = (duration != null ? DurationUtils.toString(duration.getSeconds(), false) : null);
GitLabApiForm formData = new GitLabApiForm().withParam("duration", durationString, true);
Response response = post(Response.Status.OK, formData.asMap(), "projects", projectId, "issues", issueIid, "time_estimate"); Response response = post(Response.Status.OK, formData.asMap(), "projects", projectId, "issues", issueIid, "time_estimate");
return (response.readEntity(TimeStats.class)); return (response.readEntity(TimeStats.class));
...@@ -428,7 +430,8 @@ public class IssuesApi extends AbstractApi implements Constants { ...@@ -428,7 +430,8 @@ public class IssuesApi extends AbstractApi implements Constants {
throw new RuntimeException("issue IID cannot be null"); throw new RuntimeException("issue IID cannot be null");
} }
GitLabApiForm formData = new GitLabApiForm().withParam("duration", duration, true); String durationString = (duration != null ? DurationUtils.toString(duration.getSeconds(), false) : null);
GitLabApiForm formData = new GitLabApiForm().withParam("duration", durationString, true);
Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "issues", issueIid, "add_spent_time"); Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "issues", issueIid, "add_spent_time");
return (response.readEntity(TimeStats.class)); return (response.readEntity(TimeStats.class));
......
...@@ -23,14 +23,28 @@ public class DurationUtils { ...@@ -23,14 +23,28 @@ public class DurationUtils {
* @return a human readable string representing the duration * @return a human readable string representing the duration
*/ */
public static final String toString(int durationSeconds) { public static final String toString(int durationSeconds) {
return DurationUtils.toString(durationSeconds, true);
}
int months = durationSeconds / TIME_UNIT_MULTIPLIERS[0]; /**
int weeks = (durationSeconds - months * TIME_UNIT_MULTIPLIERS[0]) / TIME_UNIT_MULTIPLIERS[1]; * Create a human readable duration string from seconds.
int days = (durationSeconds - months * TIME_UNIT_MULTIPLIERS[0] - weeks * TIME_UNIT_MULTIPLIERS[1]) / TIME_UNIT_MULTIPLIERS[2]; *
int seconds = durationSeconds - (months * TIME_UNIT_MULTIPLIERS[0]) - (weeks * TIME_UNIT_MULTIPLIERS[1]) - (days * TIME_UNIT_MULTIPLIERS[2]); * @param durationSeconds the total number of seconds in the duration
* @return a human readable string representing the duration
*/
public static final String toString(int durationSeconds, boolean includeMonths) {
int seconds = durationSeconds;
int months = (includeMonths ? seconds / TIME_UNIT_MULTIPLIERS[0] : 0);
seconds -= months * TIME_UNIT_MULTIPLIERS[0];
int weeks = seconds / TIME_UNIT_MULTIPLIERS[1];
seconds -= weeks * TIME_UNIT_MULTIPLIERS[1];
int days = seconds / TIME_UNIT_MULTIPLIERS[2];
seconds -= days * TIME_UNIT_MULTIPLIERS[2];
int hours = seconds / 3600; int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60; seconds -= hours * 3600;
seconds = seconds % 60; int minutes = seconds / 60;
seconds -= minutes * 60;
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
if (months > 0) { if (months > 0) {
......
...@@ -102,5 +102,11 @@ public class TestDuration { ...@@ -102,5 +102,11 @@ public class TestDuration {
duration = DurationUtils.toString(60 * 60 * 8 * 5 * 4 * 3 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 6 + 8); duration = DurationUtils.toString(60 * 60 * 8 * 5 * 4 * 3 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 6 + 8);
assertEquals("3mo2d3h6m8s", duration); assertEquals("3mo2d3h6m8s", duration);
duration = DurationUtils.toString(60 * 60 * 8 * 5 * 5 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 4 + 5, false);
assertEquals("5w2d3h4m5s", duration);
duration = DurationUtils.toString(60 * 60 * 8 * 5 * 4 * 3 + 60 * 60 * 8 * 2 + 60 * 60 * 3 + 60 * 6 + 8, false);
assertEquals("12w2d3h6m8s", duration);
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment