1#pragma once
2
3/****************************************************************************************
4 ** GitQlient is an application to manage and operate one or several Git repositories. With
5 ** GitQlient you will be able to add commits, branches and manage all the options Git provides.
6 ** Copyright (C) 2021 Francesc Martinez
7 **
8 ** LinkedIn: www.linkedin.com/in/cescmm/
9 ** Web: www.francescmm.com
10 **
11 ** This program is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU Lesser General Public
13 ** License as published by the Free Software Foundation; either
14 ** version 2 of the License, or (at your option) any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** Lesser General Public License for more details.
20 **
21 ** You should have received a copy of the GNU Lesser General Public
22 ** License along with this library; if not, write to the Free Software
23 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 ***************************************************************************************/
25
26#include <Milestone.h>
27#include <Label.h>
28#include <PullRequest.h>
29
30#include <QObject>
31#include <QMap>
32#include <QNetworkRequest>
33
34class QNetworkAccessManager;
35class QNetworkReply;
36
37namespace GitServer
38{
39
40struct Issue;
41
42struct ServerAuthentication
43{
44 QString userName;
45 QString userPass;
46 QString endpointUrl;
47};
48
49class IRestApi : public QObject
50{
51 Q_OBJECT
52
53signals:
54 /**
55 * @brief connectionTested Signal triggered when the connection to the remote Git server succeeds.
56 */
57 void connectionTested();
58 /**
59 * @brief labelsReceived Signal triggered after the labels are received and processed.
60 * @param labels The processed labels.
61 */
62 void labelsReceived(const QVector<GitServer::Label> &labels);
63 /**
64 * @brief milestonesReceived Signal triggered after the milestones are received and processed.
65 * @param milestones The processed milestones.
66 */
67 void milestonesReceived(const QVector<GitServer::Milestone> &milestones);
68
69 /**
70 * @brief issuesReceived Signal triggered when the issues has been received.
71 * @param issues The list of issues.
72 */
73 void issuesReceived(const QVector<GitServer::Issue> &issues);
74
75 /**
76 * @brief pullRequestsReceived Signal triggered when the pull requests has been received.
77 * @param prs The list of prs.
78 */
79 void pullRequestsReceived(const QVector<GitServer::PullRequest> &prs);
80
81 /**
82 * @brief pullRequestMerged Signal triggered when the pull request has been merged.
83 */
84 void pullRequestMerged();
85
86 /**
87 * @brief errorOccurred Signal triggered when an error happened.
88 * @param errorStr The error in string format.
89 */
90 void errorOccurred(const QString &errorStr);
91
92 /**
93 * @brief paginationPresent Signal triggered when the issues or pull requests are so many that they are sent
94 * paginated.
95 * @param current The current page.
96 * @param next The next page.
97 * @param total The total of pages.
98 */
99 void paginationPresent(int current, int next, int total);
100
101 /**
102 * @brief issueCreated Signal triggered when an issue has been created.
103 * @param url The url of the issue.
104 */
105 void issueUpdated(const GitServer::Issue &issue);
106
107 /**
108 * @brief commentsReceived Signal triggered when comments for an issue has been received.
109 * @param issueNumber The number of the issue.
110 * @param comments The list of comments.
111 */
112 void commentsReceived(int issueNumber, const QVector<GitServer::Comment> &comments);
113
114 /**
115 * @brief onCodeReviewsReceived Signal triggered when code reviews for a PR has been received.
116 * @param prNumber The number of the PR.
117 * @param codeReviews The code reviews.
118 */
119 void codeReviewsReceived(int prNumber, const QVector<GitServer::CodeReview> &codeReviews);
120
121 /**
122 * @brief onCommentReviewsReceived Signal triggered when the review comments for a PR has been received.
123 * @param prNumber The number of the PR.
124 * @param commentReviews The comment reviews.
125 */
126 void commentReviewsReceived(int prNumber, const QMap<int, GitServer::Review> &commentReviews);
127
128 /**
129 * @brief commitsReceived Signal triggered when the commits of a PR has been received.
130 * @param prNumber The number of the PR.
131 * @param commits The commits.
132 */
133 void commitsReceived(int prNumber, const QVector<GitServer::Commit> &commits, int currentPage, int lastPage);
134
135 /**
136 * @brief pullRequestUpdated Signal triggered when a pull request has been updated.
137 * @param pr The updated pull request.
138 */
139 void pullRequestUpdated(const GitServer::PullRequest &pr);
140
141public:
142 explicit IRestApi(const ServerAuthentication &auth, QObject *parent = nullptr);
143 virtual ~IRestApi();
144
145 virtual QString getUserName() const { return mAuth.userName; }
146
147 static QJsonDocument validateData(QNetworkReply *reply, QString &errorString);
148
149 /**
150 * @brief testConnection Tests the connection against the server.
151 */
152 virtual void testConnection() = 0;
153 /**
154 * @brief createIssue Creates a new issue in the remote Git server.
155 * @param issue The informatio of the issue.
156 */
157 virtual void createIssue(const Issue &issue) = 0;
158 /**
159 * @brief updateIssue Updates an existing issue or pull request, if it doesn't exist it reports an error.
160 * @param issueNumber The issue number to update.
161 * @param issue The updated information of the issue.
162 */
163 virtual void updateIssue(int issueNumber, const Issue &issue) = 0;
164
165 /**
166 * @brief updatePullRequest Updates a pull request.
167 * @param number The number of the PR
168 * @param pr The pr to extract the data
169 */
170 virtual void updatePullRequest(int number, const PullRequest &pr) = 0;
171 /**
172 * @brief createPullRequest Creates a pull request in the remote Git server.
173 * @param pullRequest The information of the pull request.
174 */
175 virtual void createPullRequest(const PullRequest &pullRequest) = 0;
176 /**
177 * @brief requestLabels Requests the labels to the remote Git server.
178 */
179 virtual void requestLabels() = 0;
180 /**
181 * @brief requestMilestones Requests the milestones to the remote Git server.
182 */
183 virtual void requestMilestones() = 0;
184 /**
185 * @brief requestIssues Requests the issues to the remote Git server.
186 */
187 virtual void requestIssues(int page = -1) = 0;
188
189 /**
190 * @brief requestPullRequests Requests the pull request to the remote Git server.
191 */
192 virtual void requestPullRequests(int page = -1) = 0;
193 /**
194 * @brief mergePullRequest Merges a pull request into the destination branch.
195 * @param number The number of the pull request.
196 * @param data Byte array in JSON format with the necessary data to merge the pull request.
197 */
198 virtual void mergePullRequest(int number, const QByteArray &data) = 0;
199
200 /**
201 * @brief requestComments Requests all the comments of an issue. This doesn't get the reviews and comments on reviews
202 * for a pull request.
203 * @param issueNumber The issue number to query.
204 */
205 virtual void requestComments(int issueNumber) = 0;
206
207 /**
208 * @brief requestReviews Requests all the reviews in a Pull Requests.
209 * @param pr The Pull Request to query.
210 */
211 virtual void requestReviews(int prNumber) = 0;
212
213 /**
214 * @brief requestCommitsFromPR Requests all the commits from a PR.
215 * @param prNumber The Pr number.
216 */
217 virtual void requestCommitsFromPR(int prNumber) = 0;
218
219 /**
220 * @brief addIssueComment Adds a comment to an issue or PR.
221 */
222 virtual void addIssueComment(const Issue &, const QString &) { }
223
224 virtual void addPrReview(int, const QString &, const QString &) { }
225
226 virtual void addPrCodeReview(int, const QString &, const QString &, int, const QString &) { }
227
228 virtual void replyCodeReview(int, int, const QString &) { }
229
230protected:
231 QNetworkAccessManager *mManager = nullptr;
232 ServerAuthentication mAuth;
233
234 /**
235 * @brief createRequest Creates a request to be consumed by the Git remote server.
236 * @param page The destination page of the request.
237 * @return Returns a QNetworkRequest object with the configuration needed by the server.
238 */
239 virtual QNetworkRequest createRequest(const QString &page) const = 0;
240};
241
242}
243