Generalize Change.Id methods for parsing from refnames

Handle edit refs from Change.Id#fromRef, and add a new method
#fromAllUsersRef for parsing from refs in All-Users. Note that it's
not possible to combine these into one method that takes a project
name, since at this low level in the code we don't have access to the
live config-based AllUsersName.

Change-Id: I55ae5340efa964bbe7965a0a491ae27ce620044c
This commit is contained in:
Dave Borowitz 2016-11-30 11:28:58 -05:00
parent e5e2bfb9d2
commit 8e917e11c2
3 changed files with 103 additions and 3 deletions

View File

@ -143,6 +143,9 @@ public final class Change {
}
public static Id fromRef(String ref) {
if (RefNames.isRefsEdit(ref)) {
return fromEditRefPart(ref);
}
int cs = startIndex(ref);
if (cs < 0) {
return null;
@ -156,6 +159,42 @@ public final class Change {
return null;
}
public static Id fromAllUsersRef(String ref) {
if (ref == null) {
return null;
}
String prefix;
if (ref.startsWith(RefNames.REFS_STARRED_CHANGES)) {
prefix = RefNames.REFS_STARRED_CHANGES;
} else if (ref.startsWith(RefNames.REFS_DRAFT_COMMENTS)) {
prefix = RefNames.REFS_DRAFT_COMMENTS;
} else {
return null;
}
int cs = startIndex(ref, prefix);
if (cs < 0) {
return null;
}
int ce = nextNonDigit(ref, cs);
if (ce < ref.length() && ref.charAt(ce) == '/'
&& isNumeric(ref, ce + 1)) {
return new Change.Id(Integer.parseInt(ref.substring(cs, ce)));
}
return null;
}
private static boolean isNumeric(String s, int off) {
if (off >= s.length()) {
return false;
}
for (int i = off; i < s.length(); i++) {
if (!Character.isDigit(s.charAt(i))) {
return false;
}
}
return true;
}
public static Id fromEditRefPart(String ref) {
int startChangeId = ref.indexOf(RefNames.EDIT_PREFIX) +
RefNames.EDIT_PREFIX.length();
@ -173,12 +212,16 @@ public final class Change {
}
static int startIndex(String ref) {
if (ref == null || !ref.startsWith(REFS_CHANGES)) {
return startIndex(ref, REFS_CHANGES);
}
static int startIndex(String ref, String expectedPrefix) {
if (ref == null || !ref.startsWith(expectedPrefix)) {
return -1;
}
// Last 2 digits.
int ls = REFS_CHANGES.length();
int ls = expectedPrefix.length();
int le = nextNonDigit(ref, ls);
if (le - ls != 2 || le >= ref.length() || ref.charAt(le) != '/') {
return -1;

View File

@ -195,7 +195,8 @@ public class RefNames {
}
public static boolean isRefsEdit(String ref) {
return ref.startsWith(REFS_USERS) && ref.contains(EDIT_PREFIX);
return ref != null && ref.startsWith(REFS_USERS)
&& ref.contains(EDIT_PREFIX);
}
public static boolean isRefsUsers(String ref) {

View File

@ -63,6 +63,15 @@ public class ChangeTest {
assertNotRef("refs/changes/34/1234foo");
}
@Test
public void parseEditRefNames() {
assertRef(5, "refs/users/34/1234/edit-5/1");
assertRef(5, "refs/users/34/1234/edit-5");
assertNotRef("refs/changes/34/1234/edit-5/1");
assertNotRef("refs/users/34/1234/EDIT-5/1");
assertNotRef("refs/users/34/1234");
}
@Test
public void parseChangeMetaRefNames() {
assertRef(1, "refs/changes/01/1/meta");
@ -73,6 +82,44 @@ public class ChangeTest {
assertNotRef("refs/changes/01/1/1/meta");
}
@Test
public void parseRobotCommentRefNames() {
assertRef(1, "refs/changes/01/1/robot-comments");
assertRef(1234, "refs/changes/34/1234/robot-comments");
assertNotRef("refs/changes/01/1/robot-comment");
assertNotRef("refs/changes/01/1/ROBOT-COMMENTS");
assertNotRef("refs/changes/01/1/1/robot-comments");
}
@Test
public void parseStarredChangesRefNames() {
assertAllUsersRef(1, "refs/starred-changes/01/1/1001");
assertAllUsersRef(1234, "refs/starred-changes/34/1234/1001");
assertNotRef("refs/starred-changes/01/1/1001");
assertNotAllUsersRef(null);
assertNotAllUsersRef("refs/starred-changes/01/1/1xx1");
assertNotAllUsersRef("refs/starred-changes/01/1/");
assertNotAllUsersRef("refs/starred-changes/01/1");
assertNotAllUsersRef("refs/starred-changes/35/1234/1001");
assertNotAllUsersRef("refs/starred-changeS/01/1/1001");
}
@Test
public void parseDraftRefNames() {
assertAllUsersRef(1, "refs/draft-comments/01/1/1001");
assertAllUsersRef(1234, "refs/draft-comments/34/1234/1001");
assertNotRef("refs/draft-comments/01/1/1001");
assertNotAllUsersRef(null);
assertNotAllUsersRef("refs/draft-comments/01/1/1xx1");
assertNotAllUsersRef("refs/draft-comments/01/1/");
assertNotAllUsersRef("refs/draft-comments/01/1");
assertNotAllUsersRef("refs/draft-comments/35/1234/1001");
assertNotAllUsersRef("refs/draft-commentS/01/1/1001");
}
@Test
public void toRefPrefix() {
assertThat(new Change.Id(1).toRefPrefix())
@ -110,6 +157,15 @@ public class ChangeTest {
assertThat(Change.Id.fromRef(refName)).isNull();
}
private static void assertAllUsersRef(int changeId, String refName) {
assertThat(Change.Id.fromAllUsersRef(refName))
.isEqualTo(new Change.Id(changeId));
}
private static void assertNotAllUsersRef(String refName) {
assertThat(Change.Id.fromAllUsersRef(refName)).isNull();
}
private static void assertRefPart(int changeId, String refName) {
assertEquals(new Change.Id(changeId), Change.Id.fromRefPart(refName));
}