Provide the host name of the OS as fallback for the canonicalWebUrl
CommitValidators was already using the host name of the OS as fallback whenever no canonicalWebUrl was defined. So, why not apply this policy throughout our code? Change-Id: I7ba0f9a14f11b4458a192c3e3355c90f3721dd0a
This commit is contained in:
@@ -20,8 +20,10 @@ import com.google.inject.Inject;
|
|||||||
import com.google.inject.OutOfScopeException;
|
import com.google.inject.OutOfScopeException;
|
||||||
import com.google.inject.Provider;
|
import com.google.inject.Provider;
|
||||||
import com.google.inject.ProvisionException;
|
import com.google.inject.ProvisionException;
|
||||||
|
import java.util.Optional;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import org.eclipse.jgit.lib.Config;
|
import org.eclipse.jgit.lib.Config;
|
||||||
|
import org.eclipse.jgit.util.SystemReader;
|
||||||
|
|
||||||
/** Sets {@code CanonicalWebUrl} to current HTTP request if not configured. */
|
/** Sets {@code CanonicalWebUrl} to current HTTP request if not configured. */
|
||||||
public class HttpCanonicalWebUrlProvider extends CanonicalWebUrlProvider {
|
public class HttpCanonicalWebUrlProvider extends CanonicalWebUrlProvider {
|
||||||
@@ -44,7 +46,16 @@ public class HttpCanonicalWebUrlProvider extends CanonicalWebUrlProvider {
|
|||||||
return canonicalUrl;
|
return canonicalUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestProvider != null) {
|
return guessUrlFromHttpRequest()
|
||||||
|
.orElseGet(() -> "http://" + SystemReader.getInstance().getHostname() + '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
private Optional<String> guessUrlFromHttpRequest() {
|
||||||
|
if (requestProvider == null) {
|
||||||
|
// We have no way of guessing our HTTP url.
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
// No canonical URL configured? Maybe we can get a reasonable
|
// No canonical URL configured? Maybe we can get a reasonable
|
||||||
// guess from the incoming HTTP request, if we are currently
|
// guess from the incoming HTTP request, if we are currently
|
||||||
// inside of an HTTP request scope.
|
// inside of an HTTP request scope.
|
||||||
@@ -56,16 +67,10 @@ public class HttpCanonicalWebUrlProvider extends CanonicalWebUrlProvider {
|
|||||||
if (noWeb.getCause() instanceof OutOfScopeException) {
|
if (noWeb.getCause() instanceof OutOfScopeException) {
|
||||||
// We can't obtain the request as we are not inside of
|
// We can't obtain the request as we are not inside of
|
||||||
// an HTTP request scope. Callers must handle null.
|
// an HTTP request scope. Callers must handle null.
|
||||||
//
|
return Optional.empty();
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
throw noWeb;
|
throw noWeb;
|
||||||
}
|
}
|
||||||
return CanonicalWebUrl.computeFromRequest(req);
|
return Optional.of(CanonicalWebUrl.computeFromRequest(req));
|
||||||
}
|
|
||||||
|
|
||||||
// We have no way of guessing our HTTP url.
|
|
||||||
//
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import static com.google.gerrit.reviewdb.client.RefNames.REFS_CONFIG;
|
|||||||
import static java.util.stream.Collectors.toList;
|
import static java.util.stream.Collectors.toList;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.CharMatcher;
|
|
||||||
import com.google.common.collect.ImmutableList;
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.google.gerrit.common.FooterConstants;
|
import com.google.gerrit.common.FooterConstants;
|
||||||
import com.google.gerrit.common.Nullable;
|
import com.google.gerrit.common.Nullable;
|
||||||
@@ -372,8 +371,8 @@ public class CommitValidators {
|
|||||||
if (hostKeys.isEmpty()) {
|
if (hostKeys.isEmpty()) {
|
||||||
String p = "${gitdir}/hooks/commit-msg";
|
String p = "${gitdir}/hooks/commit-msg";
|
||||||
return String.format(
|
return String.format(
|
||||||
" gitdir=$(git rev-parse --git-dir); curl -o %s %s/tools/hooks/commit-msg ; chmod +x %s",
|
" gitdir=$(git rev-parse --git-dir); curl -o %s %stools/hooks/commit-msg ; chmod +x %s",
|
||||||
p, getGerritUrl(canonicalWebUrl), p);
|
p, canonicalWebUrl, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SSH keys exist, so the hook can be installed with scp.
|
// SSH keys exist, so the hook can be installed with scp.
|
||||||
@@ -882,20 +881,6 @@ public class CommitValidators {
|
|||||||
return new CommitValidationMessage(sb.toString(), false);
|
return new CommitValidationMessage(sb.toString(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the Gerrit URL.
|
|
||||||
*
|
|
||||||
* @return the canonical URL (with any trailing slash removed) if it is configured, otherwise fall
|
|
||||||
* back to "http://hostname" where hostname is the value returned by {@link
|
|
||||||
* #getGerritHost(String)}.
|
|
||||||
*/
|
|
||||||
private static String getGerritUrl(String canonicalWebUrl) {
|
|
||||||
if (canonicalWebUrl != null) {
|
|
||||||
return CharMatcher.is('/').trimTrailingFrom(canonicalWebUrl);
|
|
||||||
}
|
|
||||||
return "http://" + getGerritHost(canonicalWebUrl);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the Gerrit hostname.
|
* Get the Gerrit hostname.
|
||||||
*
|
*
|
||||||
@@ -903,17 +888,14 @@ public class CommitValidators {
|
|||||||
* the hostname is.
|
* the hostname is.
|
||||||
*/
|
*/
|
||||||
private static String getGerritHost(String canonicalWebUrl) {
|
private static String getGerritHost(String canonicalWebUrl) {
|
||||||
String host;
|
|
||||||
if (canonicalWebUrl != null) {
|
if (canonicalWebUrl != null) {
|
||||||
try {
|
try {
|
||||||
host = new URL(canonicalWebUrl).getHost();
|
return new URL(canonicalWebUrl).getHost();
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException ignored) {
|
||||||
host = SystemReader.getInstance().getHostname();
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
host = SystemReader.getInstance().getHostname();
|
|
||||||
}
|
}
|
||||||
return host;
|
|
||||||
|
return SystemReader.getInstance().getHostname();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void addError(String error, List<CommitValidationMessage> messages) {
|
private static void addError(String error, List<CommitValidationMessage> messages) {
|
||||||
|
|||||||
Reference in New Issue
Block a user