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)
All Classes Namespaces Files Functions Variables Typedefs Macros
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 
10 namespace 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: Repository.hpp:51
Definition: Log.hpp:12
std::int64_t RepositoryId
Definition: Repository.hpp:11
std::int64_t forks_count
Definition: Repository.hpp:20
static auto table()
Definition: Repository.hpp:28
std::time_t createdAt
Definition: Repository.hpp:24
std::int64_t watchers_count
Definition: Repository.hpp:17
std::string full_name
Definition: Repository.hpp:15
std::int64_t pulls_count
Definition: Repository.hpp:19
std::string language
Definition: Repository.hpp:23
std::string description
Definition: Repository.hpp:21
std::unique_ptr< UserId > watcher_id
! User id who is watching changes on this repo
Definition: Repository.hpp:26
Repository()=default
std::int64_t stargazers_count
Definition: Repository.hpp:16
Repository(const nl::json &json)
Definition: Repository.hpp:62
std::time_t updatedAt
Definition: Repository.hpp:25
RepositoryId id
Definition: Repository.hpp:14
std::int64_t open_issues_count
Definition: Repository.hpp:18
std::size_t size
Definition: Repository.hpp:22
UserId id
Definition: User.hpp:16