IDEA-136499 Use iid as presentable ID of Gitlab issues

Real ID used to find issue on server is now in form <projectId>:<id>.
This commit is contained in:
Mikhail Golubev
2016-02-01 18:39:36 +03:00
parent ab464f07cd
commit 63cc98ee2d
4 changed files with 45 additions and 15 deletions
@@ -30,6 +30,7 @@ import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.intellij.tasks.impl.httpclient.TaskResponseUtil.GsonMultipleObjectsDeserializer;
@@ -42,8 +43,9 @@ import static com.intellij.tasks.impl.httpclient.TaskResponseUtil.GsonSingleObje
public class GitlabRepository extends NewBaseRepositoryImpl {
@NonNls public static final String REST_API_PATH_PREFIX = "/api/v3/";
private static final Pattern ID_PATTERN = Pattern.compile("\\d+");
@NonNls private static final String TOKEN_HEADER = "PRIVATE-TOKEN";
private static final Pattern ID_PATTERN = Pattern.compile("(\\d+):(\\d+)");
public static final Gson GSON = TaskGsonUtil.createDefaultBuilder().create();
public static final TypeToken<List<GitlabProject>> LIST_OF_PROJECTS_TYPE = new TypeToken<List<GitlabProject>>() {
};
@@ -113,11 +115,12 @@ public class GitlabRepository extends NewBaseRepositoryImpl {
@Nullable
@Override
public Task findTask(@NotNull String id) throws Exception {
// doesn't work now, because Gitlab's REST API doesn't provide endpoint to find task
// by its global ID, only by project ID and task's local ID (iid).
//GitlabIssue issue = fetchIssue(Integer.parseInt(id));
//return issue == null ? null : new GitlabTask(this, issue);
return null;
final Matcher matcher = ID_PATTERN.matcher(id);
if (!matcher.matches()) return null;
final int projectId = Integer.parseInt(matcher.group(1)), issueId = Integer.parseInt(matcher.group(2));
final GitlabIssue issue = fetchIssue(projectId, issueId);
if (issue == null) return null;
return new GitlabTask(this, issue);
}
@Nullable
@@ -182,11 +185,13 @@ public class GitlabRepository extends NewBaseRepositoryImpl {
return getRestApiUrl("issues");
}
@SuppressWarnings("UnusedDeclaration")
/**
* @param issueId global issue's ID (<tt>id</tt> field, not <tt>iid</tt>)
*/
@Nullable
public GitlabIssue fetchIssue(int id) throws Exception {
public GitlabIssue fetchIssue(int projectId, int issueId) throws Exception {
ensureProjectsDiscovered();
HttpGet request = new HttpGet(getRestApiUrl("issues", id));
final HttpGet request = new HttpGet(getRestApiUrl("projects", projectId, "issues", issueId));
ResponseHandler<GitlabIssue> handler = new GsonSingleObjectDeserializer<GitlabIssue>(GSON, GitlabIssue.class, true);
return getHttpClient().execute(request, handler);
}
@@ -223,7 +228,7 @@ public class GitlabRepository extends NewBaseRepositoryImpl {
return new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
request.addHeader("PRIVATE-TOKEN", myPassword);
request.addHeader(TOKEN_HEADER, myPassword);
//request.addHeader("Accept", "application/json");
}
};
@@ -37,7 +37,14 @@ public class GitlabTask extends Task {
@NotNull
@Override
public String getId() {
return String.valueOf(myIssue.getId());
// Will be in form <projectId>:<issueId>
return myIssue.getProjectId() + ":" + myIssue.getId();
}
@NotNull
@Override
public String getPresentableId() {
return "#" + myIssue.getLocalId();
}
@NotNull
@@ -404,6 +404,7 @@ public class LocalTaskImpl extends LocalTask {
@NotNull
@Override
public String getPresentableId() {
// Use global ID for compatibility
return StringUtil.isEmpty(myPresentableId) ? getId() : myPresentableId;
}
}
@@ -15,7 +15,6 @@ import com.intellij.util.containers.ContainerUtil;
import java.util.Collections;
/**
* TODO: install Gitlab on server and add more functional tests
* @author Mikhail Golubev
*/
public class GitlabIntegrationTest extends TaskManagerTestCase {
@@ -48,14 +47,13 @@ public class GitlabIntegrationTest extends TaskManagerTestCase {
LocalTaskImpl localTask = new LocalTaskImpl(new GitlabTask(myRepository, issue));
String changeListComment = TaskUtil.getChangeListComment(localTask);
assertEquals("project-1 2 1 Sample title", changeListComment);
assertEquals("project-1 2 #2 Sample title", changeListComment);
myRepository.setProjects(Collections.<GitlabProject>emptyList());
localTask = new LocalTaskImpl(new GitlabTask(myRepository, issue));
changeListComment = TaskUtil.getChangeListComment(localTask);
// Project is unknown, so "" is substituted instead
assertEquals(" 2 1 Sample title", changeListComment);
assertEquals(" 2 #2 Sample title", changeListComment);
}
public void testIssueFilteringByState() throws Exception {
@@ -74,6 +72,25 @@ public class GitlabIntegrationTest extends TaskManagerTestCase {
assertEquals("Opened issue #1", openedIssues[0].getSummary());
}
// IDEA-136499
public void testPresentableId() throws Exception {
final GitlabIssue issue = myRepository.fetchIssue(5 /* ID Formatting Tests */, 10);
assertNotNull(issue);
assertEquals(10, issue.getId());
assertEquals(1, issue.getLocalId());
assertEquals(5, issue.getProjectId());
final GitlabTask task = new GitlabTask(myRepository, issue);
assertEquals("#1", task.getPresentableId());
assertEquals("1", task.getNumber());
assertEquals("ID Formatting Tests", task.getProject());
assertEquals("5:10", task.getId());
assertEquals("#1: First issue with iid = 1", task.toString());
myRepository.setShouldFormatCommitMessage(true);
assertEquals("#1 First issue with iid = 1", myRepository.getTaskComment(task));
}
@Override
public void setUp() throws Exception {
super.setUp();