From 1221556de7419c42255018f98256d0f48cff71d5 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 13 Nov 2020 22:09:14 -0500 Subject: [PATCH] removed debug files stuff added github_key to ini --- mob.ini | 1 + src/cmd/pr.cpp | 125 +++++++++++++++++++++---------------------------- 2 files changed, 55 insertions(+), 71 deletions(-) diff --git a/mob.ini b/mob.ini index 6274651..a26f159 100644 --- a/mob.ini +++ b/mob.ini @@ -11,6 +11,7 @@ output_log_level = 3 file_log_level = 5 log_file = mob.log ignore_uncommitted = false +github_key = [task] enabled = true diff --git a/src/cmd/pr.cpp b/src/cmd/pr.cpp index 14c2e25..00a7006 100644 --- a/src/cmd/pr.cpp +++ b/src/cmd/pr.cpp @@ -64,6 +64,9 @@ clipp::group pr_command::do_group() int pr_command::do_run() { + if (github_token_.empty()) + github_token_ = conf::global_by_name("github_key"); + if (op_ == "pull") return pull(); else if (op_ == "find") @@ -197,6 +200,8 @@ std::vector pr_command::get_matching_prs( u8cout << "getting info for pr " << src_pr << " in " << task->name() << "\n"; const auto info = get_pr_info(task, src_pr); + if (info.repo.empty()) + return {}; u8cout << "found pr from " << info.author << ":" << info.branch << "\n"; @@ -218,49 +223,38 @@ std::vector pr_command::get_matching_prs( std::vector pr_command::search_prs( const std::string& org, const std::string& author, const std::string& branch) { - constexpr bool from_file = true; - nlohmann::json json; - if (from_file) + constexpr auto* pattern = + "https://api.github.com/search/issues?q=" + "is:pr+org:{org:}+author:{author:}+is:open+head:{branch:}"; + + const auto search_url = ::fmt::format( + pattern, + ::fmt::arg("org", org), + ::fmt::arg("author", author), + ::fmt::arg("branch", branch)); + + u8cout << "search url is " << search_url << "\n"; + + u8cout << "searching for matching prs\n"; + + curl_downloader dl; + + dl + .url(search_url) + .header("Authorization", "token " + github_token_) + .start() + .join(); + + if (!dl.ok()) { - json = nlohmann::json::parse(read_file("c:\\tmp\\1277-search.json")); - if (json.empty()) - return {}; + u8cerr << "failed to search github\n"; + return {}; } - else - { - constexpr auto* pattern = - "https://api.github.com/search/issues?q=" - "is:pr+org:{org:}+author:{author:}+is:open+head:{branch:}"; - const auto url = ::fmt::format( - pattern, - ::fmt::arg("org", org), - ::fmt::arg("author", author), - ::fmt::arg("branch", branch)); - - u8cout << "search url is " << url << "\n"; - - u8cout << "searching for matching prs\n"; - - curl_downloader dl; - - dl - .url(url) - .header("Authorization", "token " + github_token_) - .start() - .join(); - - if (!dl.ok()) - { - u8cerr << "failed to search github\n"; - return {}; - } - - const auto output = dl.steal_output(); - json = nlohmann::json::parse(output); - } + const auto output = dl.steal_output(); + json = nlohmann::json::parse(output); std::map repos; @@ -300,46 +294,35 @@ std::vector pr_command::search_prs( pr_command::pr_info pr_command::get_pr_info( const modorganizer* task, const std::string& pr) { - constexpr bool from_file = true; - nlohmann::json json; - if constexpr (from_file) + if (github_token_.empty()) { - json = nlohmann::json::parse(read_file("c:\\tmp\\" + pr + ".json")); - if (json.empty()) - return {}; + u8cerr << "missing --github-token\n"; + return {}; } - else + + const url u(::fmt::format( + "https://api.github.com/repos/{}/{}/pulls/{}", + task->org(), task->repo(), pr)); + + curl_downloader dl; + + dl + .url(u) + .header("Authorization", "token " + github_token_) + .start() + .join(); + + if (!dl.ok()) { - if (github_token_.empty()) - { - u8cerr << "missing --github-token\n"; - return {}; - } - - const url u(::fmt::format( - "https://api.github.com/repos/{}/{}/pulls/{}", - task->org(), task->repo(), pr)); - - curl_downloader dl; - - dl - .url(u) - .header("Authorization", "token " + github_token_) - .start() - .join(); - - if (!dl.ok()) - { - u8cerr << "failed to get pr info from github\n"; - return {}; - } - - const auto output = dl.steal_output(); - json = nlohmann::json::parse(output); + u8cerr << "failed to get pr info from github\n"; + return {}; } + const auto output = dl.steal_output(); + json = nlohmann::json::parse(output); + const std::string repo = json["head"]["repo"]["name"]; const std::string author = json["head"]["repo"]["owner"]["login"]; const std::string branch = json["head"]["ref"];