Add support for commits.
continuous-integration/drone/push Build is passing Details

Bug: #2
This commit is contained in:
tastytea 2021-04-21 11:03:39 +02:00
parent 98b6572b14
commit 6b08adce38
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 89 additions and 3 deletions

78
src/commits.cpp Normal file
View File

@ -0,0 +1,78 @@
/* This file is part of gitea2rss.
* Copyright © 2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gitea2rss.hpp"
#include <json/json.h>
#include <iostream>
#include <sstream>
namespace gitea2rss
{
using std::cerr;
using std::cout;
using std::endl;
using std::stringstream;
uint8_t write_commits(const string &url)
{
const string baseurl = get_baseurl(url);
const string repo = get_repo(url);
stringstream data(get_http(baseurl + "/api/v1/repos/" + repo + "/commits"));
if (cgi)
{
cout << endl;
}
if (data.str().empty())
{
cerr << "Error: Could not download commits.\n";
return 2;
}
write_preamble(url, "commits");
Json::Value json;
data >> json;
for (const Json::Value &ref : json)
{
const string sha = ref["sha"].asString();
const string message = ref["commit"]["message"].asString();
const string title = message.substr(0, message.find('\n'));
const string commit_url = ref["html_url"].asString();
const string author = ref["commit"]["author"]["email"].asString() + " ("
+ ref["commit"]["author"]["name"].asString()
+ ")";
cout << " <item>\n";
write_line(6, "title", get_project(url) + ": " + title);
write_line(6, "link", commit_url);
write_line(6, "guid isPermaLink=\"false\"",
get_domain(url) + " commit " + sha);
write_line(6, "description",
"\n <![CDATA[<p>" + message + "</p>]]>\n ");
write_line(6, "author", author);
cout << " </item>\n";
}
return 0;
}
} // namespace gitea2rss

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
* Copyright © 2019-2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -55,6 +55,9 @@ uint8_t write_releases(const string &url);
//! Write tags.
uint8_t write_tags(const string &url);
//! Write commits.
uint8_t write_commits(const string &url);
//! @brief Get the base URL, without trailing slash.
string get_baseurl(const string &url);

View File

@ -1,5 +1,5 @@
/* This file is part of gitea2rss.
* Copyright © 2019, 2020 tastytea <tastytea@tastytea.de>
* Copyright © 2019-2021 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -16,6 +16,7 @@
#include "gitea2rss.hpp"
#include "version.hpp"
#include <iostream>
#include <string>
@ -67,7 +68,7 @@ int main(int argc, char *argv[])
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
cerr << "usage: " << argv[0]
<< " URL of Gitea project [releases|tags]\n";
<< " URL of Gitea project [releases|tags|commits]\n";
return 1;
}
else
@ -90,6 +91,10 @@ int main(int argc, char *argv[])
{
ret = write_tags(url);
}
else if (type == "commits")
{
ret = write_commits(url);
}
if (ret != 0)
{