2020-07-02 00:19:18 +02:00
|
|
|
/* This file is part of FediBlock-backend.
|
|
|
|
* Copyright © 2020 tastytea <tastytea@tastytea.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gitea.hpp"
|
|
|
|
|
|
|
|
#include "cgi.hpp"
|
|
|
|
#include "files.hpp"
|
|
|
|
#include "json.hpp"
|
|
|
|
|
2020-07-02 01:17:08 +02:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2020-07-02 00:19:18 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
namespace FediBlock::gitea
|
|
|
|
{
|
|
|
|
|
|
|
|
using std::runtime_error;
|
|
|
|
using std::string;
|
|
|
|
using std::string_view;
|
|
|
|
using std::to_string;
|
|
|
|
|
|
|
|
CURL *_connection;
|
2020-07-02 01:17:08 +02:00
|
|
|
string _curl_buffer_body;
|
|
|
|
uint64_t _last_pr_number{0};
|
2020-07-02 03:20:27 +02:00
|
|
|
constexpr string_view _forge_baseurl{"https://schlomp.space"};
|
|
|
|
constexpr string_view _repo_name{"FediBlock/data"};
|
2020-07-02 00:19:18 +02:00
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
_connection = curl_easy_init();
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
CURLcode code{curl_easy_setopt(_connection, CURLOPT_FOLLOWLOCATION, 1L)};
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw runtime_error{"HTTP is not supported."};
|
|
|
|
}
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(_connection, CURLOPT_MAXREDIRS, 5L);
|
|
|
|
|
|
|
|
// Add extra headers.
|
|
|
|
struct curl_slist *headers{nullptr};
|
|
|
|
string authheader{"Authorization: token " + files::get_access_token()};
|
|
|
|
headers = curl_slist_append(headers, authheader.c_str());
|
|
|
|
headers = curl_slist_append(headers, "Accept: application/json");
|
|
|
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_HTTPHEADER, headers);
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw runtime_error{"Could not add headers"};
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-07-02 01:17:08 +02:00
|
|
|
curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, writer_body);
|
2020-07-02 00:19:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
curl_easy_cleanup(gitea::_connection);
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:17:08 +02:00
|
|
|
size_t writer_body(char *data, size_t size, size_t nmemb)
|
2020-07-02 00:19:18 +02:00
|
|
|
{
|
2020-07-02 01:17:08 +02:00
|
|
|
if (data == nullptr)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_curl_buffer_body.append(data, size * nmemb);
|
|
|
|
|
2020-07-02 00:19:18 +02:00
|
|
|
return size * nmemb;
|
|
|
|
}
|
|
|
|
|
2020-07-02 01:17:08 +02:00
|
|
|
string api_request(const http_method method, const string_view path,
|
|
|
|
const string_view body)
|
2020-07-02 00:19:18 +02:00
|
|
|
{
|
|
|
|
CURLcode code;
|
2020-07-02 03:20:27 +02:00
|
|
|
string url{string(_forge_baseurl) += path};
|
2020-07-02 01:17:08 +02:00
|
|
|
_curl_buffer_body.clear();
|
2020-07-02 00:19:18 +02:00
|
|
|
|
2020-07-02 01:17:08 +02:00
|
|
|
switch (method)
|
|
|
|
{
|
|
|
|
case http_method::GET:
|
|
|
|
{
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(_connection, CURLOPT_HTTPGET, 1L);
|
|
|
|
url += body;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case http_method::POST:
|
2020-07-02 00:19:18 +02:00
|
|
|
{
|
2020-07-02 01:17:08 +02:00
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_setopt(_connection, CURLOPT_POSTFIELDSIZE, body.size());
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_POSTFIELDS, body.data());
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw runtime_error{"Couldn't set up data to send"};
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-07-02 00:19:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
2020-07-02 01:17:08 +02:00
|
|
|
code = curl_easy_setopt(_connection, CURLOPT_URL, url.c_str());
|
2020-07-02 00:19:18 +02:00
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
2020-07-02 01:17:08 +02:00
|
|
|
throw runtime_error{"Couldn't set URL: " + to_string(code)};
|
2020-07-02 00:19:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
code = curl_easy_perform(_connection);
|
|
|
|
if (code != CURLE_OK)
|
|
|
|
{
|
|
|
|
throw runtime_error{"libcurl error: " + to_string(code)};
|
|
|
|
}
|
|
|
|
|
|
|
|
long http_status; // NOLINT(google-runtime-int)
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
|
|
curl_easy_getinfo(_connection, CURLINFO_RESPONSE_CODE, &http_status);
|
2020-07-02 01:17:08 +02:00
|
|
|
if (http_status < 200 || http_status >= 300)
|
2020-07-02 00:19:18 +02:00
|
|
|
{
|
|
|
|
throw runtime_error{"HTTP error: " + to_string(http_status)};
|
|
|
|
}
|
2020-07-02 01:17:08 +02:00
|
|
|
|
|
|
|
return _curl_buffer_body;
|
2020-07-02 00:19:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pull_request(const string_view branch, const cgi::entry_type &entry)
|
|
|
|
{
|
2020-07-02 03:20:27 +02:00
|
|
|
api_request(http_method::POST,
|
|
|
|
(string("/api/v1/repos/") += _repo_name) += "/pulls",
|
2020-07-02 00:19:18 +02:00
|
|
|
json::pull_request_body(branch, entry));
|
2020-07-02 01:17:08 +02:00
|
|
|
++_last_pr_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t get_last_pr_number()
|
|
|
|
{
|
|
|
|
if (_last_pr_number == 0)
|
|
|
|
{
|
|
|
|
const auto answer{api_request(http_method::GET,
|
|
|
|
"/api/v1/repos/FediBlock/data/pulls",
|
2020-07-02 01:38:03 +02:00
|
|
|
"?sort=oldest")};
|
2020-07-02 01:17:08 +02:00
|
|
|
if (answer == "[]")
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const auto json{nlohmann::json::parse(answer)};
|
2020-07-02 01:38:03 +02:00
|
|
|
_last_pr_number = json.front().back()["number"].get<uint64_t>();
|
2020-07-02 01:17:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return _last_pr_number;
|
2020-07-02 00:19:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FediBlock::gitea
|