Purpose

reviewboardplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Aleix Pol Gonzalez <aleixpol@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <QJsonArray>
8#include <QStandardPaths>
9#include <QUrl>
10
11#include <KPluginFactory>
12
13#include "reviewboardjobs.h"
14
15#include "purpose/job.h"
16#include "purpose/pluginbase.h"
17
18class TheReviewboardJob : public Purpose::Job
19{
21public:
22 TheReviewboardJob(QObject *object = nullptr)
23 : Purpose::Job(object)
24 {
25 }
26
27 void start() override
28 {
29 const QString baseDir(data().value(QLatin1String("baseDir")).toString());
30 const QString repository(data().value(QLatin1String("repository")).toString());
31 const QUrl sourceFile(data().value(QLatin1String("urls")).toArray().first().toString());
32 const QString updateRR = data().value(QLatin1String("updateRR")).toString();
33 const QJsonObject extraData = data().value(QLatin1String("extraData")).toObject();
34 const QString username = data().value(QLatin1String("username")).toString();
35 const QString password = data().value(QLatin1String("password")).toString();
36 QUrl server(data().value(QLatin1String("server")).toString());
37 server.setUserInfo(username + QLatin1Char(':') + password);
38
39 KJob *job;
40 if (!updateRR.isEmpty()) {
41 job = new ReviewBoard::SubmitPatchRequest(server, sourceFile, baseDir, updateRR);
42 connect(job, &KJob::finished, this, &TheReviewboardJob::reviewDone);
43 } else {
44 job = new ReviewBoard::NewRequest(server, repository);
45 job->setProperty("extraData", extraData);
46 connect(job, &KJob::finished, this, &TheReviewboardJob::reviewCreated);
47 }
48 job->setProperty("baseDir", baseDir);
49 job->start();
50 }
51
52 void reviewCreated(KJob *j)
53 {
54 if (j->error() != 0) {
55 setError(j->error());
57 emitResult();
58 return;
59 }
60
61 ReviewBoard::NewRequest const *job = qobject_cast<ReviewBoard::NewRequest *>(j);
62
63 // This will provide things like groups and users for review from .reviewboardrc
64 QVariantMap extraData = job->property("extraData").toMap();
65 if (!extraData.isEmpty()) {
66 KJob *updateJob = new ReviewBoard::UpdateRequest(job->server(), job->requestId(), extraData);
67 updateJob->start();
68 }
69
70 // for git projects, m_source will be a VCSDiffPatchSource instance
71 const QUrl sourceFile(data().value(QStringLiteral("urls")).toArray().first().toString());
72 ReviewBoard::SubmitPatchRequest *submitPatchJob =
73 new ReviewBoard::SubmitPatchRequest(job->server(), sourceFile, j->property("baseDir").toString(), job->requestId());
74 connect(submitPatchJob, &ReviewBoard::SubmitPatchRequest::finished, this, &TheReviewboardJob::reviewDone);
75 submitPatchJob->start();
76 }
77
78 void reviewDone(KJob *j)
79 {
80 if (j->error() != 0) {
81 setError(j->error());
83 emitResult();
84 return;
85 }
86
87 ReviewBoard::SubmitPatchRequest const *job = qobject_cast<ReviewBoard::SubmitPatchRequest *>(j);
88 QUrl url = job->server();
89 url.setUserInfo(QString());
90 QString requrl = QStringLiteral("%1/r/%2/").arg(url.toDisplayString(QUrl::PreferLocalFile), job->requestId());
91 setOutput({{QStringLiteral("url"), requrl}});
92 emitResult();
93 }
94};
95
96class ReviewBoardPlugin : public Purpose::PluginBase
97{
99public:
100 using PluginBase::PluginBase;
101 Purpose::Job *createJob() const override
102 {
103 return new TheReviewboardJob;
104 }
105};
106
107K_PLUGIN_CLASS_WITH_JSON(ReviewBoardPlugin, "reviewboardplugin.json")
108
109#include "reviewboardplugin.moc"
void setErrorText(const QString &errorText)
virtual QString errorString() const
void emitResult()
int error() const
void finished(KJob *job)
void setError(int errorCode)
virtual Q_SCRIPTABLE void start()=0
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
Base class to implement by plugins.
Definition pluginbase.h:29
char * toString(const EngineQuery &query)
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() const const
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QVariant property(const char *name) const const
T qobject_cast(QObject *object)
bool setProperty(const char *name, QVariant &&value)
QString arg(Args &&... args) const const
bool isEmpty() const const
PreferLocalFile
void setUserInfo(const QString &userInfo, ParsingMode mode)
QString toDisplayString(FormattingOptions options) const const
QMap< QString, QVariant > toMap() const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.