forked from qt-creator/qt-creator
GitLab: Introduce minimal GitLab plugin
So far only adding global settings and project settings. Global settings allow to specify configurations used later inside the project settings or when accessing GitLab server instances. Project settings already can "link" to GitLab projects, which will allow to fetch notifications for the project later on. Real functionality is added in the follow ups. Change-Id: I8a0f978001c58c6dc9e07917c183891abec9a3d0 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -62,6 +62,7 @@ add_subdirectory(languageclient)
|
||||
add_subdirectory(cmakeprojectmanager)
|
||||
add_subdirectory(debugger)
|
||||
add_subdirectory(coco)
|
||||
add_subdirectory(gitlab)
|
||||
|
||||
# Level 7:
|
||||
add_subdirectory(android)
|
||||
|
12
src/plugins/gitlab/CMakeLists.txt
Normal file
12
src/plugins/gitlab/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
add_qtc_plugin(GitLab
|
||||
PLUGIN_CLASS GitLabPlugin
|
||||
PLUGIN_DEPENDS Core ProjectExplorer Git VcsBase
|
||||
DEPENDS Utils
|
||||
SOURCES
|
||||
gitlaboptionspage.cpp gitlaboptionspage.h
|
||||
gitlabparameters.cpp gitlabparameters.h
|
||||
gitlabplugin.cpp gitlabplugin.h
|
||||
gitlabprojectsettings.cpp gitlabprojectsettings.h
|
||||
queryrunner.cpp queryrunner.h
|
||||
resultparser.cpp resultparser.h
|
||||
)
|
19
src/plugins/gitlab/GitLab.json.in
Normal file
19
src/plugins/gitlab/GitLab.json.in
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
\"Name\" : \"GitLab\",
|
||||
\"Version\" : \"$$QTCREATOR_VERSION\",
|
||||
\"CompatVersion\" : \"$$QTCREATOR_COMPAT_VERSION\",
|
||||
\"Experimental\" : true,
|
||||
\"Vendor\" : \"The Qt Company Ltd\",
|
||||
\"Copyright\" : \"(C) $$QTCREATOR_COPYRIGHT_YEAR The Qt Company Ltd\",
|
||||
\"License\" : [ \"Commercial Usage\",
|
||||
\"\",
|
||||
\"Licensees holding valid Qt Commercial licenses may use this plugin in accordance with the Qt Commercial License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and The Qt Company.\",
|
||||
\"\",
|
||||
\"GNU General Public License Usage\",
|
||||
\"\",
|
||||
\"Alternatively, this file may be used under the terms of the GNU General Public License version 3 as published by the Free Software Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT included in the packaging of this file. Please review the following information to ensure the GNU General Public License requirements will be met: https://www.gnu.org/licenses/gpl-3.0.html.\"
|
||||
],
|
||||
\"Description\" : \"GitLab plugin.\",
|
||||
\"Url\" : \"http://www.qt.io\",
|
||||
$$dependencyList
|
||||
}
|
26
src/plugins/gitlab/gitlab.qbs
Normal file
26
src/plugins/gitlab/gitlab.qbs
Normal file
@@ -0,0 +1,26 @@
|
||||
import qbs
|
||||
|
||||
QtcPlugin {
|
||||
name: "GitLab"
|
||||
|
||||
Depends { name: "Core" }
|
||||
Depends { name: "ProjectExplorer" }
|
||||
Depends { name: "Git" }
|
||||
Depends { name: "VcsBase" }
|
||||
Depends { name: "Utils" }
|
||||
|
||||
files: [
|
||||
"gitlaboptionspage.cpp",
|
||||
"gitlaboptionspage.h",
|
||||
"gitlabparameters.cpp",
|
||||
"gitlabparameters.h",
|
||||
"gitlabplugin.cpp",
|
||||
"gitlabplugin.h",
|
||||
"gitlabprojectsettings.cpp",
|
||||
"gitlabprojectsettings.h",
|
||||
"queryrunner.cpp",
|
||||
"queryrunner.h",
|
||||
"resultparser.cpp",
|
||||
"resultparser.h",
|
||||
]
|
||||
}
|
303
src/plugins/gitlab/gitlaboptionspage.cpp
Normal file
303
src/plugins/gitlab/gitlaboptionspage.cpp
Normal file
@@ -0,0 +1,303 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "gitlaboptionspage.h"
|
||||
|
||||
#include "gitlabparameters.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/layoutbuilder.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <vcsbase/vcsbaseconstants.h>
|
||||
|
||||
#include <QAction>
|
||||
#include <QComboBox>
|
||||
#include <QDialogButtonBox>
|
||||
#include <QFormLayout>
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QRegularExpression>
|
||||
#include <QUuid>
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
static bool hostValid(const QString &host)
|
||||
{
|
||||
static const QRegularExpression ip(R"(^(\d+).(\d+).(\d+).(\d+)$)");
|
||||
static const QRegularExpression dn(R"(^([a-zA-Z0-9][a-zA-Z0-9-]+\.)+[a-zA-Z0-9][a-zA-Z0-9-]+$)");
|
||||
const QRegularExpressionMatch match = ip.match(host);
|
||||
if (match.hasMatch()) {
|
||||
for (int i = 1; i < 5; ++i) {
|
||||
int val = match.captured(i).toInt();
|
||||
if (val < 0 || val > 255)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return dn.match(host).hasMatch();
|
||||
}
|
||||
|
||||
GitLabServerWidget::GitLabServerWidget(Mode m, QWidget *parent)
|
||||
: QWidget(parent)
|
||||
, m_mode(m)
|
||||
{
|
||||
using namespace Utils::Layouting;
|
||||
|
||||
auto hostLabel = new QLabel(tr("Host:"), this);
|
||||
m_host.setDisplayStyle(m == Display ? Utils::StringAspect::LabelDisplay
|
||||
: Utils::StringAspect::LineEditDisplay);
|
||||
m_host.setValidationFunction([](Utils::FancyLineEdit *l, QString *) {
|
||||
return hostValid(l->text());
|
||||
});
|
||||
auto descriptionLabel = new QLabel(tr("Description:"), this);
|
||||
m_description.setDisplayStyle(m == Display ? Utils::StringAspect::LabelDisplay
|
||||
: Utils::StringAspect::LineEditDisplay);
|
||||
auto tokenLabel = new QLabel(tr("Access token:"), this);
|
||||
m_token.setDisplayStyle(m == Display ? Utils::StringAspect::LabelDisplay
|
||||
: Utils::StringAspect::LineEditDisplay);
|
||||
m_token.setVisible(m == Edit);
|
||||
tokenLabel->setVisible(m == Edit);
|
||||
m_port.setRange(1, 65535);
|
||||
auto portLabel = new QLabel(tr("Port:"), this);
|
||||
m_port.setDefaultValue(GitLabServer::defaultPort);
|
||||
m_port.setEnabled(m == Edit);
|
||||
|
||||
using namespace Utils::Layouting;
|
||||
const Break nl;
|
||||
|
||||
Form {
|
||||
hostLabel, m_host, nl,
|
||||
descriptionLabel, m_description, nl,
|
||||
tokenLabel, m_token, nl,
|
||||
portLabel, Span(1, Row { m_port, Stretch() }), nl,
|
||||
}.attachTo(this, m == Edit);
|
||||
}
|
||||
|
||||
GitLabServer GitLabServerWidget::gitLabServer() const
|
||||
{
|
||||
GitLabServer result;
|
||||
result.id = m_mode == Edit ? Utils::Id::fromName(QUuid::createUuid().toByteArray()) : m_id;
|
||||
result.host = m_host.value();
|
||||
result.description = m_description.value();
|
||||
result.token = m_token.value();
|
||||
result.port = m_port.value();
|
||||
return result;
|
||||
}
|
||||
|
||||
void GitLabServerWidget::setGitLabServer(const GitLabServer &server)
|
||||
{
|
||||
m_id = server.id;
|
||||
m_host.setValue(server.host);
|
||||
m_description.setValue(server.description);
|
||||
m_token.setValue(server.token);
|
||||
m_port.setValue(server.port);
|
||||
}
|
||||
|
||||
GitLabOptionsWidget::GitLabOptionsWidget(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
auto defaultLabel = new QLabel(tr("Default:"), this);
|
||||
m_defaultGitLabServer = new QComboBox(this);
|
||||
m_curl.setDisplayStyle(Utils::StringAspect::DisplayStyle::PathChooserDisplay);
|
||||
m_curl.setLabelText(tr("curl:"));
|
||||
m_curl.setExpectedKind(Utils::PathChooser::ExistingCommand);
|
||||
|
||||
m_gitLabServerWidget = new GitLabServerWidget(GitLabServerWidget::Display, this);
|
||||
|
||||
m_edit = new QPushButton(tr("Edit..."), this);
|
||||
m_edit->setToolTip(tr("Edit current selected GitLab server configuration."));
|
||||
m_remove = new QPushButton(tr("Remove"), this);
|
||||
m_remove->setToolTip(tr("Remove current selected GitLab server configuration."));
|
||||
m_add = new QPushButton(tr("Add..."), this);
|
||||
m_add->setToolTip(tr("Add new GitLab server configuration."));
|
||||
|
||||
using namespace Utils::Layouting;
|
||||
const Break nl;
|
||||
|
||||
Grid {
|
||||
Form {
|
||||
defaultLabel, m_defaultGitLabServer, nl,
|
||||
Row { Group { m_gitLabServerWidget, Space(1) } }, nl,
|
||||
m_curl, nl,
|
||||
}, Column { m_add, m_edit, m_remove, Stretch() },
|
||||
}.attachTo(this);
|
||||
|
||||
connect(m_edit, &QPushButton::clicked, this, &GitLabOptionsWidget::showEditServerDialog);
|
||||
connect(m_remove, &QPushButton::clicked, this, &GitLabOptionsWidget::removeCurrentTriggered);
|
||||
connect(m_add, &QPushButton::clicked, this, &GitLabOptionsWidget::showAddServerDialog);
|
||||
connect(m_defaultGitLabServer, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, [this]() {
|
||||
m_gitLabServerWidget->setGitLabServer(
|
||||
m_defaultGitLabServer->currentData().value<GitLabServer>());
|
||||
});
|
||||
}
|
||||
|
||||
GitLabParameters GitLabOptionsWidget::parameters() const
|
||||
{
|
||||
GitLabParameters result;
|
||||
// get all configured gitlabservers
|
||||
for (int i = 0, end = m_defaultGitLabServer->count(); i < end; ++i)
|
||||
result.gitLabServers.append(m_defaultGitLabServer->itemData(i).value<GitLabServer>());
|
||||
if (m_defaultGitLabServer->count())
|
||||
result.defaultGitLabServer = m_defaultGitLabServer->currentData().value<GitLabServer>().id;
|
||||
result.curl = m_curl.filePath();
|
||||
return result;
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::setParameters(const GitLabParameters ¶ms)
|
||||
{
|
||||
m_curl.setFilePath(params.curl);
|
||||
|
||||
for (const auto &gitLabServer : params.gitLabServers) {
|
||||
m_defaultGitLabServer->addItem(gitLabServer.displayString(),
|
||||
QVariant::fromValue(gitLabServer));
|
||||
}
|
||||
|
||||
const GitLabServer found = params.currentDefaultServer();
|
||||
if (found.id.isValid()) {
|
||||
m_defaultGitLabServer->setCurrentIndex(m_defaultGitLabServer->findData(
|
||||
QVariant::fromValue(found)));
|
||||
}
|
||||
updateButtonsState();
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::showEditServerDialog()
|
||||
{
|
||||
const GitLabServer old = m_defaultGitLabServer->currentData().value<GitLabServer>();
|
||||
QDialog d;
|
||||
d.setWindowTitle(tr("Edit Server..."));
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
GitLabServerWidget *serverWidget = new GitLabServerWidget(GitLabServerWidget::Edit, this);
|
||||
serverWidget->setGitLabServer(old);
|
||||
layout->addWidget(serverWidget);
|
||||
auto buttons = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
|
||||
auto modifyButton = buttons->addButton(tr("Modify"), QDialogButtonBox::AcceptRole);
|
||||
connect(modifyButton, &QPushButton::clicked, &d, &QDialog::accept);
|
||||
connect(buttons->button(QDialogButtonBox::Cancel), &QPushButton::clicked, &d, &QDialog::reject);
|
||||
layout->addWidget(buttons);
|
||||
d.setLayout(layout);
|
||||
d.resize(300, 200);
|
||||
if (d.exec() != QDialog::Accepted)
|
||||
return;
|
||||
|
||||
const GitLabServer server = serverWidget->gitLabServer();
|
||||
if (server != old && hostValid(server.host))
|
||||
modifyCurrentServer(server);
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::showAddServerDialog()
|
||||
{
|
||||
QDialog d;
|
||||
d.setWindowTitle(tr("Add Server..."));
|
||||
QVBoxLayout *layout = new QVBoxLayout;
|
||||
GitLabServerWidget *serverWidget = new GitLabServerWidget(GitLabServerWidget::Edit, this);
|
||||
layout->addWidget(serverWidget);
|
||||
auto buttons = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
|
||||
auto addButton = buttons->addButton(tr("Add"), QDialogButtonBox::AcceptRole);
|
||||
connect(addButton, &QPushButton::clicked, &d, &QDialog::accept);
|
||||
connect(buttons->button(QDialogButtonBox::Cancel), &QPushButton::clicked, &d, &QDialog::reject);
|
||||
layout->addWidget(buttons);
|
||||
d.setLayout(layout);
|
||||
d.resize(300, 200);
|
||||
if (d.exec() != QDialog::Accepted)
|
||||
return;
|
||||
|
||||
const GitLabServer server = serverWidget->gitLabServer();
|
||||
if (hostValid(server.host))
|
||||
addServer(server);
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::removeCurrentTriggered()
|
||||
{
|
||||
int current = m_defaultGitLabServer->currentIndex();
|
||||
if (current > -1)
|
||||
m_defaultGitLabServer->removeItem(current);
|
||||
updateButtonsState();
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::addServer(const GitLabServer &newServer)
|
||||
{
|
||||
QTC_ASSERT(newServer.id.isValid(), return);
|
||||
const QVariant variant = QVariant::fromValue(newServer);
|
||||
m_defaultGitLabServer->addItem(newServer.displayString(), variant);
|
||||
int index = m_defaultGitLabServer->findData(variant);
|
||||
m_defaultGitLabServer->setCurrentIndex(index);
|
||||
m_gitLabServerWidget->setGitLabServer(newServer);
|
||||
updateButtonsState();
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::modifyCurrentServer(const GitLabServer &newServer)
|
||||
{
|
||||
int current = m_defaultGitLabServer->currentIndex();
|
||||
if (current > -1)
|
||||
m_defaultGitLabServer->setItemData(current, newServer.displayString(), Qt::DisplayRole);
|
||||
m_defaultGitLabServer->setItemData(current, QVariant::fromValue(newServer));
|
||||
m_gitLabServerWidget->setGitLabServer(newServer);
|
||||
}
|
||||
|
||||
void GitLabOptionsWidget::updateButtonsState()
|
||||
{
|
||||
const bool hasItems = m_defaultGitLabServer->count() > 0;
|
||||
m_edit->setEnabled(hasItems);
|
||||
m_remove->setEnabled(hasItems);
|
||||
}
|
||||
|
||||
GitLabOptionsPage::GitLabOptionsPage(GitLabParameters *p, QObject *parent)
|
||||
: Core::IOptionsPage{parent}
|
||||
, m_parameters(p)
|
||||
{
|
||||
setId("GitLab");
|
||||
setDisplayName(tr("GitLab"));
|
||||
setCategory(VcsBase::Constants::VCS_SETTINGS_CATEGORY);
|
||||
}
|
||||
|
||||
QWidget *GitLabOptionsPage::widget()
|
||||
{
|
||||
if (!m_widget) {
|
||||
m_widget = new GitLabOptionsWidget;
|
||||
m_widget->setParameters(*m_parameters);
|
||||
}
|
||||
return m_widget;
|
||||
}
|
||||
|
||||
void GitLabOptionsPage::apply()
|
||||
{
|
||||
if (GitLabOptionsWidget *w = m_widget.data()) {
|
||||
GitLabParameters newParameters = w->parameters();
|
||||
if (newParameters != *m_parameters) {
|
||||
*m_parameters = newParameters;
|
||||
m_parameters->toSettings(Core::ICore::settings());
|
||||
emit settingsChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GitLabOptionsPage::finish()
|
||||
{
|
||||
delete m_widget;
|
||||
}
|
||||
|
||||
} // namespace GitLab
|
106
src/plugins/gitlab/gitlaboptionspage.h
Normal file
106
src/plugins/gitlab/gitlaboptionspage.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gitlabparameters.h"
|
||||
|
||||
#include <coreplugin/dialogs/ioptionspage.h>
|
||||
#include <utils/aspects.h>
|
||||
|
||||
#include <QPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QComboBox;
|
||||
class QPushButton;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
class GitLabServerWidget : public QWidget
|
||||
{
|
||||
public:
|
||||
enum Mode { Display, Edit };
|
||||
explicit GitLabServerWidget(Mode m, QWidget *parent = nullptr);
|
||||
|
||||
GitLabServer gitLabServer() const;
|
||||
void setGitLabServer(const GitLabServer &server);
|
||||
|
||||
bool isValid() const;
|
||||
private:
|
||||
Mode m_mode = Display;
|
||||
Utils::Id m_id;
|
||||
Utils::StringAspect m_host;
|
||||
Utils::StringAspect m_description;
|
||||
Utils::StringAspect m_token;
|
||||
Utils::IntegerAspect m_port;
|
||||
};
|
||||
|
||||
class GitLabOptionsWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GitLabOptionsWidget(QWidget *parent = nullptr);
|
||||
|
||||
GitLabParameters parameters() const;
|
||||
void setParameters(const GitLabParameters ¶ms);
|
||||
|
||||
private:
|
||||
void showEditServerDialog();
|
||||
void showAddServerDialog();
|
||||
void removeCurrentTriggered();
|
||||
void addServer(const GitLabServer &newServer);
|
||||
void modifyCurrentServer(const GitLabServer &newServer);
|
||||
void updateButtonsState();
|
||||
|
||||
GitLabServerWidget *m_gitLabServerWidget = nullptr;
|
||||
QPushButton *m_edit = nullptr;
|
||||
QPushButton *m_remove = nullptr;
|
||||
QPushButton *m_add = nullptr;
|
||||
QComboBox *m_defaultGitLabServer = nullptr;
|
||||
Utils::StringAspect m_curl;
|
||||
};
|
||||
|
||||
class GitLabOptionsPage : public Core::IOptionsPage
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GitLabOptionsPage(GitLabParameters *p, QObject *parent = nullptr);
|
||||
|
||||
QWidget *widget() final;
|
||||
void apply() final;
|
||||
void finish() final;
|
||||
|
||||
signals:
|
||||
void settingsChanged();
|
||||
|
||||
private:
|
||||
void addServer();
|
||||
|
||||
GitLabParameters *m_parameters;
|
||||
QPointer<GitLabOptionsWidget> m_widget;
|
||||
};
|
||||
|
||||
} // namespace GitLab
|
212
src/plugins/gitlab/gitlabparameters.cpp
Normal file
212
src/plugins/gitlab/gitlabparameters.cpp
Normal file
@@ -0,0 +1,212 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "gitlabparameters.h"
|
||||
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/hostosinfo.h>
|
||||
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
const char settingsGroupC[] = "GitLab";
|
||||
const char curlKeyC[] = "Curl";
|
||||
const char defaultUuidKeyC[] = "DefaultUuid";
|
||||
|
||||
GitLabServer::GitLabServer()
|
||||
{
|
||||
}
|
||||
|
||||
GitLabServer::GitLabServer(const Utils::Id &id, const QString &host, const QString &description,
|
||||
const QString &token, unsigned short port)
|
||||
: id(id)
|
||||
, host(host)
|
||||
, description(description)
|
||||
, token(token)
|
||||
, port(port)
|
||||
{
|
||||
}
|
||||
|
||||
bool GitLabServer::operator==(const GitLabServer &other) const
|
||||
{
|
||||
if (port && other.port && port != other.port)
|
||||
return false;
|
||||
return id == other.id && host == other.host && description == other.description
|
||||
&& token == other.token ;
|
||||
}
|
||||
|
||||
bool GitLabServer::operator!=(const GitLabServer &other) const
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
QJsonObject GitLabServer::toJson() const
|
||||
{
|
||||
QJsonObject result;
|
||||
result.insert("id", id.toString());
|
||||
result.insert("host", host);
|
||||
result.insert("description", description);
|
||||
result.insert("port", port);
|
||||
result.insert("token", token);
|
||||
return result;
|
||||
}
|
||||
|
||||
GitLabServer GitLabServer::fromJson(const QJsonObject &json)
|
||||
{
|
||||
GitLabServer invalid{Utils::Id(), "", "", "", 0};
|
||||
const QJsonValue id = json.value("id");
|
||||
if (id == QJsonValue::Undefined)
|
||||
return invalid;
|
||||
const QJsonValue host = json.value("host");
|
||||
if (host == QJsonValue::Undefined)
|
||||
return invalid;
|
||||
const QJsonValue description = json.value("description");
|
||||
if (description == QJsonValue::Undefined)
|
||||
return invalid;
|
||||
const QJsonValue token = json.value("token");
|
||||
if (token == QJsonValue::Undefined)
|
||||
return invalid;
|
||||
const QJsonValue port = json.value("port");
|
||||
if (port == QJsonValue::Undefined)
|
||||
return invalid;
|
||||
return {Utils::Id::fromString(id.toString()), host.toString(), description.toString(),
|
||||
token.toString(), (unsigned short)port.toInt()};
|
||||
}
|
||||
|
||||
QStringList GitLabServer::curlArguments() const
|
||||
{
|
||||
// credentials from .netrc (?), no progress
|
||||
QStringList args = { "-nsS" };
|
||||
if (!validateCert)
|
||||
args << "-k";
|
||||
return args;
|
||||
}
|
||||
|
||||
QString GitLabServer::displayString() const
|
||||
{
|
||||
if (!description.isEmpty())
|
||||
return host + " (" + description + ')';
|
||||
return host;
|
||||
}
|
||||
|
||||
GitLabParameters::GitLabParameters()
|
||||
{
|
||||
}
|
||||
|
||||
bool GitLabParameters::equals(const GitLabParameters &other) const
|
||||
{
|
||||
return curl == other.curl && defaultGitLabServer == other.defaultGitLabServer
|
||||
&& gitLabServers == other.gitLabServers;
|
||||
}
|
||||
|
||||
bool GitLabParameters::isValid() const
|
||||
{
|
||||
const GitLabServer found = currentDefaultServer();
|
||||
return found.id.isValid() && !found.host.isEmpty() && curl.isExecutableFile() ;
|
||||
}
|
||||
|
||||
static void writeTokensFile(const Utils::FilePath &filePath, const QList<GitLabServer> &servers)
|
||||
{
|
||||
QJsonDocument doc;
|
||||
QJsonArray array;
|
||||
for (const GitLabServer &server : servers)
|
||||
array.append(server.toJson());
|
||||
doc.setArray(array);
|
||||
filePath.writeFileContents(doc.toJson());
|
||||
filePath.setPermissions(QFile::ReadUser | QFile::WriteUser);
|
||||
}
|
||||
|
||||
static QList<GitLabServer> readTokensFile(const Utils::FilePath &filePath)
|
||||
{
|
||||
if (!filePath.exists())
|
||||
return {};
|
||||
const QByteArray content = filePath.fileContents();
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(content);
|
||||
if (!doc.isArray())
|
||||
return {};
|
||||
|
||||
QList<GitLabServer> result;
|
||||
const QJsonArray array = doc.array();
|
||||
for (const auto &it : array) {
|
||||
if (it.isObject())
|
||||
result.append(GitLabServer::fromJson(it.toObject()));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Utils::FilePath tokensFilePath(const QSettings *s)
|
||||
{
|
||||
return Utils::FilePath::fromString(s->fileName()).parentDir()
|
||||
.pathAppended("/qtcreator/gitlabtokens.json");
|
||||
}
|
||||
|
||||
void GitLabParameters::toSettings(QSettings *s) const
|
||||
{
|
||||
|
||||
writeTokensFile(tokensFilePath(s), gitLabServers);
|
||||
s->beginGroup(settingsGroupC);
|
||||
s->setValue(curlKeyC, curl.toVariant());
|
||||
s->setValue(defaultUuidKeyC, defaultGitLabServer.toSetting());
|
||||
s->endGroup();
|
||||
}
|
||||
|
||||
void GitLabParameters::fromSettings(const QSettings *s)
|
||||
{
|
||||
const QString rootKey = QLatin1String(settingsGroupC) + '/';
|
||||
curl = Utils::FilePath::fromVariant(s->value(rootKey + curlKeyC));
|
||||
defaultGitLabServer = Utils::Id::fromSetting(s->value(rootKey + defaultUuidKeyC));
|
||||
|
||||
gitLabServers = readTokensFile(tokensFilePath(s));
|
||||
|
||||
if (gitLabServers.isEmpty())
|
||||
defaultGitLabServer = Utils::Id();
|
||||
|
||||
if (curl.isEmpty() || !curl.exists()) {
|
||||
const QString curlPath = QStandardPaths::findExecutable(
|
||||
Utils::HostOsInfo::withExecutableSuffix("curl"));
|
||||
if (!curlPath.isEmpty())
|
||||
curl = Utils::FilePath::fromString(curlPath);
|
||||
}
|
||||
}
|
||||
|
||||
GitLabServer GitLabParameters::currentDefaultServer() const
|
||||
{
|
||||
return serverForId(defaultGitLabServer);
|
||||
}
|
||||
|
||||
GitLabServer GitLabParameters::serverForId(const Utils::Id &id) const
|
||||
{
|
||||
return Utils::findOrDefault(gitLabServers, [id](const GitLabServer &s) {
|
||||
return id == s.id;
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace GitLab
|
92
src/plugins/gitlab/gitlabparameters.h
Normal file
92
src/plugins/gitlab/gitlabparameters.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/id.h>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QSettings;
|
||||
class QJsonObject;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
class GitLabServer
|
||||
{
|
||||
public:
|
||||
enum { defaultPort = 443 };
|
||||
|
||||
GitLabServer(); // TODO different protocol handling e.g. for clone / push?
|
||||
GitLabServer(const Utils::Id &id, const QString &host, const QString &description,
|
||||
const QString &token, unsigned short port);;
|
||||
bool operator==(const GitLabServer &other) const;
|
||||
bool operator!=(const GitLabServer &other) const;
|
||||
QJsonObject toJson() const;
|
||||
static GitLabServer fromJson(const QJsonObject &json);
|
||||
QStringList curlArguments() const;
|
||||
QString displayString() const;
|
||||
|
||||
Utils::Id id;
|
||||
QString host;
|
||||
QString description;
|
||||
QString token;
|
||||
unsigned short port = 0;
|
||||
|
||||
bool validateCert = true; // TODO
|
||||
};
|
||||
|
||||
class GitLabParameters
|
||||
{
|
||||
public:
|
||||
GitLabParameters();
|
||||
|
||||
bool equals(const GitLabParameters &other) const;
|
||||
bool isValid() const;
|
||||
|
||||
void toSettings(QSettings *s) const;
|
||||
void fromSettings(const QSettings *s);
|
||||
|
||||
GitLabServer currentDefaultServer() const;
|
||||
GitLabServer serverForId(const Utils::Id &id) const;
|
||||
|
||||
friend bool operator==(const GitLabParameters &p1, const GitLabParameters &p2)
|
||||
{
|
||||
return p1.equals(p2);
|
||||
}
|
||||
friend bool operator!=(const GitLabParameters &p1, const GitLabParameters &p2)
|
||||
{
|
||||
return !p1.equals(p2);
|
||||
}
|
||||
|
||||
Utils::Id defaultGitLabServer;
|
||||
QList<GitLabServer> gitLabServers;
|
||||
Utils::FilePath curl;
|
||||
};
|
||||
|
||||
} // namespace GitLab
|
||||
|
||||
Q_DECLARE_METATYPE(GitLab::GitLabServer)
|
110
src/plugins/gitlab/gitlabplugin.cpp
Normal file
110
src/plugins/gitlab/gitlabplugin.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "gitlabplugin.h"
|
||||
|
||||
#include "gitlaboptionspage.h"
|
||||
#include "gitlabparameters.h"
|
||||
#include "gitlabprojectsettings.h"
|
||||
|
||||
#include <coreplugin/icore.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/projectpanelfactory.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
class GitLabPluginPrivate
|
||||
{
|
||||
public:
|
||||
GitLabParameters parameters;
|
||||
GitLabOptionsPage optionsPage{¶meters};
|
||||
QHash<ProjectExplorer::Project *, GitLabProjectSettings *> projectSettings;
|
||||
};
|
||||
|
||||
static GitLabPluginPrivate *dd = nullptr;
|
||||
|
||||
GitLabPlugin::GitLabPlugin()
|
||||
{
|
||||
}
|
||||
|
||||
GitLabPlugin::~GitLabPlugin()
|
||||
{
|
||||
if (!dd->projectSettings.isEmpty()) {
|
||||
qDeleteAll(dd->projectSettings);
|
||||
dd->projectSettings.clear();
|
||||
}
|
||||
delete dd;
|
||||
dd = nullptr;
|
||||
}
|
||||
|
||||
bool GitLabPlugin::initialize(const QStringList & /*arguments*/, QString * /*errorString*/)
|
||||
{
|
||||
dd = new GitLabPluginPrivate;
|
||||
dd->parameters.fromSettings(Core::ICore::settings());
|
||||
auto panelFactory = new ProjectExplorer::ProjectPanelFactory;
|
||||
panelFactory->setPriority(999);
|
||||
panelFactory->setDisplayName(tr("GitLab"));
|
||||
panelFactory->setCreateWidgetFunction([](ProjectExplorer::Project *project) {
|
||||
return new GitLabProjectSettingsWidget(project);
|
||||
});
|
||||
ProjectExplorer::ProjectPanelFactory::registerFactory(panelFactory);
|
||||
return true;
|
||||
}
|
||||
|
||||
QList<GitLabServer> GitLabPlugin::allGitLabServers()
|
||||
{
|
||||
QTC_ASSERT(dd, return {});
|
||||
return dd->parameters.gitLabServers;
|
||||
}
|
||||
|
||||
GitLabServer GitLabPlugin::gitLabServerForId(const Utils::Id &id)
|
||||
{
|
||||
QTC_ASSERT(dd, return {});
|
||||
return dd->parameters.serverForId(id);
|
||||
}
|
||||
|
||||
GitLabParameters *GitLabPlugin::globalParameters()
|
||||
{
|
||||
return &dd->parameters;
|
||||
}
|
||||
|
||||
GitLabProjectSettings *GitLabPlugin::projectSettings(ProjectExplorer::Project *project)
|
||||
{
|
||||
QTC_ASSERT(project, return nullptr);
|
||||
QTC_ASSERT(dd, return nullptr);
|
||||
auto &settings = dd->projectSettings[project];
|
||||
if (!settings)
|
||||
settings = new GitLabProjectSettings(project);
|
||||
return settings;
|
||||
}
|
||||
|
||||
GitLabOptionsPage *GitLabPlugin::optionsPage()
|
||||
{
|
||||
QTC_ASSERT(dd, return {});
|
||||
return &dd->optionsPage;
|
||||
}
|
||||
|
||||
} // namespace GitLab
|
57
src/plugins/gitlab/gitlabplugin.h
Normal file
57
src/plugins/gitlab/gitlabplugin.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "gitlabparameters.h"
|
||||
|
||||
#include <extensionsystem/iplugin.h>
|
||||
|
||||
namespace ProjectExplorer { class Project; }
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
class GitLabProjectSettings;
|
||||
class GitLabOptionsPage;
|
||||
|
||||
class GitLabPlugin : public ExtensionSystem::IPlugin
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QtCreatorPlugin" FILE "GitLab.json")
|
||||
|
||||
public:
|
||||
GitLabPlugin();
|
||||
~GitLabPlugin() override;
|
||||
|
||||
bool initialize(const QStringList &arguments, QString *errorString) override;
|
||||
|
||||
static QList<GitLabServer> allGitLabServers();
|
||||
static GitLabServer gitLabServerForId(const Utils::Id &id);
|
||||
static GitLabParameters *globalParameters();
|
||||
static GitLabProjectSettings *projectSettings(ProjectExplorer::Project *project);
|
||||
static GitLabOptionsPage *optionsPage();
|
||||
};
|
||||
|
||||
} // namespace GitLab
|
316
src/plugins/gitlab/gitlabprojectsettings.cpp
Normal file
316
src/plugins/gitlab/gitlabprojectsettings.cpp
Normal file
@@ -0,0 +1,316 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "gitlabprojectsettings.h"
|
||||
|
||||
#include "gitlaboptionspage.h"
|
||||
#include "gitlabplugin.h"
|
||||
#include "queryrunner.h"
|
||||
#include "resultparser.h"
|
||||
|
||||
#include <git/gitclient.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <utils/infolabel.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <QComboBox>
|
||||
#include <QFormLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QUrl>
|
||||
#include <QVBoxLayout>
|
||||
#include <QVariant>
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
const char PSK_LINKED_ID[] = "GitLab.LinkedId";
|
||||
const char PSK_SERVER[] = "GitLab.Server";
|
||||
const char PSK_PROJECT[] = "GitLab.Project";
|
||||
|
||||
static QString accessLevelString(int accessLevel)
|
||||
{
|
||||
const char trContext[] = "GitLab::GitLabProjectSettingsWidget";
|
||||
switch (accessLevel) {
|
||||
case 10: return QCoreApplication::translate(trContext, "Guest");
|
||||
case 20: return QCoreApplication::translate(trContext, "Reporter");
|
||||
case 30: return QCoreApplication::translate(trContext, "Developer");
|
||||
case 40: return QCoreApplication::translate(trContext, "Maintainer");
|
||||
case 50: return QCoreApplication::translate(trContext, "Owner");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
std::tuple<QString, QString, int>
|
||||
GitLabProjectSettings::remotePartsFromRemote(const QString &remote)
|
||||
{
|
||||
QString host;
|
||||
QString path;
|
||||
int port = -1;
|
||||
if (remote.startsWith("git@")) {
|
||||
int colon = remote.indexOf(':');
|
||||
host = remote.mid(4, colon - 4);
|
||||
path = remote.mid(colon + 1);
|
||||
} else {
|
||||
const QUrl url(remote);
|
||||
host = url.host();
|
||||
path = url.path().mid(1); // ignore leading slash
|
||||
port = url.port();
|
||||
}
|
||||
if (path.endsWith(".git"))
|
||||
path.chop(4);
|
||||
|
||||
return std::make_tuple(host, path, port);
|
||||
}
|
||||
|
||||
GitLabProjectSettings::GitLabProjectSettings(ProjectExplorer::Project *project)
|
||||
: m_project(project)
|
||||
{
|
||||
load();
|
||||
connect(project, &ProjectExplorer::Project::settingsLoaded,
|
||||
this, &GitLabProjectSettings::load);
|
||||
connect(project, &ProjectExplorer::Project::aboutToSaveSettings,
|
||||
this, &GitLabProjectSettings::save);
|
||||
}
|
||||
|
||||
void GitLabProjectSettings::setLinked(bool linked)
|
||||
{
|
||||
m_linked = linked;
|
||||
save();
|
||||
}
|
||||
|
||||
void GitLabProjectSettings::load()
|
||||
{
|
||||
m_id = Utils::Id::fromSetting(m_project->namedSettings(PSK_LINKED_ID));
|
||||
m_host = m_project->namedSettings(PSK_SERVER).toString();
|
||||
m_currentProject = m_project->namedSettings(PSK_PROJECT).toString();
|
||||
|
||||
// may still be wrong, but we avoid an additional request by just doing sanity check here
|
||||
if (!m_id.isValid() || m_host.isEmpty())
|
||||
m_linked = false;
|
||||
else
|
||||
m_linked = GitLabPlugin::globalParameters()->serverForId(m_id).id.isValid();
|
||||
}
|
||||
|
||||
void GitLabProjectSettings::save()
|
||||
{
|
||||
if (m_linked) {
|
||||
m_project->setNamedSettings(PSK_LINKED_ID, m_id.toSetting());
|
||||
m_project->setNamedSettings(PSK_SERVER, m_host);
|
||||
} else {
|
||||
m_project->setNamedSettings(PSK_LINKED_ID, Utils::Id().toSetting());
|
||||
m_project->setNamedSettings(PSK_SERVER, QString());
|
||||
}
|
||||
m_project->setNamedSettings(PSK_PROJECT, m_currentProject);
|
||||
}
|
||||
|
||||
GitLabProjectSettingsWidget::GitLabProjectSettingsWidget(ProjectExplorer::Project *project,
|
||||
QWidget *parent)
|
||||
: ProjectExplorer::ProjectSettingsWidget(parent)
|
||||
, m_projectSettings(GitLabPlugin::projectSettings(project))
|
||||
{
|
||||
setUseGlobalSettingsCheckBoxVisible(false);
|
||||
// setup ui
|
||||
auto verticalLayout = new QVBoxLayout(this);
|
||||
verticalLayout->setContentsMargins(0, 0, 0, 0);
|
||||
auto formLayout = new QFormLayout;
|
||||
m_hostCB = new QComboBox;
|
||||
formLayout->addRow(tr("Host:"), m_hostCB);
|
||||
m_linkedGitLabServer = new QComboBox;
|
||||
formLayout->addRow(tr("Linked GitLab Configuration:"), m_linkedGitLabServer);
|
||||
verticalLayout->addLayout(formLayout);
|
||||
m_infoLabel = new Utils::InfoLabel;
|
||||
m_infoLabel->setVisible(false);
|
||||
verticalLayout->addWidget(m_infoLabel);
|
||||
auto horizontalLayout = new QHBoxLayout;
|
||||
horizontalLayout->setContentsMargins(0, 0, 0, 0);
|
||||
m_linkWithGitLab = new QPushButton(tr("Link with GitLab"));
|
||||
horizontalLayout->addWidget(m_linkWithGitLab);
|
||||
m_unlink = new QPushButton(tr("Unlink from GitLab"));
|
||||
m_unlink->setEnabled(false);
|
||||
horizontalLayout->addWidget(m_unlink);
|
||||
m_checkConnection = new QPushButton(tr("Test Connection"));
|
||||
m_checkConnection->setEnabled(false);
|
||||
horizontalLayout->addWidget(m_checkConnection);
|
||||
horizontalLayout->addStretch(1);
|
||||
verticalLayout->addLayout(horizontalLayout);
|
||||
|
||||
connect(m_linkWithGitLab, &QPushButton::clicked, this, [this]() {
|
||||
checkConnection(Link);
|
||||
});
|
||||
connect(m_unlink, &QPushButton::clicked,
|
||||
this, &GitLabProjectSettingsWidget::unlink);
|
||||
connect(m_checkConnection, &QPushButton::clicked, this, [this]() {
|
||||
checkConnection(Connection);
|
||||
});
|
||||
connect(m_linkedGitLabServer, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, [this]() {
|
||||
m_infoLabel->setVisible(false);
|
||||
});
|
||||
connect(m_hostCB, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
||||
this, [this]() {
|
||||
m_infoLabel->setVisible(false);
|
||||
});
|
||||
connect(GitLabPlugin::optionsPage(), &GitLabOptionsPage::settingsChanged,
|
||||
this, &GitLabProjectSettingsWidget::updateUi);
|
||||
updateUi();
|
||||
}
|
||||
|
||||
void GitLabProjectSettingsWidget::unlink()
|
||||
{
|
||||
QTC_ASSERT(m_projectSettings->isLinked(), return);
|
||||
m_projectSettings->setLinked(false);
|
||||
m_projectSettings->setCurrentProject({});
|
||||
updateEnabledStates();
|
||||
}
|
||||
|
||||
void GitLabProjectSettingsWidget::checkConnection(CheckMode mode)
|
||||
{
|
||||
const GitLabServer server = m_linkedGitLabServer->currentData().value<GitLabServer>();
|
||||
const QString remote = m_hostCB->currentData().toString();
|
||||
|
||||
const auto [remoteHost, projName, port] = GitLabProjectSettings::remotePartsFromRemote(remote);
|
||||
if (remoteHost != server.host) { // port check as well
|
||||
m_infoLabel->setType(Utils::InfoLabel::NotOk);
|
||||
m_infoLabel->setText(tr("Remote host does not match chosen GitLab configuration."));
|
||||
m_infoLabel->setVisible(true);
|
||||
return;
|
||||
}
|
||||
|
||||
// temporarily disable ui
|
||||
m_linkedGitLabServer->setEnabled(false);
|
||||
m_hostCB->setEnabled(false);
|
||||
m_checkConnection->setEnabled(false);
|
||||
|
||||
m_checkMode = mode;
|
||||
const Query query(Query::Project, {projName});
|
||||
QueryRunner *runner = new QueryRunner(query, server.id, this);
|
||||
// can't use server, projName as captures inside the lambda below (bindings vs. local vars) :/
|
||||
const Utils::Id id = server.id;
|
||||
const QString projectName = projName;
|
||||
connect(runner, &QueryRunner::resultRetrieved, this,
|
||||
[this, id, remote, projectName](const QByteArray &result) {
|
||||
onConnectionChecked(ResultParser::parseProject(result), id, remote, projectName);
|
||||
});
|
||||
connect(runner, &QueryRunner::finished, this, [runner]() { runner->deleteLater(); });
|
||||
runner->start();
|
||||
}
|
||||
|
||||
void GitLabProjectSettingsWidget::onConnectionChecked(const Project &project,
|
||||
const Utils::Id &serverId,
|
||||
const QString &remote,
|
||||
const QString &projectName)
|
||||
{
|
||||
bool linkable = false;
|
||||
if (!project.error.message.isEmpty()) {
|
||||
m_infoLabel->setType(Utils::InfoLabel::Error);
|
||||
m_infoLabel->setText(project.error.message);
|
||||
} else {
|
||||
if (project.accessLevel != -1) {
|
||||
m_infoLabel->setType(Utils::InfoLabel::Ok);
|
||||
m_infoLabel->setText(tr("Accessible (%1)")
|
||||
.arg(accessLevelString(project.accessLevel)));
|
||||
linkable = true;
|
||||
} else {
|
||||
m_infoLabel->setType(Utils::InfoLabel::Warning);
|
||||
m_infoLabel->setText(tr("Read only access"));
|
||||
}
|
||||
}
|
||||
m_infoLabel->setVisible(true);
|
||||
|
||||
if (m_checkMode == Link && linkable) {
|
||||
m_projectSettings->setCurrentServer(serverId);
|
||||
m_projectSettings->setCurrentServerHost(remote);
|
||||
m_projectSettings->setLinked(true);
|
||||
m_projectSettings->setCurrentProject(projectName);
|
||||
}
|
||||
updateEnabledStates();
|
||||
}
|
||||
|
||||
void GitLabProjectSettingsWidget::updateUi()
|
||||
{
|
||||
m_linkedGitLabServer->clear();
|
||||
const QList<GitLabServer> allServers = GitLabPlugin::allGitLabServers();
|
||||
for (const GitLabServer &server : allServers) {
|
||||
const QString display = server.host + " (" + server.description + ')';
|
||||
m_linkedGitLabServer->addItem(display, QVariant::fromValue(server));
|
||||
}
|
||||
|
||||
const Utils::FilePath projectDirectory = m_projectSettings->project()->projectDirectory();
|
||||
const auto *gitClient = Git::Internal::GitClient::instance();
|
||||
const Utils::FilePath repository = gitClient
|
||||
? gitClient->findRepositoryForDirectory(projectDirectory) : Utils::FilePath();
|
||||
|
||||
m_hostCB->clear();
|
||||
if (!repository.isEmpty()) {
|
||||
const QMap<QString, QString> remotes = gitClient->synchronousRemotesList(repository);
|
||||
for (auto it = remotes.begin(), end = remotes.end(); it != end; ++it) {
|
||||
const QString display = it.key() + " (" + it.value() + ')';
|
||||
m_hostCB->addItem(display, QVariant::fromValue(it.value()));
|
||||
}
|
||||
}
|
||||
|
||||
const Utils::Id id = m_projectSettings->currentServer();
|
||||
const QString serverHost = m_projectSettings->currentServerHost();
|
||||
if (id.isValid()) {
|
||||
const GitLabServer server = GitLabPlugin::gitLabServerForId(id);
|
||||
auto [remoteHost, projName, port] = GitLabProjectSettings::remotePartsFromRemote(serverHost);
|
||||
if (server.id.isValid() && server.host == remoteHost) { // found config
|
||||
m_projectSettings->setLinked(true);
|
||||
m_hostCB->setCurrentIndex(m_hostCB->findData(QVariant::fromValue(serverHost)));
|
||||
m_linkedGitLabServer->setCurrentIndex(
|
||||
m_linkedGitLabServer->findData(QVariant::fromValue(server)));
|
||||
} else {
|
||||
m_projectSettings->setLinked(false);
|
||||
}
|
||||
}
|
||||
updateEnabledStates();
|
||||
}
|
||||
|
||||
void GitLabProjectSettingsWidget::updateEnabledStates()
|
||||
{
|
||||
const bool isGitRepository = m_hostCB->count() > 0;
|
||||
const bool hasGitLabServers = m_linkedGitLabServer->count();
|
||||
const bool linked = m_projectSettings->isLinked();
|
||||
m_linkedGitLabServer->setEnabled(isGitRepository && !linked);
|
||||
m_hostCB->setEnabled(isGitRepository && !linked);
|
||||
m_linkWithGitLab->setEnabled(isGitRepository && !linked && hasGitLabServers);
|
||||
m_unlink->setEnabled(isGitRepository && linked);
|
||||
m_checkConnection->setEnabled(isGitRepository && hasGitLabServers);
|
||||
if (!isGitRepository) {
|
||||
const Utils::FilePath projectDirectory = m_projectSettings->project()->projectDirectory();
|
||||
const auto *gitClient = Git::Internal::GitClient::instance();
|
||||
const Utils::FilePath repository = gitClient
|
||||
? gitClient->findRepositoryForDirectory(projectDirectory) : Utils::FilePath();
|
||||
if (repository.isEmpty())
|
||||
m_infoLabel->setText(tr("Not a git repository."));
|
||||
else
|
||||
m_infoLabel->setText(tr("Local git repository without remotes."));
|
||||
m_infoLabel->setType(Utils::InfoLabel::None);
|
||||
m_infoLabel->setVisible(true);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace GitLab
|
104
src/plugins/gitlab/gitlabprojectsettings.h
Normal file
104
src/plugins/gitlab/gitlabprojectsettings.h
Normal file
@@ -0,0 +1,104 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <projectexplorer/projectsettingswidget.h>
|
||||
#include <utils/id.h>
|
||||
|
||||
#include <QObject>
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QComboBox;
|
||||
class QPushButton;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace ProjectExplorer { class Project; }
|
||||
|
||||
namespace Utils { class InfoLabel; }
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
class Project;
|
||||
|
||||
class GitLabProjectSettings : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GitLabProjectSettings(ProjectExplorer::Project *project);
|
||||
Utils::Id currentServer() const { return m_id; }
|
||||
void setCurrentServer(const Utils::Id &id) { m_id = id; }
|
||||
QString currentServerHost() const { return m_host; }
|
||||
void setCurrentServerHost(const QString &server) { m_host = server; }
|
||||
void setCurrentProject(const QString &projectName) { m_currentProject = projectName; }
|
||||
QString currentProject() const { return m_currentProject; }
|
||||
bool isLinked() const { return m_linked; }
|
||||
void setLinked(bool linked);
|
||||
ProjectExplorer::Project *project() const { return m_project; }
|
||||
|
||||
static std::tuple<QString, QString, int> remotePartsFromRemote(const QString &remote);
|
||||
private:
|
||||
void load();
|
||||
void save();
|
||||
|
||||
ProjectExplorer::Project *m_project = nullptr;
|
||||
QString m_host;
|
||||
Utils::Id m_id;
|
||||
QString m_currentProject;
|
||||
bool m_linked = false;
|
||||
};
|
||||
|
||||
class GitLabProjectSettingsWidget : public ProjectExplorer::ProjectSettingsWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit GitLabProjectSettingsWidget(ProjectExplorer::Project *project,
|
||||
QWidget *parent = nullptr);
|
||||
|
||||
private:
|
||||
enum CheckMode { Connection, Link };
|
||||
|
||||
void unlink();
|
||||
void checkConnection(CheckMode mode);
|
||||
void onConnectionChecked(const Project &project, const Utils::Id &serverId,
|
||||
const QString &remote, const QString &projName);
|
||||
void updateUi();
|
||||
void updateEnabledStates();
|
||||
|
||||
GitLabProjectSettings *m_projectSettings = nullptr;
|
||||
QComboBox *m_linkedGitLabServer = nullptr;
|
||||
QComboBox *m_hostCB = nullptr;
|
||||
QPushButton *m_linkWithGitLab = nullptr;
|
||||
QPushButton *m_unlink = nullptr;
|
||||
QPushButton *m_checkConnection = nullptr;
|
||||
Utils::InfoLabel *m_infoLabel = nullptr;
|
||||
CheckMode m_checkMode = Connection;
|
||||
};
|
||||
|
||||
} // namespace GitLab
|
||||
|
133
src/plugins/gitlab/queryrunner.cpp
Normal file
133
src/plugins/gitlab/queryrunner.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "queryrunner.h"
|
||||
|
||||
#include "gitlabparameters.h"
|
||||
#include "gitlabplugin.h"
|
||||
|
||||
#include <coreplugin/progressmanager/futureprogress.h>
|
||||
#include <coreplugin/progressmanager/progressmanager.h>
|
||||
#include <utils/algorithm.h>
|
||||
#include <utils/commandline.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <vcsbase/vcsoutputwindow.h>
|
||||
|
||||
#include <QUrl>
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
const char API_PREFIX[] = "/api/v4";
|
||||
const char QUERY_PROJECT[] = "/projects/%1";
|
||||
|
||||
Query::Query(Type type, const QStringList ¶meter)
|
||||
: m_type(type)
|
||||
, m_parameter(parameter)
|
||||
{
|
||||
}
|
||||
|
||||
QString Query::toString() const
|
||||
{
|
||||
QString query = API_PREFIX;
|
||||
switch (m_type) {
|
||||
case Query::NoQuery:
|
||||
return QString();
|
||||
case Query::Project:
|
||||
QTC_ASSERT(!m_parameter.isEmpty(), return {});
|
||||
query += QLatin1String(QUERY_PROJECT).arg(QLatin1String(
|
||||
QUrl::toPercentEncoding(m_parameter.at(0))));
|
||||
break;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
QueryRunner::QueryRunner(const Query &query, const Utils::Id &id, QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
const GitLabParameters *p = GitLabPlugin::globalParameters();
|
||||
const auto server = p->serverForId(id);
|
||||
QStringList args = server.curlArguments();
|
||||
if (!server.token.isEmpty())
|
||||
args << "--header" << "PRIVATE-TOKEN: " + server.token;
|
||||
QString url = "https://" + server.host;
|
||||
if (server.port != GitLabServer::defaultPort)
|
||||
url.append(':' + QString::number(server.port));
|
||||
url += query.toString();
|
||||
args << url;
|
||||
m_process.setCommand({p->curl, args});
|
||||
connect(&m_process, &Utils::QtcProcess::finished, this, &QueryRunner::processFinished);
|
||||
connect(&m_process, &Utils::QtcProcess::errorOccurred, this, &QueryRunner::processError);
|
||||
}
|
||||
|
||||
QueryRunner::~QueryRunner()
|
||||
{
|
||||
m_process.disconnect();
|
||||
terminate();
|
||||
}
|
||||
|
||||
void QueryRunner::start()
|
||||
{
|
||||
QTC_ASSERT(!m_running, return);
|
||||
m_running = true;
|
||||
m_process.start();
|
||||
}
|
||||
|
||||
void QueryRunner::terminate()
|
||||
{
|
||||
m_process.stopProcess();
|
||||
}
|
||||
|
||||
void QueryRunner::errorTermination(const QString &msg)
|
||||
{
|
||||
if (!m_running)
|
||||
return;
|
||||
VcsBase::VcsOutputWindow::appendError(msg);
|
||||
m_running = false;
|
||||
emit finished();
|
||||
}
|
||||
|
||||
void QueryRunner::processError(QProcess::ProcessError /*error*/)
|
||||
{
|
||||
const QString msg = tr("Error running %1: %2")
|
||||
.arg(m_process.commandLine().executable().toUserOutput(),
|
||||
m_process.errorString());
|
||||
errorTermination(msg);
|
||||
}
|
||||
void QueryRunner::processFinished()
|
||||
{
|
||||
const QString executable = m_process.commandLine().executable().toUserOutput();
|
||||
if (m_process.exitStatus() != QProcess::NormalExit) {
|
||||
errorTermination(tr("%1 crashed.").arg(executable));
|
||||
return;
|
||||
} else if (m_process.exitCode()) {
|
||||
errorTermination(tr("%1 returned %2.").arg(executable).arg(m_process.exitCode()));
|
||||
return;
|
||||
}
|
||||
m_running = false;
|
||||
emit resultRetrieved(m_process.readAllStandardOutput());
|
||||
emit finished();
|
||||
}
|
||||
|
||||
} // namespace GitLab
|
75
src/plugins/gitlab/queryrunner.h
Normal file
75
src/plugins/gitlab/queryrunner.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utils/qtcprocess.h>
|
||||
|
||||
#include <QObject>
|
||||
|
||||
namespace Utils { class Id; }
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
class Query
|
||||
{
|
||||
public:
|
||||
enum Type {
|
||||
NoQuery,
|
||||
Project
|
||||
};
|
||||
|
||||
explicit Query(Type type, const QStringList ¶meters = {});
|
||||
QString toString() const;
|
||||
|
||||
private:
|
||||
Type m_type = NoQuery;
|
||||
QStringList m_parameter;
|
||||
};
|
||||
|
||||
class QueryRunner : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QueryRunner(const Query &query, const Utils::Id &id, QObject *parent = nullptr);
|
||||
~QueryRunner();
|
||||
|
||||
void start();
|
||||
void terminate();
|
||||
|
||||
signals:
|
||||
void finished();
|
||||
void resultRetrieved(const QByteArray &json);
|
||||
|
||||
private:
|
||||
void errorTermination(const QString &msg);
|
||||
void processError(QProcess::ProcessError error);
|
||||
void processFinished();
|
||||
|
||||
Utils::QtcProcess m_process;
|
||||
bool m_running = false;
|
||||
};
|
||||
|
||||
} // namespace GitLab
|
109
src/plugins/gitlab/resultparser.cpp
Normal file
109
src/plugins/gitlab/resultparser.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "resultparser.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace GitLab {
|
||||
namespace ResultParser {
|
||||
|
||||
static std::pair<Error, QJsonObject> preHandleSingle(const QByteArray &json)
|
||||
{
|
||||
Error result;
|
||||
QJsonObject object;
|
||||
QJsonParseError error;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(json, &error);
|
||||
|
||||
if (error.error != QJsonParseError::NoError) {
|
||||
result.message = error.errorString();
|
||||
} else if (!doc.isObject()) {
|
||||
result.message = "Not an Object";
|
||||
} else {
|
||||
object = doc.object();
|
||||
if (object.contains("message")) {
|
||||
result = parseErrorMessage(object.value("message").toString());
|
||||
} else if (object.contains("error")) {
|
||||
if (object.value("error").toString() == "insufficient_scope")
|
||||
result.code = 1;
|
||||
result.message = object.value("error_description").toString();
|
||||
}
|
||||
}
|
||||
|
||||
return std::make_pair(result, object);
|
||||
}
|
||||
|
||||
static Project projectFromJson(const QJsonObject &jsonObj)
|
||||
{
|
||||
Project project;
|
||||
project.name = jsonObj.value("name").toString();
|
||||
project.displayName = jsonObj.value("name_with_namespace").toString();
|
||||
project.pathName = jsonObj.value("path_with_namespace").toString();
|
||||
project.id = jsonObj.value("id").toInt(-1);
|
||||
project.visibility = jsonObj.value("visibility").toString("public");
|
||||
if (jsonObj.contains("forks_count"))
|
||||
project.forkCount = jsonObj.value("forks_count").toInt();
|
||||
if (jsonObj.contains("star_count"))
|
||||
project.starCount = jsonObj.value("star_count").toInt();
|
||||
if (jsonObj.contains("open_issues_count"))
|
||||
project.issuesCount = jsonObj.value("open_issues_count").toInt();
|
||||
const QJsonObject permissions = jsonObj.value("permissions").toObject();
|
||||
if (!permissions.isEmpty()) { // separate permissions obj?
|
||||
const QJsonObject projAccObj = permissions.value("project_access").toObject();
|
||||
if (!projAccObj.isEmpty())
|
||||
project.accessLevel = projAccObj.value("access_level").toInt(-1);
|
||||
}
|
||||
return project;
|
||||
}
|
||||
|
||||
Project parseProject(const QByteArray &input)
|
||||
{
|
||||
auto [error, projectObj] = preHandleSingle(input);
|
||||
if (!error.message.isEmpty()) {
|
||||
Project result;
|
||||
result.error = error;
|
||||
return result;
|
||||
}
|
||||
return projectFromJson(projectObj);
|
||||
}
|
||||
|
||||
Error parseErrorMessage(const QString &message)
|
||||
{
|
||||
Error error;
|
||||
bool ok = false;
|
||||
error.code = message.left(3).toInt(&ok);
|
||||
if (ok)
|
||||
error.message = message.mid(4);
|
||||
else
|
||||
error.message = "Internal Parse Error";
|
||||
return error;
|
||||
}
|
||||
|
||||
} // namespace ResultParser
|
||||
} // namespace GitLab
|
59
src/plugins/gitlab/resultparser.h
Normal file
59
src/plugins/gitlab/resultparser.h
Normal file
@@ -0,0 +1,59 @@
|
||||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2022 The Qt Company Ltd.
|
||||
** Contact: https://www.qt.io/licensing/
|
||||
**
|
||||
** This file is part of Qt Creator.
|
||||
**
|
||||
** Commercial License Usage
|
||||
** Licensees holding valid commercial Qt licenses may use this file in
|
||||
** accordance with the commercial license agreement provided with the
|
||||
** Software or, alternatively, in accordance with the terms contained in
|
||||
** a written agreement between you and The Qt Company. For licensing terms
|
||||
** and conditions see https://www.qt.io/terms-conditions. For further
|
||||
** information use the contact form at https://www.qt.io/contact-us.
|
||||
**
|
||||
** GNU General Public License Usage
|
||||
** Alternatively, this file may be used under the terms of the GNU
|
||||
** General Public License version 3 as published by the Free Software
|
||||
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
||||
** included in the packaging of this file. Please review the following
|
||||
** information to ensure the GNU General Public License requirements will
|
||||
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace GitLab {
|
||||
|
||||
struct Error
|
||||
{
|
||||
int code = 200; // below 200: internal, 200+ HTTP
|
||||
QString message;
|
||||
};
|
||||
|
||||
class Project
|
||||
{
|
||||
public:
|
||||
QString name;
|
||||
QString displayName;
|
||||
QString pathName;
|
||||
QString visibility;
|
||||
Error error;
|
||||
int id = -1;
|
||||
int starCount = -1;
|
||||
int forkCount = -1;
|
||||
int issuesCount = -1;
|
||||
int accessLevel = -1; // 40 maintainer, 30 developer, 20 reporter, 10 guest
|
||||
};
|
||||
|
||||
namespace ResultParser {
|
||||
|
||||
Project parseProject(const QByteArray &input);
|
||||
Error parseErrorMessage(const QString &message);
|
||||
|
||||
} // namespace ResultParser
|
||||
} // namespace GitLab
|
@@ -40,6 +40,7 @@ Project {
|
||||
"emacskeys/emacskeys.qbs",
|
||||
"genericprojectmanager/genericprojectmanager.qbs",
|
||||
"git/git.qbs",
|
||||
"gitlab/gitlab.qbs",
|
||||
"glsleditor/glsleditor.qbs",
|
||||
"helloworld/helloworld.qbs",
|
||||
"help/help.qbs",
|
||||
|
Reference in New Issue
Block a user