1/*
2 SPDX-FileCopyrightText: 2017 René J.V. Bertin <rjvbertin@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <QFileInfo>
8#include <QJsonArray>
9#include <QStandardPaths>
10#include <QUrl>
11
12#include <KLocalizedString>
13#include <KPluginFactory>
14
15#include "debug.h"
16#include "phabricatorjobs.h"
17
18#include "purpose/job.h"
19#include "purpose/pluginbase.h"
20
21class PhabricatorJob : public Purpose::Job
22{
23 Q_OBJECT
24public:
25 PhabricatorJob(QObject *object = nullptr)
26 : Purpose::Job(object)
27 {
28 }
29
30 void start() override
31 {
32 const QString localBaseDir(data().value(key: QLatin1String("localBaseDir")).toString());
33 const QUrl sourceFile(data().value(key: QLatin1String("urls")).toArray().first().toString());
34 const QString updateDR = data().value(key: QLatin1String("updateDR")).toString();
35 const bool doBrowse = data().value(key: QLatin1String("doBrowse")).toBool();
36
37 const QString baseDir = QUrl(localBaseDir).toLocalFile();
38
39 if (QFileInfo(sourceFile.toLocalFile()).size() <= 0) {
40 setError(KJob::UserDefinedError + 1);
41 setErrorText(i18n("Phabricator refuses empty patchfiles"));
42 Q_EMIT PhabricatorJob::warning(job: this, message: errorString());
43 qCCritical(PLUGIN_PHABRICATOR) << errorString();
44 emitResult();
45 return;
46 } else if (updateDR.localeAwareCompare(i18n("unknown")) == 0) {
47 setError(KJob::UserDefinedError + 1);
48 setErrorText(i18n("Please choose between creating a new revision or updating an existing one"));
49 Q_EMIT PhabricatorJob::warning(job: this, message: errorString());
50 qCCritical(PLUGIN_PHABRICATOR) << errorString();
51 emitResult();
52 return;
53 }
54
55 m_drTitle = data().value(key: QLatin1String("drTitle")).toString();
56
57 KJob *job;
58 if (!updateDR.isEmpty()) {
59 const QString updateComment = data().value(key: QLatin1String("updateComment")).toString();
60 job = new Phabricator::UpdateDiffRev(sourceFile, baseDir, updateDR, updateComment, doBrowse, this);
61 connect(sender: job, signal: &KJob::finished, context: this, slot: &PhabricatorJob::diffUpdated);
62 } else {
63 job = new Phabricator::NewDiffRev(sourceFile, baseDir, true, this);
64 connect(sender: job, signal: &KJob::finished, context: this, slot: &PhabricatorJob::diffCreated);
65 }
66 job->start();
67 Q_EMIT PhabricatorJob::infoMessage(job: this, QStringLiteral("upload job started"));
68 }
69
70 void diffCreatedOrUpdated(KJob *j, bool created)
71 {
72 if (j->error() != 0) {
73 setError(j->error());
74 setErrorText(j->errorString());
75 Q_EMIT PhabricatorJob::warning(job: this, message: j->errorString());
76 qCCritical(PLUGIN_PHABRICATOR) << "Could not upload the patch" << j->errorString();
77 emitResult();
78 return;
79 }
80
81 if (created) {
82 Phabricator::NewDiffRev const *job = qobject_cast<Phabricator::NewDiffRev *>(object: j);
83 qCWarning(PLUGIN_PHABRICATOR) << "new diff:" << job->diffURI();
84 setOutput({{QStringLiteral("url"), job->diffURI()}});
85 } else {
86 Phabricator::UpdateDiffRev const *job = qobject_cast<Phabricator::UpdateDiffRev *>(object: j);
87 qCWarning(PLUGIN_PHABRICATOR) << "updated diff" << job->requestId() << ":" << job->diffURI();
88 setOutput({{QStringLiteral("url"), job->diffURI()}});
89 Q_EMIT PhabricatorJob::infoMessage(job: this, QStringLiteral("updated diff %1: %2").arg(a: job->requestId()).arg(a: job->diffURI()));
90 }
91 emitResult();
92 }
93
94 void diffCreated(KJob *j)
95 {
96 diffCreatedOrUpdated(j, created: true);
97 }
98
99 void diffUpdated(KJob *j)
100 {
101 diffCreatedOrUpdated(j, created: false);
102 }
103
104 QString m_drTitle;
105};
106
107class PhabricatorPlugin : public Purpose::PluginBase
108{
109 Q_OBJECT
110public:
111 using PluginBase::PluginBase;
112 Purpose::Job *createJob() const override
113 {
114 return new PhabricatorJob;
115 }
116};
117
118K_PLUGIN_CLASS_WITH_JSON(PhabricatorPlugin, "phabricatorplugin.json")
119
120#include "phabricatorplugin.moc"
121

source code of purpose/src/plugins/phabricator/phabricatorplugin.cpp