Gerrit: Persist server details

Avoid repeating connection tests for the same server between runs.

Change-Id: Iecea52af986a708f317878c171d4fcea088cbbde
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Orgad Shaneh
2017-03-13 00:18:50 +02:00
committed by Orgad Shaneh
parent a6a271881f
commit c1d013437f
2 changed files with 75 additions and 7 deletions

View File

@@ -29,12 +29,14 @@
#include "../gitplugin.h"
#include "../gitclient.h"
#include <coreplugin/icore.h>
#include <coreplugin/shellcommand.h>
#include <utils/hostosinfo.h>
#include <QFile>
#include <QJsonDocument>
#include <QRegularExpression>
#include <QSettings>
using namespace Utils;
using namespace Git::Internal;
@@ -44,6 +46,11 @@ namespace Internal {
static const char defaultHostC[] = "codereview.qt-project.org";
static const char accountUrlC[] = "/accounts/self";
static const char isGerritKey[] = "IsGerrit";
static const char rootPathKey[] = "RootPath";
static const char userNameKey[] = "UserName";
static const char fullNameKey[] = "FullName";
static const char isAuthenticatedKey[] = "IsAuthenticated";
bool GerritUser::isSameAs(const GerritUser &other) const
{
@@ -136,12 +143,60 @@ bool GerritServer::fillFromRemote(const QString &remote, const GerritParameters
curlBinary = parameters.curl;
if (curlBinary.isEmpty() || !QFile::exists(curlBinary))
return false;
switch (loadSettings()) {
case Invalid:
rootPath = r.path;
// Strip the last part of the path, which is always the repo name
// The rest of the path needs to be inspected to find the root path
// (can be http://example.net/review)
ascendPath();
return resolveRoot();
case NotGerrit:
return false;
case Valid:
return true;
}
return true;
}
GerritServer::StoredHostValidity GerritServer::loadSettings()
{
StoredHostValidity validity = Invalid;
QSettings *settings = Core::ICore::settings();
settings->beginGroup("Gerrit/" + host);
if (!settings->value(isGerritKey, true).toBool()) {
validity = NotGerrit;
} else if (settings->contains(isAuthenticatedKey)) {
rootPath = settings->value(rootPathKey).toString();
user.userName = settings->value(userNameKey).toString();
user.fullName = settings->value(fullNameKey).toString();
authenticated = settings->value(isAuthenticatedKey).toBool();
validity = Valid;
}
settings->endGroup();
return validity;
}
void GerritServer::saveSettings(StoredHostValidity validity) const
{
QSettings *settings = Core::ICore::settings();
settings->beginGroup("Gerrit/" + host);
switch (validity) {
case NotGerrit:
settings->setValue(isGerritKey, false);
break;
case Valid:
settings->setValue(rootPathKey, rootPath);
settings->setValue(userNameKey, user.userName);
settings->setValue(fullNameKey, user.fullName);
settings->setValue(isAuthenticatedKey, authenticated);
break;
case Invalid:
settings->clear();
break;
}
settings->endGroup();
}
QStringList GerritServer::curlArguments()
@@ -182,6 +237,7 @@ bool GerritServer::setupAuthentication()
if (!dialog.exec())
return false;
authenticated = dialog.isAuthenticated();
saveSettings(Valid);
return true;
}
@@ -199,12 +255,15 @@ bool GerritServer::resolveRoot()
for (;;) {
switch (testConnection()) {
case 200:
saveSettings(Valid);
return true;
case 401:
return setupAuthentication();
case 404:
if (!ascendPath())
if (!ascendPath()) {
saveSettings(NotGerrit);
return false;
}
break;
default: // unknown error - fail
return false;

View File

@@ -61,6 +61,13 @@ public:
RestUrl
};
enum StoredHostValidity
{
Invalid,
NotGerrit,
Valid
};
GerritServer();
GerritServer(const QString &host, unsigned short port, const QString &userName, HostType type);
bool operator==(const GerritServer &other) const;
@@ -68,6 +75,8 @@ public:
QString hostArgument() const;
QString url(UrlType urlType = DefaultUrl) const;
bool fillFromRemote(const QString &remote, const GerritParameters &parameters);
StoredHostValidity loadSettings();
void saveSettings(StoredHostValidity validity) const;
int testConnection();
static QStringList curlArguments();