1 | #include "MergePullRequestDlg.h" |
2 | #include "ui_MergePullRequestDlg.h" |
3 | #include <GitConfig.h> |
4 | #include <GitQlientSettings.h> |
5 | #include <GitHubRestApi.h> |
6 | #include <GitLabRestApi.h> |
7 | #include <GitRemote.h> |
8 | |
9 | #include <QMessageBox> |
10 | #include <QJsonDocument> |
11 | |
12 | using namespace GitServer; |
13 | |
14 | MergePullRequestDlg::MergePullRequestDlg(const QSharedPointer<GitBase> git, const PullRequest &pr, const QString &sha, |
15 | QWidget *parent) |
16 | : QDialog(parent) |
17 | , ui(new Ui::MergePullRequestDlg) |
18 | , mGit(git) |
19 | , mPr(pr) |
20 | , mSha(sha) |
21 | { |
22 | ui->setupUi(this); |
23 | |
24 | QScopedPointer<GitConfig> gitConfig(new GitConfig(mGit)); |
25 | const auto serverUrl = gitConfig->getServerHost(); |
26 | |
27 | GitQlientSettings settings("" ); |
28 | const auto userName = settings.globalValue(QString("%1/user" ).arg(serverUrl)).toString(); |
29 | const auto userToken = settings.globalValue(QString("%1/token" ).arg(serverUrl)).toString(); |
30 | const auto repoInfo = gitConfig->getCurrentRepoAndOwner(); |
31 | const auto endpoint = settings.globalValue(QString("%1/endpoint" ).arg(serverUrl)).toString(); |
32 | |
33 | if (serverUrl.contains("github" )) |
34 | mApi = new GitHubRestApi(repoInfo.first, repoInfo.second, { userName, userToken, endpoint }); |
35 | else |
36 | mApi = new GitLabRestApi(userName, repoInfo.second, serverUrl, { userName, userToken, endpoint }); |
37 | |
38 | connect(mApi, &GitHubRestApi::pullRequestMerged, this, &MergePullRequestDlg::onPRMerged); |
39 | connect(mApi, &IRestApi::errorOccurred, this, &MergePullRequestDlg::onGitServerError); |
40 | |
41 | connect(ui->pbMerge, &QPushButton::clicked, this, &MergePullRequestDlg::accept); |
42 | connect(ui->pbCancel, &QPushButton::clicked, this, &MergePullRequestDlg::reject); |
43 | } |
44 | |
45 | MergePullRequestDlg::~MergePullRequestDlg() |
46 | { |
47 | delete ui; |
48 | } |
49 | |
50 | void MergePullRequestDlg::accept() |
51 | { |
52 | if (ui->leTitle->text().isEmpty() || ui->leMessage->text().isEmpty()) |
53 | QMessageBox::warning(this, tr("Empty fields" ), tr("Please, complete all fields with valid data." )); |
54 | else |
55 | { |
56 | ui->pbMerge->setEnabled(false); |
57 | |
58 | QJsonObject object; |
59 | object.insert("commit_title" , ui->leTitle->text()); |
60 | object.insert("commit_message" , ui->leMessage->text()); |
61 | object.insert("sha" , mSha); |
62 | object.insert("merge_method" , "merge" ); |
63 | QJsonDocument doc(object); |
64 | const auto data = doc.toJson(QJsonDocument::Compact); |
65 | |
66 | mApi->mergePullRequest(mPr.id, data); |
67 | } |
68 | } |
69 | |
70 | void MergePullRequestDlg::onPRMerged() |
71 | { |
72 | QMessageBox::information(this, tr("PR merged!" ), tr("The pull request has been merged." )); |
73 | |
74 | QScopedPointer<GitRemote> git(new GitRemote(mGit)); |
75 | |
76 | if (auto ret = git->pull(); ret.success) |
77 | { |
78 | git->prune(); |
79 | emit signalRepositoryUpdated(); |
80 | } |
81 | |
82 | QDialog::accept(); |
83 | } |
84 | |
85 | void MergePullRequestDlg::onGitServerError(const QString &error) |
86 | { |
87 | ui->pbMerge->setEnabled(true); |
88 | |
89 | QMessageBox::warning(this, tr("API access error!" ), error); |
90 | } |
91 | |