From b7dccc5e33704605743e82906fd7cf12bb8b7ad0 Mon Sep 17 00:00:00 2001 From: "Damien Buhl (alias daminetreg)" Date: Fri, 27 Jan 2023 15:07:12 +0100 Subject: [PATCH] :wink: gh::get_tag can be used to retrieve the actual commit pointed by an annotated tag. --- gh/refs.hxx | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ test/gh-refs.cpp | 4 ++++ 2 files changed, 61 insertions(+) diff --git a/gh/refs.hxx b/gh/refs.hxx index 9d89492e..dbfa772b 100644 --- a/gh/refs.hxx +++ b/gh/refs.hxx @@ -27,10 +27,30 @@ namespace gh::git_data { }; using refs = std::vector; + + struct verification_t { + bool verified; + std::string reason; + std::optional signature; + std::optional payload; + }; + + //! |brief Object type "tag" on github is used only for tags with annotations + struct tag_t { + std::string sha; + std::string tag; + std::string message; + std::string url; + object_t object; + std::optional verification; + }; + } BOOST_FUSION_ADAPT_STRUCT(gh::git_data::object_t, type, sha, url); BOOST_FUSION_ADAPT_STRUCT(gh::git_data::ref_t, ref, url, object); +BOOST_FUSION_ADAPT_STRUCT(gh::git_data::verification_t, verified, reason, signature, payload); +BOOST_FUSION_ADAPT_STRUCT(gh::git_data::tag_t, sha, tag, message, url, object, verification); namespace gh { @@ -143,4 +163,41 @@ namespace gh { } + /** + * \brief get an annotated tag details, useful to know which commit the annotated tag refers too. + * \param auth credentials + * \param owner + * \param repository + * \param annotated_tag_obj_sha The sha of the annotation object that the tags points to. This is the object.sha from an object returned by get_refs, when object.type is "tag". + * \param result_handler taking a single git::data::tag_t + */ + inline void get_tag(std::string owner, std::string repository, + const std::string& annotated_tag_obj_sha, + std::function&& result_handler, + std::optional auth = std::nullopt, + const std::string& api_endpoint = "https://api.github.com"s ) { + + using namespace xxhr; + auto url = api_endpoint + "/repos/"s + owner + "/" + repository + + "/git/tags/" + annotated_tag_obj_sha; + + auto response_handler = [&](auto&& resp) { + if ( (!resp.error) && (resp.status_code == 200) ) { + result_handler(pre::json::from_json(resp.text)); + } else { + throw std::runtime_error( "err : "s + std::string(resp.error) + "status: "s + + std::to_string(resp.status_code) + " accessing : "s + url ); + } + }; + + if (auth) { + GET(url, Authentication{auth->user, auth->pass}, + on_response = response_handler); + } else { + GET(url, on_response = response_handler); + } + + } + + } diff --git a/test/gh-refs.cpp b/test/gh-refs.cpp index e16920e5..31725637 100644 --- a/test/gh-refs.cpp +++ b/test/gh-refs.cpp @@ -34,5 +34,9 @@ int main(int argc, char** argv) { std::cout << "Successfully retrieved " << refs.size() << " refs from github.com/grpc/grpc with AUTHENTICATION - which is a PASS" << std::endl; }, auth); + gh::get_tag("nlohmann", "json", "0ca0fe433eb70cea0d5761079c0c5b47b736565b", [](gh::git_data::tag_t&& annotated) { + std::cout << "Successfully retrieved 0ca0fe433eb70cea0d5761079c0c5b47b736565b: " << annotated.tag << " pointing to commit " << annotated.object.sha << std::endl; + }, auth); + return 0; }