Merge branch 'stable-2.13' into stable-2.14

* stable-2.13:
  ReceiveCommits: Fix NEW_PATCHSET regex pattern
  CreateChangeIT#createNewChangeSignedOffByFooter: Remove unnecessary assume
  CommitValidators: Always check for Change-Id in subject line
  AbstractPushForReview: Factor repeated code to setRequireChangeId method

Change-Id: I01c93d3d104558ec74f757843620e8f55d2233a2
This commit is contained in:
David Pursehouse 2018-02-05 16:57:11 +09:00
commit fe39bf86f4
5 changed files with 40 additions and 22 deletions

View File

@ -823,6 +823,15 @@ public abstract class AbstractDaemonTest {
} }
} }
protected void setRequireChangeId(InheritableBoolean value) throws Exception {
try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
ProjectConfig config = ProjectConfig.read(md);
config.getProject().setRequireChangeID(value);
config.commit(md);
projectCache.evict(config.getProject());
}
}
protected void deny(String permission, AccountGroup.UUID id, String ref) throws Exception { protected void deny(String permission, AccountGroup.UUID id, String ref) throws Exception {
deny(project, permission, id, ref); deny(project, permission, id, ref);
} }

View File

@ -988,9 +988,7 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
assertThat(GitUtil.getChangeId(testRepo, c).isPresent()).isFalse(); assertThat(GitUtil.getChangeId(testRepo, c).isPresent()).isFalse();
pushForReviewRejected(testRepo, "missing Change-Id in commit message footer"); pushForReviewRejected(testRepo, "missing Change-Id in commit message footer");
ProjectConfig config = projectCache.checkedGet(project).getConfig(); setRequireChangeId(InheritableBoolean.FALSE);
config.getProject().setRequireChangeID(InheritableBoolean.FALSE);
saveProjectConfig(project, config);
pushForReviewOk(testRepo); pushForReviewOk(testRepo);
} }
@ -1014,9 +1012,7 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
+ "Change-Id: Ie9a132e107def33bdd513b7854b50de911edba0a\n"); + "Change-Id: Ie9a132e107def33bdd513b7854b50de911edba0a\n");
pushForReviewRejected(testRepo, "multiple Change-Id lines in commit message footer"); pushForReviewRejected(testRepo, "multiple Change-Id lines in commit message footer");
ProjectConfig config = projectCache.checkedGet(project).getConfig(); setRequireChangeId(InheritableBoolean.FALSE);
config.getProject().setRequireChangeID(InheritableBoolean.FALSE);
saveProjectConfig(project, config);
pushForReviewRejected(testRepo, "multiple Change-Id lines in commit message footer"); pushForReviewRejected(testRepo, "multiple Change-Id lines in commit message footer");
} }
@ -1035,9 +1031,7 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
createCommit(testRepo, "Message with invalid Change-Id\n\nChange-Id: X\n"); createCommit(testRepo, "Message with invalid Change-Id\n\nChange-Id: X\n");
pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer"); pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer");
ProjectConfig config = projectCache.checkedGet(project).getConfig(); setRequireChangeId(InheritableBoolean.FALSE);
config.getProject().setRequireChangeID(InheritableBoolean.FALSE);
saveProjectConfig(project, config);
pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer"); pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer");
} }
@ -1061,12 +1055,19 @@ public abstract class AbstractPushForReview extends AbstractDaemonTest {
+ "Change-Id: I0000000000000000000000000000000000000000\n"); + "Change-Id: I0000000000000000000000000000000000000000\n");
pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer"); pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer");
ProjectConfig config = projectCache.checkedGet(project).getConfig(); setRequireChangeId(InheritableBoolean.FALSE);
config.getProject().setRequireChangeID(InheritableBoolean.FALSE);
saveProjectConfig(project, config);
pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer"); pushForReviewRejected(testRepo, "invalid Change-Id line format in commit message footer");
} }
@Test
public void pushWithChangeIdInSubjectLine() throws Exception {
createCommit(testRepo, "Change-Id: I1234000000000000000000000000000000000000");
pushForReviewRejected(testRepo, "missing subject; Change-Id must be in commit message footer");
setRequireChangeId(InheritableBoolean.FALSE);
pushForReviewRejected(testRepo, "missing subject; Change-Id must be in commit message footer");
}
@Test @Test
public void pushCommitWithSameChangeIdAsPredecessorChange() throws Exception { public void pushCommitWithSameChangeIdAsPredecessorChange() throws Exception {
PushOneCommit push = PushOneCommit push =

View File

@ -37,6 +37,7 @@ import com.google.gerrit.extensions.common.ChangeInput;
import com.google.gerrit.extensions.common.MergeInput; import com.google.gerrit.extensions.common.MergeInput;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException; import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.Branch; import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change; import com.google.gerrit.reviewdb.client.Change;
@ -94,6 +95,16 @@ public class CreateChangeIT extends AbstractDaemonTest {
assertCreateFails(ci, BadRequestException.class, "unsupported change status"); assertCreateFails(ci, BadRequestException.class, "unsupported change status");
} }
@Test
public void createEmptyChange_InvalidChangeId() throws Exception {
ChangeInput ci = newChangeInput(ChangeStatus.NEW);
ci.subject = "Subject\n\nChange-Id: I0000000000000000000000000000000000000000";
assertCreateFails(
ci,
ResourceConflictException.class,
"invalid Change-Id line format in commit message footer");
}
@Test @Test
public void createNewChange() throws Exception { public void createNewChange() throws Exception {
assertCreateSucceeds(newChangeInput(ChangeStatus.NEW)); assertCreateSucceeds(newChangeInput(ChangeStatus.NEW));
@ -124,7 +135,6 @@ public class CreateChangeIT extends AbstractDaemonTest {
@Test @Test
public void createNewChangeSignedOffByFooter() throws Exception { public void createNewChangeSignedOffByFooter() throws Exception {
assume().that(isAllowDrafts()).isTrue();
setSignedOffByFooter(); setSignedOffByFooter();
ChangeInfo info = assertCreateSucceeds(newChangeInput(ChangeStatus.NEW)); ChangeInfo info = assertCreateSucceeds(newChangeInput(ChangeStatus.NEW));
String message = info.revisions.get(info.currentRevision).commit.message; String message = info.revisions.get(info.currentRevision).commit.message;

View File

@ -181,7 +181,7 @@ public class ReceiveCommits {
private static final Logger log = LoggerFactory.getLogger(ReceiveCommits.class); private static final Logger log = LoggerFactory.getLogger(ReceiveCommits.class);
public static final Pattern NEW_PATCHSET = public static final Pattern NEW_PATCHSET =
Pattern.compile("^" + REFS_CHANGES + "(?:[0-9][0-9]/)?([1-9][0-9]*)(?:/new)?$"); Pattern.compile("^" + REFS_CHANGES + "(?:[0-9][0-9]/)?([1-9][0-9]*)(?:/[1-9][0-9]*)?$");
private static final String COMMAND_REJECTION_MESSAGE_FOOTER = private static final String COMMAND_REJECTION_MESSAGE_FOOTER =
"Please read the documentation and contact an administrator\n" "Please read the documentation and contact an administrator\n"

View File

@ -248,15 +248,13 @@ public class CommitValidators {
String sha1 = commit.abbreviate(RevId.ABBREV_LEN).name(); String sha1 = commit.abbreviate(RevId.ABBREV_LEN).name();
if (idList.isEmpty()) { if (idList.isEmpty()) {
String shortMsg = commit.getShortMessage();
if (shortMsg.startsWith(CHANGE_ID_PREFIX)
&& CHANGE_ID.matcher(shortMsg.substring(CHANGE_ID_PREFIX.length()).trim()).matches()) {
String errMsg = String.format(MISSING_SUBJECT_MSG, sha1);
throw new CommitValidationException(errMsg);
}
if (projectControl.getProjectState().isRequireChangeID()) { if (projectControl.getProjectState().isRequireChangeID()) {
String shortMsg = commit.getShortMessage();
if (shortMsg.startsWith(CHANGE_ID_PREFIX)
&& CHANGE_ID
.matcher(shortMsg.substring(CHANGE_ID_PREFIX.length()).trim())
.matches()) {
String errMsg = String.format(MISSING_SUBJECT_MSG, sha1);
throw new CommitValidationException(errMsg);
}
String errMsg = String.format(MISSING_CHANGE_ID_MSG, sha1); String errMsg = String.format(MISSING_CHANGE_ID_MSG, sha1);
messages.add(getMissingChangeIdErrorMsg(errMsg, commit)); messages.add(getMissingChangeIdErrorMsg(errMsg, commit));
throw new CommitValidationException(errMsg, messages); throw new CommitValidationException(errMsg, messages);