File indexing completed on 2024-05-19 04:03:03

0001 /*
0002     SPDX-FileCopyrightText: 2017 René J.V. Bertin <rjvbertin@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include <QFileInfo>
0008 #include <QJsonArray>
0009 #include <QStandardPaths>
0010 #include <QUrl>
0011 
0012 #include <KLocalizedString>
0013 #include <KPluginFactory>
0014 
0015 #include "debug.h"
0016 #include "phabricatorjobs.h"
0017 
0018 #include "purpose/job.h"
0019 #include "purpose/pluginbase.h"
0020 
0021 class PhabricatorJob : public Purpose::Job
0022 {
0023     Q_OBJECT
0024 public:
0025     PhabricatorJob(QObject *object = nullptr)
0026         : Purpose::Job(object)
0027     {
0028     }
0029 
0030     void start() override
0031     {
0032         const QString localBaseDir(data().value(QLatin1String("localBaseDir")).toString());
0033         const QUrl sourceFile(data().value(QLatin1String("urls")).toArray().first().toString());
0034         const QString updateDR = data().value(QLatin1String("updateDR")).toString();
0035         const bool doBrowse = data().value(QLatin1String("doBrowse")).toBool();
0036 
0037         const QString baseDir = QUrl(localBaseDir).toLocalFile();
0038 
0039         if (QFileInfo(sourceFile.toLocalFile()).size() <= 0) {
0040             setError(KJob::UserDefinedError + 1);
0041             setErrorText(i18n("Phabricator refuses empty patchfiles"));
0042             Q_EMIT PhabricatorJob::warning(this, errorString());
0043             qCCritical(PLUGIN_PHABRICATOR) << errorString();
0044             emitResult();
0045             return;
0046         } else if (updateDR.localeAwareCompare(i18n("unknown")) == 0) {
0047             setError(KJob::UserDefinedError + 1);
0048             setErrorText(i18n("Please choose between creating a new revision or updating an existing one"));
0049             Q_EMIT PhabricatorJob::warning(this, errorString());
0050             qCCritical(PLUGIN_PHABRICATOR) << errorString();
0051             emitResult();
0052             return;
0053         }
0054 
0055         m_drTitle = data().value(QLatin1String("drTitle")).toString();
0056 
0057         KJob *job;
0058         if (!updateDR.isEmpty()) {
0059             const QString updateComment = data().value(QLatin1String("updateComment")).toString();
0060             job = new Phabricator::UpdateDiffRev(sourceFile, baseDir, updateDR, updateComment, doBrowse, this);
0061             connect(job, &KJob::finished, this, &PhabricatorJob::diffUpdated);
0062         } else {
0063             job = new Phabricator::NewDiffRev(sourceFile, baseDir, true, this);
0064             connect(job, &KJob::finished, this, &PhabricatorJob::diffCreated);
0065         }
0066         job->start();
0067         Q_EMIT PhabricatorJob::infoMessage(this, QStringLiteral("upload job started"));
0068     }
0069 
0070     void diffCreatedOrUpdated(KJob *j, bool created)
0071     {
0072         if (j->error() != 0) {
0073             setError(j->error());
0074             setErrorText(j->errorString());
0075             Q_EMIT PhabricatorJob::warning(this, j->errorString());
0076             qCCritical(PLUGIN_PHABRICATOR) << "Could not upload the patch" << j->errorString();
0077             emitResult();
0078             return;
0079         }
0080 
0081         if (created) {
0082             Phabricator::NewDiffRev const *job = qobject_cast<Phabricator::NewDiffRev *>(j);
0083             qCWarning(PLUGIN_PHABRICATOR) << "new diff:" << job->diffURI();
0084             setOutput({{QStringLiteral("url"), job->diffURI()}});
0085         } else {
0086             Phabricator::UpdateDiffRev const *job = qobject_cast<Phabricator::UpdateDiffRev *>(j);
0087             qCWarning(PLUGIN_PHABRICATOR) << "updated diff" << job->requestId() << ":" << job->diffURI();
0088             setOutput({{QStringLiteral("url"), job->diffURI()}});
0089             Q_EMIT PhabricatorJob::infoMessage(this, QStringLiteral("updated diff %1: %2").arg(job->requestId()).arg(job->diffURI()));
0090         }
0091         emitResult();
0092     }
0093 
0094     void diffCreated(KJob *j)
0095     {
0096         diffCreatedOrUpdated(j, true);
0097     }
0098 
0099     void diffUpdated(KJob *j)
0100     {
0101         diffCreatedOrUpdated(j, false);
0102     }
0103 
0104     QString m_drTitle;
0105 };
0106 
0107 class PhabricatorPlugin : public Purpose::PluginBase
0108 {
0109     Q_OBJECT
0110 public:
0111     using PluginBase::PluginBase;
0112     Purpose::Job *createJob() const override
0113     {
0114         return new PhabricatorJob;
0115     }
0116 };
0117 
0118 K_PLUGIN_CLASS_WITH_JSON(PhabricatorPlugin, "phabricatorplugin.json")
0119 
0120 #include "phabricatorplugin.moc"