Line data Source code
1 : // Copyright 2011 Google Inc. All Rights Reserved. 2 : 3 : package com.google.gerrit.httpd; 4 : 5 : import com.google.common.base.CharMatcher; 6 : import com.google.common.collect.ImmutableList; 7 : import com.google.common.flogger.FluentLogger; 8 : import com.google.gerrit.common.PageLinks; 9 : import com.google.gerrit.entities.Change; 10 : import com.google.gerrit.entities.Project; 11 : import com.google.gerrit.extensions.api.changes.Changes; 12 : import com.google.gerrit.extensions.common.ChangeInfo; 13 : import com.google.gerrit.extensions.restapi.RestApiException; 14 : import com.google.inject.Inject; 15 : import com.google.inject.Singleton; 16 : import java.io.IOException; 17 : import java.util.List; 18 : import javax.servlet.http.HttpServlet; 19 : import javax.servlet.http.HttpServletRequest; 20 : import javax.servlet.http.HttpServletResponse; 21 : 22 : @Singleton 23 : class DirectChangeByCommit extends HttpServlet { 24 : private static final long serialVersionUID = 1L; 25 : 26 99 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 27 : 28 : private final Changes changes; 29 : 30 : @Inject 31 99 : DirectChangeByCommit(Changes changes) { 32 99 : this.changes = changes; 33 99 : } 34 : 35 : @Override 36 : protected void doGet(HttpServletRequest req, HttpServletResponse rsp) throws IOException { 37 0 : String query = CharMatcher.is('/').trimTrailingFrom(req.getPathInfo()); 38 : List<ChangeInfo> results; 39 : try { 40 0 : results = changes.query(query).withLimit(2).get(); 41 0 : } catch (RestApiException e) { 42 0 : logger.atWarning().withCause(e).log("Cannot process query by URL: /r/%s", query); 43 0 : results = ImmutableList.of(); 44 0 : } 45 : String token; 46 0 : if (results.size() == 1) { 47 : // If exactly one change matches, link to that change. 48 : // TODO Link to a specific patch set, if one matched. 49 0 : ChangeInfo ci = results.iterator().next(); 50 0 : token = PageLinks.toChange(Project.nameKey(ci.project), Change.id(ci._number)); 51 0 : } else { 52 : // Otherwise, link to the query page. 53 0 : token = PageLinks.toChangeQuery(query); 54 : } 55 0 : UrlModule.toGerrit(token, req, rsp); 56 0 : } 57 : }