Gerrit: Resolve and store gerrit version

This can be used for enabling features that are available from a specific
version.

Change-Id: I4ce1c8684df2b8e194e8bbb02769ba99fc256408
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Orgad Shaneh
2017-10-12 23:35:37 +03:00
committed by Orgad Shaneh
parent a83755157b
commit 5a7a8cbf74
2 changed files with 56 additions and 4 deletions

View File

@@ -32,6 +32,7 @@
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/shellcommand.h> #include <coreplugin/shellcommand.h>
#include <utils/hostosinfo.h> #include <utils/hostosinfo.h>
#include <utils/synchronousprocess.h>
#include <QFile> #include <QFile>
#include <QJsonDocument> #include <QJsonDocument>
@@ -47,12 +48,14 @@ namespace Internal {
static const char defaultHostC[] = "codereview.qt-project.org"; static const char defaultHostC[] = "codereview.qt-project.org";
static const char accountUrlC[] = "/accounts/self"; static const char accountUrlC[] = "/accounts/self";
static const char versionUrlC[] = "/config/server/version";
static const char isGerritKey[] = "IsGerrit"; static const char isGerritKey[] = "IsGerrit";
static const char rootPathKey[] = "RootPath"; static const char rootPathKey[] = "RootPath";
static const char userNameKey[] = "UserName"; static const char userNameKey[] = "UserName";
static const char fullNameKey[] = "FullName"; static const char fullNameKey[] = "FullName";
static const char isAuthenticatedKey[] = "IsAuthenticated"; static const char isAuthenticatedKey[] = "IsAuthenticated";
static const char validateCertKey[] = "ValidateCert"; static const char validateCertKey[] = "ValidateCert";
static const char versionKey[] = "Version";
enum ErrorCodes enum ErrorCodes
{ {
@@ -151,8 +154,10 @@ bool GerritServer::fillFromRemote(const QString &remote,
host = r.host; host = r.host;
port = r.port; port = r.port;
user.userName = r.userName.isEmpty() ? parameters.server.user.userName : r.userName; user.userName = r.userName.isEmpty() ? parameters.server.user.userName : r.userName;
if (type == GerritServer::Ssh) if (type == GerritServer::Ssh) {
resolveVersion(parameters, forceReload);
return true; return true;
}
curlBinary = parameters.curl; curlBinary = parameters.curl;
if (curlBinary.isEmpty() || !QFile::exists(curlBinary)) if (curlBinary.isEmpty() || !QFile::exists(curlBinary))
return false; return false;
@@ -164,7 +169,12 @@ bool GerritServer::fillFromRemote(const QString &remote,
// The rest of the path needs to be inspected to find the root path // The rest of the path needs to be inspected to find the root path
// (can be http://example.net/review) // (can be http://example.net/review)
ascendPath(); ascendPath();
return resolveRoot(); if (resolveRoot()) {
resolveVersion(parameters, forceReload);
saveSettings(Valid);
return true;
}
return false;
case NotGerrit: case NotGerrit:
return false; return false;
case Valid: case Valid:
@@ -318,5 +328,45 @@ bool GerritServer::resolveRoot()
return false; return false;
} }
void GerritServer::resolveVersion(const GerritParameters &p, bool forceReload)
{
static GitClient *const client = GitPlugin::client();
QSettings *settings = Core::ICore::settings();
const QString fullVersionKey = "Gerrit/" + host + '/' + versionKey;
version = settings->value(fullVersionKey).toString();
if (!version.isEmpty() && !forceReload)
return;
if (type == Ssh) {
SynchronousProcess process;
QStringList arguments;
if (port)
arguments << p.portFlag << QString::number(port);
arguments << hostArgument() << "gerrit" << "version";
const SynchronousProcessResponse resp = client->vcsFullySynchronousExec(
QString(), FileName::fromString(p.ssh), arguments,
Core::ShellCommand::NoOutput);
QString stdOut = resp.stdOut().trimmed();
stdOut.remove("gerrit version ");
version = stdOut;
} else {
const QStringList arguments = curlArguments() << (url(RestUrl) + versionUrlC);
const SynchronousProcessResponse resp = client->vcsFullySynchronousExec(
QString(), FileName::fromString(curlBinary), arguments,
Core::ShellCommand::NoOutput);
// REST endpoint for version is only available from 2.8 and up. Do not consider invalid
// if it fails.
if (resp.result == SynchronousProcessResponse::Finished) {
QString output = resp.stdOut();
if (output.isEmpty())
return;
output.remove(0, output.indexOf('\n')); // Strip first line
output.remove('\n');
output.remove('"');
version = output;
}
}
settings->setValue(fullVersionKey, version);
}
} // namespace Internal } // namespace Internal
} // namespace Gerrit } // namespace Gerrit

View File

@@ -75,14 +75,13 @@ public:
QString hostArgument() const; QString hostArgument() const;
QString url(UrlType urlType = DefaultUrl) const; QString url(UrlType urlType = DefaultUrl) const;
bool fillFromRemote(const QString &remote, const GerritParameters &parameters, bool forceReload); bool fillFromRemote(const QString &remote, const GerritParameters &parameters, bool forceReload);
StoredHostValidity loadSettings();
void saveSettings(StoredHostValidity validity) const;
int testConnection(); int testConnection();
QStringList curlArguments() const; QStringList curlArguments() const;
QString host; QString host;
GerritUser user; GerritUser user;
QString rootPath; // for http QString rootPath; // for http
QString version;
unsigned short port = 0; unsigned short port = 0;
HostType type = Ssh; HostType type = Ssh;
bool authenticated = true; bool authenticated = true;
@@ -90,9 +89,12 @@ public:
private: private:
QString curlBinary; QString curlBinary;
StoredHostValidity loadSettings();
void saveSettings(StoredHostValidity validity) const;
bool setupAuthentication(); bool setupAuthentication();
bool ascendPath(); bool ascendPath();
bool resolveRoot(); bool resolveRoot();
void resolveVersion(const GerritParameters &p, bool forceReload);
}; };
} // namespace Internal } // namespace Internal