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 | |
18 | class TheReviewboardJob : public Purpose::Job |
19 | { |
20 | Q_OBJECT |
21 | public: |
22 | TheReviewboardJob(QObject *object = nullptr) |
23 | : Purpose::Job(object) |
24 | { |
25 | } |
26 | |
27 | void start() override |
28 | { |
29 | const QString baseDir(data().value(key: QLatin1String("baseDir" )).toString()); |
30 | const QString repository(data().value(key: QLatin1String("repository" )).toString()); |
31 | const QUrl sourceFile(data().value(key: QLatin1String("urls" )).toArray().first().toString()); |
32 | const QString updateRR = data().value(key: QLatin1String("updateRR" )).toString(); |
33 | const QJsonObject = data().value(key: QLatin1String("extraData" )).toObject(); |
34 | const QString username = data().value(key: QLatin1String("username" )).toString(); |
35 | const QString password = data().value(key: QLatin1String("password" )).toString(); |
36 | QUrl server(data().value(key: QLatin1String("server" )).toString()); |
37 | server.setUserInfo(userInfo: username + QLatin1Char(':') + password); |
38 | |
39 | KJob *job; |
40 | if (!updateRR.isEmpty()) { |
41 | job = new ReviewBoard::SubmitPatchRequest(server, sourceFile, baseDir, updateRR); |
42 | connect(sender: job, signal: &KJob::finished, context: this, slot: &TheReviewboardJob::reviewDone); |
43 | } else { |
44 | job = new ReviewBoard::NewRequest(server, repository); |
45 | job->setProperty(name: "extraData" , value: extraData); |
46 | connect(sender: job, signal: &KJob::finished, context: this, slot: &TheReviewboardJob::reviewCreated); |
47 | } |
48 | job->setProperty(name: "baseDir" , value: baseDir); |
49 | job->start(); |
50 | } |
51 | |
52 | void reviewCreated(KJob *j) |
53 | { |
54 | if (j->error() != 0) { |
55 | setError(j->error()); |
56 | setErrorText(j->errorString()); |
57 | emitResult(); |
58 | return; |
59 | } |
60 | |
61 | ReviewBoard::NewRequest const *job = qobject_cast<ReviewBoard::NewRequest *>(object: j); |
62 | |
63 | // This will provide things like groups and users for review from .reviewboardrc |
64 | QVariantMap = job->property(name: "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(name: "baseDir" ).toString(), job->requestId()); |
74 | connect(sender: submitPatchJob, signal: &ReviewBoard::SubmitPatchRequest::finished, context: this, slot: &TheReviewboardJob::reviewDone); |
75 | submitPatchJob->start(); |
76 | } |
77 | |
78 | void reviewDone(KJob *j) |
79 | { |
80 | if (j->error() != 0) { |
81 | setError(j->error()); |
82 | setErrorText(j->errorString()); |
83 | emitResult(); |
84 | return; |
85 | } |
86 | |
87 | ReviewBoard::SubmitPatchRequest const *job = qobject_cast<ReviewBoard::SubmitPatchRequest *>(object: j); |
88 | QUrl url = job->server(); |
89 | url.setUserInfo(userInfo: QString()); |
90 | QString requrl = QStringLiteral("%1/r/%2/" ).arg(args: url.toDisplayString(options: QUrl::PreferLocalFile), args: job->requestId()); |
91 | setOutput({{QStringLiteral("url" ), requrl}}); |
92 | emitResult(); |
93 | } |
94 | }; |
95 | |
96 | class ReviewBoardPlugin : public Purpose::PluginBase |
97 | { |
98 | Q_OBJECT |
99 | public: |
100 | using PluginBase::PluginBase; |
101 | Purpose::Job *createJob() const override |
102 | { |
103 | return new TheReviewboardJob; |
104 | } |
105 | }; |
106 | |
107 | K_PLUGIN_CLASS_WITH_JSON(ReviewBoardPlugin, "reviewboardplugin.json" ) |
108 | |
109 | #include "reviewboardplugin.moc" |
110 | |