Skip to content

Commit

Permalink
Make the github server URL required and fix preexisting bad *x.get()s.
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyONeal committed Dec 6, 2024
1 parent 7df73fd commit 10dce58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 38 deletions.
11 changes: 4 additions & 7 deletions include/vcpkg/base/downloads.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ namespace vcpkg
View<std::string> headers,
View<std::string> secrets);

bool send_snapshot_to_api(StringView github_api_url,
StringView github_token,
StringView github_repository,
const Json::Object& snapshot);
bool send_snapshot_to_api(const std::string& github_token,
const std::string& github_repository,
const Json::Object& snapshot);
bool submit_github_dependency_graph_snapshot(const Optional<std::string>& maybe_github_server_url,
const std::string& github_token,
const std::string& github_repository,
const Json::Object& snapshot);
ExpectedL<int> put_file(const ReadOnlyFilesystem&,
StringView url,
const std::vector<std::string>& secrets,
Expand Down
33 changes: 19 additions & 14 deletions src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,20 +483,27 @@ namespace vcpkg
secrets);
}

bool send_snapshot_to_api(const std::string& github_token,
const std::string& github_repository,
const Json::Object& snapshot)
{
return send_snapshot_to_api("https://api.github.com", github_token, github_repository, snapshot);
}

bool send_snapshot_to_api(StringView github_api_url,
StringView github_token,
StringView github_repository,
const Json::Object& snapshot)
bool submit_github_dependency_graph_snapshot(const Optional<std::string>& maybe_github_server_url,
const std::string& github_token,
const std::string& github_repository,
const Json::Object& snapshot)
{
static constexpr StringLiteral guid_marker = "fcfad8a3-bb68-4a54-ad00-dab1ff671ed2";

std::string uri;
if (auto github_server_url = maybe_github_server_url.get())
{
uri = *github_server_url;
uri.append("/api/v3");
}
else
{
uri = "https://api.github.com";
}

fmt::format_to(
std::back_inserter(uri), "/repos/{}/dependency-graph/snapshots", url_encode_spaces(github_repository));

auto cmd = Command{"curl"};
cmd.string_arg("-w").string_arg("\\n" + guid_marker.to_string() + "%{http_code}");
cmd.string_arg("-X").string_arg("POST");
Expand All @@ -505,9 +512,7 @@ namespace vcpkg
std::string res = "Authorization: Bearer " + github_token;
cmd.string_arg("-H").string_arg(res);
cmd.string_arg("-H").string_arg("X-GitHub-Api-Version: 2022-11-28");
cmd.string_arg(Strings::concat(static_cast<std::string>(github_api_url) + "/repos/",
url_encode_spaces(github_repository),
"/dependency-graph/snapshots"));
cmd.string_arg(uri);
cmd.string_arg("-d").string_arg("@-");

RedirectedProcessLaunchSettings settings;
Expand Down
27 changes: 10 additions & 17 deletions src/vcpkg/commands.set-installed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,23 +194,16 @@ namespace vcpkg
if (paths.manifest_mode_enabled() && paths.get_feature_flags().dependency_graph)
{
msg::println(msgDependencyGraphCalculation);
auto snapshot = create_dependency_graph_snapshot(args, action_plan);
bool s = false;
if (snapshot.has_value() && args.github_token.has_value() && args.github_repository.has_value())
auto maybe_snapshot = create_dependency_graph_snapshot(args, action_plan);
auto snapshot = maybe_snapshot.get();
auto github_token = args.github_token.get();
auto github_repository = args.github_repository.get();
bool dependency_graph_success = false;
if (snapshot && github_token && github_repository)
{
if (args.github_server_url.has_value())
{
s = send_snapshot_to_api(*args.github_server_url.get() + "/api/v3",
*args.github_token.get(),
*args.github_repository.get(),
*snapshot.get());
}
else
{
s = send_snapshot_to_api(*args.github_token.get(), *args.github_repository.get(), *snapshot.get());
}

if (s)
dependency_graph_success = submit_github_dependency_graph_snapshot(
args.github_server_url, *github_token, *github_repository, *snapshot);
if (dependency_graph_success)
{
msg::println(msgDependencyGraphSuccess);
}
Expand All @@ -219,7 +212,7 @@ namespace vcpkg
msg::println(msgDependencyGraphFailure);
}
}
get_global_metrics_collector().track_bool(BoolMetric::DependencyGraphSuccess, s);
get_global_metrics_collector().track_bool(BoolMetric::DependencyGraphSuccess, dependency_graph_success);
}

// currently (or once) installed specifications
Expand Down

0 comments on commit 10dce58

Please sign in to comment.