Index changes by external issue tracking systems

Parse out external references from footer lines in the commit message
based on configuration in gerrit.config.  Connect the change with
the external tracking ids and tracking system in a new table in
the DB, making the tracking ids searchable by tr:<id>.

Bug: issue 124
Change-Id: I3ddada57240040f27329f5f26f1f8e99e94f1469
This commit is contained in:
Goran Lungberg
2010-06-15 17:20:37 -07:00
committed by Shawn O. Pearce
parent de08e6304b
commit 04132a143f
15 changed files with 544 additions and 4 deletions

View File

@@ -1551,6 +1551,50 @@ code, or standard color name.
+
By default a shade of yellow, `FFFFCC`.
[[trackingid]] Section trackingid
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tagged footer lines containing references to exteranal tracking systems,
parsed out of the commit message and saved in Gerrit's DB.
The tracking ids are serachable using tr:<tracking id>.
----
[trackingid "jira-bug"]
footer = Bugfix:
match = JRA\\d{2,8}
system = JIRA
[trackingid "jira-feature"]
footer = Feature
match = JRA(\\d{2,8})
system = JIRA
----
[[trackingid.name.footer]]trackingid.<name>.footer::
+
A prefix tag that identify the footer line to parse for tracking ids.
Several trakingid entries can have the same footer tag.
(the trailing ":" is optional)
[[trackingid.name.match]]trackingid.<name>.match::
+
A regular expression used to match the external tracking id part of the
footer line. The match can result in several entries in the DB.
If grouping is used in the regex the first group will be interpreted
as the tracking id. Tracking ids > 20 char will be ignored.
+
The configuration file parser eats one level of backslashes, so the
character class `\s` requires `\\s` in the configuration file. The
parser also terminates the line at the first `#`, so a match
expression containing # must be wrapped in double quotes.
[[trackingid.name.system]]trackingid.<name>.system::
+
The name of the external tracking system(max 10 char).
It is possible to have several trackingid entries for the same
tracking system.
[[transfer]] Section transfer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -43,7 +43,7 @@ menuDocumentationIndex = Index
menuDocumentationUpload = Uploading Changes
menuDocumentationAccess = Access Controls
searchHint = Change #, SHA-1, owner:email or reviewer:email
searchHint = Change #, SHA-1, tr:id, owner:email or reviewer:email
searchButton = Search
rpcStatusLoading = Loading ...

View File

