This repository has been archived on 2021-03-22. You can view files and clone it, but cannot push or open issues or pull requests.
backend/src/main.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

2020-06-29 03:01:30 +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/>.
*/
2020-06-29 05:21:23 +02:00
#include "cgi.hpp"
#include "config.hpp"
#include "files.hpp"
#include "fs-compat.hpp"
2020-07-01 03:51:00 +02:00
#include "git.hpp"
2020-07-02 00:19:18 +02:00
#include "gitea.hpp"
2020-06-29 06:10:15 +02:00
#include "json.hpp"
2020-07-02 07:13:57 +02:00
#include "types.hpp"
2020-06-29 05:21:23 +02:00
2020-07-01 03:51:00 +02:00
#include <git2/global.h>
#include <exception>
2020-06-29 03:28:07 +02:00
#include <iostream>
2020-06-29 03:01:30 +02:00
using std::cerr;
2020-06-29 03:28:07 +02:00
using std::cout;
using std::exception;
2020-06-29 03:01:30 +02:00
2020-06-29 05:21:23 +02:00
using namespace FediBlock;
2020-06-29 03:28:07 +02:00
2020-06-29 05:21:23 +02:00
int main()
{
cout << "Content-Type: text/plain; charset=utf-8\r\n\r\n";
2020-06-29 22:37:09 +02:00
2020-10-17 01:55:29 +02:00
git::init(true);
gitea::init();
2020-07-01 03:51:00 +02:00
2020-07-02 01:26:47 +02:00
try
{
2020-07-02 07:13:57 +02:00
const entry_type entry{cgi::parse_formdata()};
if (entry.instance.empty() || entry.tags.empty()
|| entry.receipts.empty() || entry.description.empty())
{
// Probably spam, since the input fields are marked as required.
cout << "One of the required fields (Instance, Tags, Receipts, "
"Description) was not filled.\r\n"
<< "Please use the back button, complete the form and submit "
"again.\r\n";
return 1;
}
2020-07-02 01:26:47 +02:00
2020-10-17 01:55:29 +02:00
git::update_cached_repo();
2020-07-02 01:26:47 +02:00
git::create_branch();
git::commit(entry);
git::push();
2020-07-01 03:51:00 +02:00
gitea::pull_request(git::get_branch_name(), entry);
2020-07-01 22:10:11 +02:00
cout << "Created pull request: <https://" << config::forge_domain << "/"
<< config::forge_org << "/" << config::forge_repo_data << "/pulls/"
<< gitea::get_last_pr_number() << ">.\r\n";
cout << "Your entry needs to be approved before it appears on the "
"website. You can subscribe to notifications or add comments "
"at the URL above.\r\n";
2020-07-02 00:19:18 +02:00
2020-07-01 20:51:35 +02:00
files::remove_tmpdir();
}
catch (const exception &e)
{
cerr << e.what() << '\n';
2020-07-02 01:39:19 +02:00
cout << "An error happened: " << e.what() << "\r\n";
cout << "Please report it at <https://" << config::forge_domain << "/"
<< config::forge_org << "/" << config::forge_repo_backend
<< "/issues>.\r\n";
}
2020-06-29 03:01:30 +02:00
2020-07-02 00:19:18 +02:00
gitea::cleanup();
2020-10-17 01:55:29 +02:00
git::cleanup(true);
2020-07-01 03:51:00 +02:00
2020-06-29 03:01:30 +02:00
return 0;
}