1 #include "github_client.hpp"
2 
3 namespace horizon {
GitHubClient()4 GitHubClient::GitHubClient() : client("https://api.github.com")
5 {
6     client.append_header("Accept: application/vnd.github.v3+json");
7     client.append_header("Accept: application/json");
8     client.append_header("Content-Type: application/json");
9 }
10 
login(const std::string & user,const std::string & passwd)11 json GitHubClient::login(const std::string &user, const std::string &passwd)
12 {
13     client.set_auth(user, passwd);
14     login_user = user;
15     return client.get("/user");
16 }
17 
login_token(const std::string & token)18 json GitHubClient::login_token(const std::string &token)
19 {
20     client.append_header("authorization: Bearer " + token);
21     auto r = client.get("/user");
22     login_user = r.at("login");
23     return r;
24 }
25 
get_repo(const std::string & owner,const std::string & repo)26 json GitHubClient::get_repo(const std::string &owner, const std::string &repo)
27 {
28     return client.get("/repos/" + owner + "/" + repo);
29 }
30 
create_fork(const std::string & owner,const std::string & repo)31 json GitHubClient::create_fork(const std::string &owner, const std::string &repo)
32 {
33     return client.post("/repos/" + owner + "/" + repo + "/forks");
34 }
35 
create_pull_request(const std::string & owner,const std::string & repo,const std::string & title,const std::string & branch,const std::string & base,const std::string & body)36 json GitHubClient::create_pull_request(const std::string &owner, const std::string &repo, const std::string &title,
37                                        const std::string &branch, const std::string &base, const std::string &body)
38 {
39     json j;
40     j["title"] = title;
41     j["head"] = login_user + ":" + branch;
42     j["base"] = base;
43     j["body"] = body;
44     j["maintainer_can_modify"] = true;
45 
46     return client.post("/repos/" + owner + "/" + repo + "/pulls", j);
47 }
48 
get_pull_requests(const std::string & owner,const std::string & repo)49 json GitHubClient::get_pull_requests(const std::string &owner, const std::string &repo)
50 {
51     return client.get("/repos/" + owner + "/" + repo + "/pulls");
52 }
53 
get_pull_request(const std::string & owner,const std::string & repo,unsigned int pr)54 json GitHubClient::get_pull_request(const std::string &owner, const std::string &repo, unsigned int pr)
55 {
56     return client.get("/repos/" + owner + "/" + repo + "/pulls/" + std::to_string(pr));
57 }
58 
add_issue_comment(const std::string & owner,const std::string & repo,unsigned int id,const std::string & body)59 json GitHubClient::add_issue_comment(const std::string &owner, const std::string &repo, unsigned int id,
60                                      const std::string &body)
61 {
62     json j;
63     j["body"] = body;
64 
65     return client.post("/repos/" + owner + "/" + repo + "/issues/" + std::to_string(id) + "/comments", j);
66 }
67 
68 } // namespace horizon
69