@@ -33,6 +33,7 @@ import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.RevId;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.StarredChange;
import com.google.gerrit.reviewdb.TrackingId;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.account.AccountInfoCacheFactory;
import com.google.gerrit.server.project.ChangeControl;
@@ -269,6 +270,11 @@ public class ChangeListServiceImpl extends BaseServiceImplementation implements
if (parsedQuery.length > 1) {
want.addAll(changesReviewedBy(db, parsedQuery[1]));
}
} else if (query.contains("tr:")) {
String[] parsedQuery = query.split(":");
if (parsedQuery.length > 1) {
want.addAll(changesReferencingTr(db, parsedQuery[1]));
}
}
if (result.isEmpty() && want.isEmpty()) {
@@ -524,6 +530,21 @@ public class ChangeListServiceImpl extends BaseServiceImplementation implements
return resultChanges;
}
/**
* @return a set of all the changes referencing tracking id. This method find
* all changes with a reference to the given external tracking id.
* The returned changes are unique and sorted by time stamp, newer first.
*/
private Set<Change.Id> changesReferencingTr(final ReviewDb db,
final String trackingId) throws OrmException {
final Set<Change.Id> resultChanges = new HashSet<Change.Id>();
for (final TrackingId tr : db.trackingIds().byTrackingId(
new TrackingId.Id(trackingId))) {
resultChanges.add(tr.getChangeId());
}
return resultChanges;
}
private abstract class QueryNext implements Action<SingleListChangeInfo> {
protected final String pos;
protected final int limit;

View File

@@ -108,6 +108,9 @@ public interface ReviewDb extends Schema {
@Relation
RefRightAccess refRights();
@Relation
TrackingIdAccess trackingIds();
/** Create the next unique id for an {@link Account}. */
@Sequence(startWith = 1000000)
int nextAccountId() throws OrmException;

View File

@@ -0,0 +1,143 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.reviewdb;
import com.google.gwtorm.client.Column;
import com.google.gwtorm.client.CompoundKey;
import com.google.gwtorm.client.StringKey;
/** External tracking id associated with a {@link Change} */
public final class TrackingId {
public static final int TRACKING_ID_MAX_CHAR = 20;
public static final int TRACKING_SYSTEM_MAX_CHAR = 10;
/** External tracking id */
public static class Id extends StringKey<com.google.gwtorm.client.Key<?>> {
private static final long serialVersionUID = 1L;
@Column(id = 1, length = TrackingId.TRACKING_ID_MAX_CHAR)
protected String id;
protected Id() {
}
public Id(final String id) {
this.id = id;
}
@Override
public String get() {
return id;
}
@Override
protected void set(String newValue) {
id = newValue;
}
}
/** Name of external tracking system */
public static class System extends StringKey<com.google.gwtorm.client.Key<?>> {
private static final long serialVersionUID = 1L;
@Column(id = 1, length = TrackingId.TRACKING_SYSTEM_MAX_CHAR)
protected String system;
protected System() {
}
public System(final String s) {
this.system = s;
}
@Override
public String get() {
return system;
}
@Override
protected void set(String newValue) {
system = newValue;
}
}
public static class Key extends CompoundKey<Change.Id> {
private static final long serialVersionUID = 1L;
@Column(id = 1)
protected Change.Id changeId;
@Column(id = 2)
protected Id trackingId;
@Column(id = 3)
protected System trackingSystem;
protected Key() {
changeId = new Change.Id();
trackingId = new Id();
trackingSystem = new System();
}
protected Key(final Change.Id ch, final Id id, final System s) {
changeId = ch;
trackingId = id;
trackingSystem = s;
}
@Override
public Change.Id getParentKey() {
return changeId;
}
@Override
public com.google.gwtorm.client.Key<?>[] members() {
return new com.google.gwtorm.client.Key<?>[] {trackingId, trackingSystem};
}
}
@Column(id = 1, name = Column.NONE)
protected Key key;
protected TrackingId() {
}
public TrackingId(final Change.Id ch, final TrackingId.Id id,
final TrackingId.System s) {
key = new Key(ch, id, s);
}
public TrackingId(final Change.Id ch, final String id, final String s) {
key = new Key(ch, new TrackingId.Id(id), new TrackingId.System(s));
}
public Change.Id getChangeId() {
return key.changeId;
}
@Override
public int hashCode() {
return key.hashCode();
}
@Override
public boolean equals(final Object obj) {
if (obj instanceof TrackingId) {
final TrackingId tr = (TrackingId) obj;
return tr.key.equals(tr.key);
}
return false;
}
}

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.reviewdb;
import com.google.gwtorm.client.Access;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.PrimaryKey;
import com.google.gwtorm.client.Query;
import com.google.gwtorm.client.ResultSet;
public interface TrackingIdAccess extends Access<TrackingId, TrackingId.Key> {
@PrimaryKey("key")
TrackingId get(TrackingId.Key key) throws OrmException;
@Query("WHERE key.changeId = ?")
ResultSet<TrackingId> byChange(Change.Id change) throws OrmException;
@Query("WHERE key.trackingId = ?")
ResultSet<TrackingId> byTrackingId(TrackingId.Id trackingId)
throws OrmException;
}

View File

@@ -166,6 +166,13 @@ CREATE INDEX ref_rights_byCatGroup
ON ref_rights (category_id, group_id);
-- *********************************************************************
-- TrackingIdAccess
--
CREATE INDEX tracking_ids_byTrkId
ON tracking_ids (tracking_id);
-- *********************************************************************
-- StarredChangeAccess
-- @PrimaryKey covers: byAccount

View File

@@ -218,6 +218,13 @@ CREATE INDEX ref_rights_byCatGroup
ON ref_rights (category_id, group_id);
-- *********************************************************************
-- TrackingIdAccess
--
CREATE INDEX tracking_ids_byTrkId
ON tracking_ids (tracking_id);
-- *********************************************************************
-- StarredChangeAccess
-- @PrimaryKey covers: byAccount

View File

@@ -25,6 +25,7 @@ public class GerritServerConfigModule extends AbstractModule {
@Override
protected void configure() {
bind(SitePaths.class);
bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class).in(SINGLETON) ;
bind(Config.class).annotatedWith(GerritServerConfig.class).toProvider(
GerritServerConfigProvider.class).in(SINGLETON);
}

View File

@@ -0,0 +1,60 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import org.eclipse.jgit.revwalk.FooterKey;
import org.eclipse.jgit.revwalk.FooterLine;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/** Tracking entry in the configuration file */
public class TrackingFooter {
private final FooterKey key;
private final Pattern match;
private final String system;
public TrackingFooter(String f, final String m, final String s)
throws PatternSyntaxException {
f = f.trim();
if (f.endsWith(":")) {
f = f.substring(0, f.length() - 1);
}
this.key = new FooterKey(f);
this.match = Pattern.compile(m.trim());
this.system = s.trim();
}
/** {@link FooterKey} to match in the commit message */
public FooterKey footerKey() {
return key;
}
/** Regex for parsing out external tracking id from {@link FooterLine} */
public Pattern match() {
return match;
}
/** Name of the remote tracking system */
public String system() {
return system;
}
@Override
public String toString() {
return "footer = " + key.getName() + ", match = " + match.pattern()
+ ", system = " + system;
}
}

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import java.util.List;
public class TrackingFooters {
protected List<TrackingFooter> trackingFooters;
public TrackingFooters (final List<TrackingFooter> trFooters) {
trackingFooters = trFooters;
}
public List<TrackingFooter> getTrackingFooters() {
return trackingFooters;
}
}

View File

@@ -0,0 +1,88 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import com.google.gerrit.reviewdb.TrackingId;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.PatternSyntaxException;
/** Provides a list of all configured {@link TrackingFooter}s. */
@Singleton
public class TrackingFootersProvider implements Provider<TrackingFooters> {
private static String TRACKING_ID_TAG = "trackingid";
private static String FOOTER_TAG = "footer";
private static String SYSTEM_TAG = "system";
private static String REGEX_TAG = "match";
private final List<TrackingFooter> trackingFooters =
new ArrayList<TrackingFooter>();
private static final Logger log =
LoggerFactory.getLogger(TrackingFootersProvider.class);
@Inject
TrackingFootersProvider(@GerritServerConfig final Config cfg) {
for (String name : cfg.getSubsections(TRACKING_ID_TAG)) {
boolean configValid = true;
String footer = cfg.getString(TRACKING_ID_TAG, name, FOOTER_TAG);
if (footer == null || footer.isEmpty()) {
configValid = false;
log.error("Missing " + TRACKING_ID_TAG + "." + name + "." + FOOTER_TAG
+ " in gerrit.config");
}
String system = cfg.getString(TRACKING_ID_TAG, name, SYSTEM_TAG);
if (system == null || system.isEmpty()) {
configValid = false;
log.error("Missing " + TRACKING_ID_TAG + "." + name + "." + SYSTEM_TAG
+ " in gerrit.config");
} else if (system.length() > TrackingId.TRACKING_SYSTEM_MAX_CHAR) {
configValid = false;
log.error("String to long \"" + system + "\" in gerrit.config "
+ TRACKING_ID_TAG + "." + name + "." + SYSTEM_TAG + " (max "
+ TrackingId.TRACKING_SYSTEM_MAX_CHAR + " char)");
}
String match = cfg.getString(TRACKING_ID_TAG, name, REGEX_TAG);
if (match == null || match.isEmpty()) {
configValid = false;
log.error("Missing " + TRACKING_ID_TAG + "." + name + "." + REGEX_TAG
+ " in gerrit.config");
}
if (configValid) {
try {
trackingFooters.add(new TrackingFooter(footer, match, system));
} catch (PatternSyntaxException e) {
log.error("Invalid pattern \"" + match + "\" in gerrit.config "
+ TRACKING_ID_TAG + "." + name + "." + REGEX_TAG + ": "
+ e.getMessage());
}
}
}
}
public TrackingFooters get() {
return new TrackingFooters(trackingFooters);
}
}

View File

@@ -36,11 +36,14 @@ import com.google.gerrit.reviewdb.PatchSetInfo;
import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.RevId;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.TrackingId;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.TrackingFooter;
import com.google.gerrit.server.config.TrackingFooters;
import com.google.gerrit.server.mail.CreateChangeSender;
import com.google.gerrit.server.mail.EmailException;
import com.google.gerrit.server.mail.MergedSender;
@@ -138,6 +141,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
private final ChangeHookRunner hooks;
private final String canonicalWebUrl;
private final PersonIdent gerritIdent;
private final TrackingFooters trackingFooters;
private final ProjectControl projectControl;
private final Project project;
@@ -167,6 +171,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
final ChangeHookRunner hooks,
@CanonicalWebUrl @Nullable final String canonicalWebUrl,
@GerritPersonIdent final PersonIdent gerritIdent,
final TrackingFooters trackingFooters,
@Assisted final ProjectControl projectControl,
@Assisted final Repository repo) {
@@ -182,6 +187,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
this.hooks = hooks;
this.canonicalWebUrl = canonicalWebUrl;
this.gerritIdent = gerritIdent;
this.trackingFooters = trackingFooters;
this.projectControl = projectControl;
this.project = projectControl.getProject();
@@ -803,7 +809,8 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
Change.Key changeKey = new Change.Key("I" + c.name());
final Set<Account.Id> reviewers = new HashSet<Account.Id>(reviewerId);
final Set<Account.Id> cc = new HashSet<Account.Id>(ccId);
for (final FooterLine footerLine : c.getFooterLines()) {
final List<FooterLine> footerLines = c.getFooterLines();
for (final FooterLine footerLine : footerLines) {
try {
if (footerLine.matches(CHANGE_ID)) {
final String v = footerLine.getValue().trim();
@@ -887,6 +894,7 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
log.error("Cannot send email for new change " + change.getId(), e);
}
addTrackingIds(change, footerLines);
hooks.doPatchsetCreatedHook(change, ps);
}
@@ -928,7 +936,8 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
final Account.Id me = currentUser.getAccountId();
final Set<Account.Id> reviewers = new HashSet<Account.Id>(reviewerId);
final Set<Account.Id> cc = new HashSet<Account.Id>(ccId);
for (final FooterLine footerLine : c.getFooterLines()) {
final List<FooterLine> footerLines = c.getFooterLines();
for (final FooterLine footerLine : footerLines) {
try {
if (isReviewer(footerLine)) {
reviewers.add(toAccountId(footerLine.getValue().trim()));
@@ -1184,10 +1193,64 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
} catch (EmailException e) {
log.error("Cannot send email for new patch set " + ps.getId(), e);
}
addTrackingIds(change, footerLines);
sendMergedEmail(result);
return result != null ? result.info.getKey() : null;
}
private void addTrackingIds(final Change change,
final List<FooterLine> footerLines) throws OrmException {
if (trackingFooters.getTrackingFooters().isEmpty() || footerLines.isEmpty()) {
return;
}
final Set<TrackingId> want = new HashSet<TrackingId>();
final Set<TrackingId> have = new HashSet<TrackingId>( //
db.trackingIds().byChange(change.getId()).toList());
for (final TrackingFooter footer : trackingFooters.getTrackingFooters()) {
for (final FooterLine footerLine : footerLines) {
if (footerLine.matches(footer.footerKey())) {
// supporting multiple tracking-ids on a single line
final Matcher m = footer.match().matcher(footerLine.getValue());
while (m.find()) {
if (m.group().isEmpty()) {
continue;
}
String idstr;
if (m.groupCount() > 0) {
idstr = m.group(1);
} else {
idstr = m.group();
}
if (idstr.isEmpty()) {
continue;
}
if (idstr.length() > TrackingId.TRACKING_ID_MAX_CHAR) {
continue;
}
want.add(new TrackingId(change.getId(), idstr, footer.system()));
}
}
}
}
// Only insert the rows we don't have, and delete rows we don't match.
//
final Set<TrackingId> toInsert = new HashSet<TrackingId>(want);
final Set<TrackingId> toDelete = new HashSet<TrackingId>(have);
toInsert.removeAll(have);
toDelete.removeAll(want);
db.trackingIds().insert(toInsert);
db.trackingIds().delete(toDelete);
}
static boolean parentsEqual(RevCommit a, RevCommit b) {
if (a.getParentCount() != b.getParentCount()) {
return false;

View File

@@ -32,7 +32,7 @@ import java.util.List;
/** A version of the database schema. */
public abstract class SchemaVersion {
/** The current schema version. */
private static final Class<? extends SchemaVersion> C = Schema_34.class;
private static final Class<? extends SchemaVersion> C = Schema_35.class;
public static class Module extends AbstractModule {
@Override

View File

@@ -0,0 +1,41 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.schema;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.sql.SQLException;
import java.sql.Statement;
public class Schema_35 extends SchemaVersion {
@Inject
Schema_35(Provider<Schema_34> prior) {
super(prior);
}
@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws SQLException {
Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
try {
stmt.execute("CREATE INDEX tracking_ids_byTrkId"
+ " ON tracking_ids (tracking_id)");
} finally {
stmt.close();
}
}
}