From 362af83c86a622db5ab0800ba09c2c6c3933056d Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 13 Nov 2020 19:24:23 -0500 Subject: [PATCH] pr: removed all the crap, a pr can be fetched with just pull/pr/head --- src/cmd/commands.h | 12 +-- src/cmd/pr.cpp | 185 ++++++++++++++------------------------------- 2 files changed, 65 insertions(+), 132 deletions(-) diff --git a/src/cmd/commands.h b/src/cmd/commands.h index c2cd550..8a23391 100644 --- a/src/cmd/commands.h +++ b/src/cmd/commands.h @@ -248,6 +248,11 @@ protected: int do_run() override; private: + struct pr_info + { + std::string repo, org, branch; + }; + std::string pr_; std::string github_token_; std::string method_; @@ -255,12 +260,9 @@ private: std::pair parse_pr( const std::string& pr) const; - int do_apply(); - int do_remote(); - int do_fetch(); + pr_info get_pr_info(const modorganizer* task, const std::string& pr); - nlohmann::json get_pr_info(const modorganizer* task, const std::string& pr); - url get_diff_url(const modorganizer* task, const std::string& pr); + int pull_pr(); }; diff --git a/src/cmd/pr.cpp b/src/cmd/pr.cpp index 9c3a402..feb354a 100644 --- a/src/cmd/pr.cpp +++ b/src/cmd/pr.cpp @@ -26,6 +26,11 @@ command::meta_t pr_command::meta() const }; } +std::string pr_command::do_doc() +{ + return {}; +} + clipp::group pr_command::do_group() { return @@ -34,11 +39,6 @@ clipp::group pr_command::do_group() (clipp::option("-h", "--help") >> help_) % ("shows this message"), - (clipp::option("--method") - & clipp::value("METHOD") >> method_) - % "how to apply the changes, one of `apply` (default), `remote` or " - " `fetch`", - (clipp::option("--github-token") & clipp::value("TOKEN") >> github_token_) % "github api key", @@ -47,30 +47,11 @@ clipp::group pr_command::do_group() % "PR to apply, must be `task/pr`, such as `modorganizer/123`"; } -std::string pr_command::do_doc() -{ - return - "Methods:\n" - " - apply: retrieves the .diff file for this PR and runs\n" - " `git apply` with it, which patches the local repo and\n" - " leaves the changes uncommitted\n" - "\n" - " - remote: adds a new remote to the local repo that matches the \n" - " PR's origin and checks out the PR's branch from it\n" - "\n" - " - fetch: ??"; -} - int pr_command::do_run() { - if (method_ == "apply") - return do_apply(); - else if (method_ == "remote") - return do_remote(); - else if (method_ == "fetch") - return do_fetch(); + if (const auto r=pull_pr(); r != 0) + return r; - u8cerr << "bad method '" << method_ << "'\n"; return 1; } @@ -117,34 +98,7 @@ std::pair pr_command::parse_pr( return {task, pr_number}; } -int pr_command::do_apply() -{ - auto&& [task, pr] = parse_pr(pr_); - if (!task) - return 1; - - const auto u = get_diff_url(task, pr); - - curl_downloader dl; - - dl - .url(u) - .start() - .join(); - - if (!dl.ok()) - { - u8cerr << "getting pr diff failed\n"; - return 1; - } - - const auto diff = dl.steal_output(); - git::apply(task->this_source_path(), diff); - - return 0; -} - -int pr_command::do_remote() +int pr_command::pull_pr() { auto&& [task, pr] = parse_pr(pr_); if (!task) @@ -152,45 +106,15 @@ int pr_command::do_remote() try { - u8cout << "looking for pr " << pr << " in " << task->name() << "\n"; - - //const auto json = get_pr_info(task, pr); - auto json = nlohmann::json::parse(read_file("c:\\tmp\\" + pr + ".json")); - if (json.empty()) - return 1; - - const std::string repo = json["head"]["repo"]["name"]; - const std::string org = json["head"]["repo"]["owner"]["login"]; - const std::string remote_name = org; - const std::string branch = json["head"]["ref"]; - - u8cout - << "found pr: " - << "repo=" << repo << " " - << "org=" << org << " " - << "branch=" << branch << "\n"; - - if (git::has_remote(task->this_source_path(), remote_name)) - { - u8cout << "remote already exists\n"; - } - else - { - u8cout << "adding remote " << remote_name << "\n"; - - git::add_remote( - task->this_source_path(), - remote_name, org, {}, false, - "https://github.com/{}/{}"); - } - - u8cout << "fetching from " << remote_name << "\n"; - git::fetch(task->this_source_path(), remote_name, branch); - - u8cout << "checking out " << remote_name << "/" << branch << "\n"; - git::checkout( + u8cout << "fetching pr " << pr << " in " << task->name() << "\n"; + git::fetch( task->this_source_path(), - ::fmt::format("{}/{}", remote_name, branch)); + task->git_url().string(), ::fmt::format("pull/{}/head", pr)); + + u8cout << "checking out FETCH_HEAD\n"; + git::checkout(task->this_source_path(), "FETCH_HEAD"); + + u8cout << "note: " << task->name() << " is in detached HEAD state\n"; return 0; } @@ -201,47 +125,54 @@ int pr_command::do_remote() } } -int pr_command::do_fetch() -{ - return 0; -} - -url pr_command::get_diff_url(const modorganizer* task, const std::string& pr) -{ - return ::fmt::format( - "https://github.com/{}/{}/pull/{}.diff", - task->org(), task->repo(), pr); -} - -nlohmann::json pr_command::get_pr_info( +pr_command::pr_info pr_command::get_pr_info( const modorganizer* task, const std::string& pr) { - if (github_token_.empty()) + constexpr bool from_file = true; + + nlohmann::json json; + + if constexpr (from_file) { - u8cerr << "missing --github-token\n"; - return {}; + json = nlohmann::json::parse(read_file("c:\\tmp\\" + pr + ".json")); + if (json.empty()) + return {}; + } + else + { + 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); } - const url u(::fmt::format( - "https://api.github.com/repos/{}/{}/pulls/{}", - task->org(), task->repo(), pr)); + const std::string repo = json["head"]["repo"]["name"]; + const std::string org = json["head"]["repo"]["owner"]["login"]; + const std::string branch = json["head"]["ref"]; - 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(); - return nlohmann::json::parse(output); + return {repo, org, branch}; } } // namespace