GitWatcherBot 1.0.0
A Telegram Bot that notifies you when a new change is made in your repositories (issues, pull requests, stars, forks, and watches)
Loading...
Searching...
No Matches
Repository.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <cstring>
6#include "sqlite_orm/sqlite_orm.h"
7#include "User.hpp"
8
9
10namespace models {
11 using RepositoryId = std::int64_t;
12
13 struct Repository {
15 std::string full_name;
16 std::int64_t stargazers_count{};
17 std::int64_t watchers_count{};
18 std::int64_t open_issues_count{};
19 std::int64_t pulls_count{};
20 std::int64_t forks_count{};
21 std::string description;
22 std::size_t size{};
23 std::string language;
24 std::time_t createdAt{};
25 std::time_t updatedAt{};
26 std::unique_ptr<UserId> watcher_id;
27
28 static auto table() {
29 using namespace sqlite_orm;
30 return make_table("Repositories",
31 make_column("id", &Repository::id), // this is repository id from GitHub Api, don't make it primary_key() since multiple users can watch the same repository
32 make_column("full_name", &Repository::full_name),
33 make_column("stargazers_count", &Repository::stargazers_count),
34 make_column("watchers_count", &Repository::watchers_count),
35 make_column("open_issues_count", &Repository::open_issues_count),
36 make_column("pulls_count", &Repository::pulls_count),
37 make_column("forks_count", &Repository::forks_count),
38 make_column("description", &Repository::description),
39 make_column("size", &Repository::size),
40 make_column("language", &Repository::language),
41 make_column("createdAt", &Repository::createdAt),
42 make_column("updatedAt", &Repository::updatedAt),
43 make_column("watcher_id", &Repository::watcher_id),
44 foreign_key(&Repository::watcher_id).references(&User::id)
45 //primary_key(&Repository::id, &Repository::watcher_id) // TODO: don't try this, doesn't work with sqlite orm,
46 // try it later in separate project (Primary key ownership is repo id + watcher id since multiple
47 // users can watch the same repository, this way we can call storage.update(repo))
48 );
49 }
50
51#define DESERIALIZE(field) \
52 try {\
53 field = json[#field]; \
54 }\
55 catch(const std::exception& e)\
56 {\
57 throw std::runtime_error("Failed to deserialize json field '" + std::string(#field) + "': " + e.what());\
58 }
59
60 Repository() = default;
61
62 explicit Repository(const nl::json &json) {
63
64 DESERIALIZE(id);
73
74
75
76 // for pulls its different https://stackoverflow.com/questions/40534533/count-open-pull-requests-and-issues-on-github
77 // https://api.github.com/search/issues?q=repo:baderouaich/tgbotxx%20is:pr%20is:open&per_page=1
78 try {
79 auto res = cpr::Get(cpr::Url("https://api.github.com/search/issues?q=repo:" + full_name + "%20is:pr%20is:open&per_page=1"));
80 nl::json data = nl::json::parse(res.text);
81 pulls_count = data["total_count"];
82 } catch (const std::exception &e) {
83 throw std::runtime_error("Failed to get pulls_count for " + full_name + ": " + e.what());
84 }
85
86 createdAt = std::time(nullptr);
88 }
89
90 };
91
92#undef DESERIALIZE
93}
#define DESERIALIZE(field)
Definition Log.hpp:12
std::int64_t RepositoryId
std::int64_t forks_count
static auto table()
std::time_t createdAt
std::int64_t watchers_count
std::string full_name
std::int64_t pulls_count
std::string language
std::string description
std::unique_ptr< UserId > watcher_id
! User id who is watching changes on this repo
Repository()=default
std::int64_t stargazers_count
Repository(const nl::json &json)
std::time_t updatedAt
RepositoryId id
std::int64_t open_issues_count
UserId id
Definition User.hpp:16