123 lines
3.7 KiB
C++
123 lines
3.7 KiB
C++
/* 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 "curl/curl.h"
|
|
#include "files.hpp"
|
|
#include "json.hpp"
|
|
|
|
#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;
|
|
|
|
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"};
|
|
}
|
|
|
|
// Write the body into the void.
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
curl_easy_setopt(_connection, CURLOPT_WRITEFUNCTION, noop_cb);
|
|
}
|
|
|
|
void cleanup()
|
|
{
|
|
curl_easy_cleanup(gitea::_connection);
|
|
}
|
|
|
|
size_t noop_cb(void * /*ptr*/, size_t size, size_t nmemb, void * /*data*/)
|
|
{
|
|
return size * nmemb;
|
|
}
|
|
|
|
void api_request(const string_view path, const string_view body)
|
|
{
|
|
CURLcode code;
|
|
const string url{string("https://schlomp.space") += path};
|
|
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
code = curl_easy_setopt(_connection, CURLOPT_URL, url.c_str());
|
|
if (code != CURLE_OK)
|
|
{
|
|
throw runtime_error{"Couldn't set URL"};
|
|
}
|
|
|
|
// Prepare body to send.
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
curl_easy_setopt(_connection, CURLOPT_POSTFIELDSIZE, body.size());
|
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
|
|
curl_easy_setopt(_connection, CURLOPT_POSTFIELDS, body.data());
|
|
if (code != CURLE_OK)
|
|
{
|
|
throw runtime_error{"Couldn't set up data to send"};
|
|
}
|
|
|
|
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);
|
|
if (http_status != 201) // 201 == Created.
|
|
{
|
|
throw runtime_error{"HTTP error: " + to_string(http_status)};
|
|
}
|
|
}
|
|
|
|
void pull_request(const string_view branch, const cgi::entry_type &entry)
|
|
{
|
|
api_request("/api/v1/repos/FediBlock/data/pulls",
|
|
json::pull_request_body(branch, entry));
|
|
}
|
|
|
|
} // namespace FediBlock::gitea
|