GitLab: Allow browsing and cloning projects

Change-Id: I1cc877ea6b5a55ae7bdb8e7a529afeb08d09e0c0
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Stenger
2022-05-06 15:15:46 +02:00
parent b336ebafc3
commit dcfa15ff17
14 changed files with 1228 additions and 2 deletions

View File

@@ -34,6 +34,38 @@
namespace GitLab {
namespace ResultParser {
static PageInformation paginationInformation(const QByteArray &header)
{
PageInformation result;
const QByteArrayList lines = header.split('\n');
for (const QByteArray &line : lines) {
const QByteArray lower = line.toLower(); // depending on OS this may be capitalized
if (lower.startsWith("x-page: "))
result.currentPage = line.mid(8).toInt();
else if (lower.startsWith("x-per-page: "))
result.perPage = line.mid(12).toInt();
else if (lower.startsWith("x-total: "))
result.total = line.mid(9).toInt();
else if (lower.startsWith("x-total-pages: "))
result.totalPages = line.mid(15).toInt();
}
return result;
}
static std::pair<QByteArray, QByteArray> splitHeaderAndBody(const QByteArray &input)
{
QByteArray header;
QByteArray json;
int emptyLine = input.indexOf("\r\n\r\n"); // we always get \r\n as line separator?
if (emptyLine != -1) {
header = input.left(emptyLine);
json = input.mid(emptyLine + 4);
} else {
json = input;
}
return std::make_pair(header, json);
}
static std::pair<Error, QJsonObject> preHandleSingle(const QByteArray &json)
{
Error result;
@@ -59,6 +91,52 @@ static std::pair<Error, QJsonObject> preHandleSingle(const QByteArray &json)
return std::make_pair(result, object);
}
static std::pair<Error, QJsonDocument> preHandleHeaderAndBody(const QByteArray &header,
const QByteArray &json)
{
Error result;
if (header.isEmpty()) {
result.message = "Missing Expected Header";
return std::make_pair(result, QJsonDocument());
}
QJsonParseError error;
const QJsonDocument doc = QJsonDocument::fromJson(json, &error);
if (error.error != QJsonParseError::NoError) {
result.message = error.errorString();
return std::make_pair(result, doc);
}
if (doc.isObject()) {
const QJsonObject obj = doc.object();
if (obj.contains("message")) {
result = parseErrorMessage(obj.value("message").toString());
return std::make_pair(result, doc);
} else if (obj.contains("error")) {
if (obj.value("error").toString() == "insufficient_scope")
result.code = 1;
result.message = obj.value("error_description").toString();
return std::make_pair(result, doc);
}
}
if (!doc.isArray())
result.message = "Not an Array";
return std::make_pair(result, doc);
}
static User userFromJson(const QJsonObject &jsonObj)
{
User user;
user.name = jsonObj.value("username").toString();
user.realname = jsonObj.value("name").toString();
user.id = jsonObj.value("id").toInt(-1);
user.email = jsonObj.value("email").toString();
user.bot = jsonObj.value("bot").toBool();
return user;
}
static Project projectFromJson(const QJsonObject &jsonObj)
{
Project project;
@@ -67,6 +145,8 @@ static Project projectFromJson(const QJsonObject &jsonObj)
project.pathName = jsonObj.value("path_with_namespace").toString();
project.id = jsonObj.value("id").toInt(-1);
project.visibility = jsonObj.value("visibility").toString("public");
project.httpUrl = jsonObj.value("http_url_to_repo").toString();
project.sshUrl = jsonObj.value("ssh_url_to_repo").toString();
if (jsonObj.contains("forks_count"))
project.forkCount = jsonObj.value("forks_count").toInt();
if (jsonObj.contains("star_count"))
@@ -82,6 +162,17 @@ static Project projectFromJson(const QJsonObject &jsonObj)
return project;
}
User parseUser(const QByteArray &input)
{
auto [error, userObj] = preHandleSingle(input);
if (!error.message.isEmpty()) {
User result;
result.error = error;
return result;
}
return userFromJson(userObj);
}
Project parseProject(const QByteArray &input)
{
auto [error, projectObj] = preHandleSingle(input);
@@ -93,6 +184,26 @@ Project parseProject(const QByteArray &input)
return projectFromJson(projectObj);
}
Projects parseProjects(const QByteArray &input)
{
auto [header, json] = splitHeaderAndBody(input);
auto [error, jsonDoc] = preHandleHeaderAndBody(header, json);
Projects result;
if (!error.message.isEmpty()) {
result.error = error;
return result;
}
result.pageInfo = paginationInformation(header);
const QJsonArray projectsArray = jsonDoc.array();
for (const QJsonValue &value : projectsArray) {
if (!value.isObject())
continue;
const QJsonObject projectObj = value.toObject();
result.projects.append(projectFromJson(projectObj));
}
return result;
}
Error parseErrorMessage(const QString &message)
{
Error error;