Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/vcpkg/base/downloads.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ namespace vcpkg
const std::string& github_repository,
const Json::Object& snapshot);

// Builds the dependency graph snapshots endpoint used for GitHub submission.
std::string github_dependency_graph_snapshots_uri(const Optional<std::string>& maybe_github_server_url,
StringView github_repository);

std::vector<int> url_heads(DiagnosticContext& context, View<std::string> urls, View<std::string> headers);

struct AssetCachingSettings
Expand Down
26 changes: 26 additions & 0 deletions src/vcpkg-test/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ TEST_CASE ("url_encode_spaces", "[downloads]")
"https://example.com/a%20%20space/b?query=value&query2=value2");
}

TEST_CASE ("github_dependency_graph_snapshots_uri", "[downloads]")
{
REQUIRE(github_dependency_graph_snapshots_uri(nullopt, "owner/repo") ==
"https://api.github.com/repos/owner/repo/dependency-graph/snapshots");

REQUIRE(github_dependency_graph_snapshots_uri(Optional<std::string>{"https://github.com"}, "owner/repo") ==
"https://api.github.com/repos/owner/repo/dependency-graph/snapshots");

REQUIRE(github_dependency_graph_snapshots_uri(Optional<std::string>{"https://github.com/"}, "owner/repo") ==
"https://api.github.com/repos/owner/repo/dependency-graph/snapshots");

REQUIRE(github_dependency_graph_snapshots_uri(Optional<std::string>{"https://github.example.com"}, "owner/repo") ==
"https://github.example.com/api/v3/repos/owner/repo/dependency-graph/snapshots");

REQUIRE(github_dependency_graph_snapshots_uri(Optional<std::string>{"https://github.example.com/"}, "owner/repo") ==
"https://github.example.com/api/v3/repos/owner/repo/dependency-graph/snapshots");

REQUIRE(github_dependency_graph_snapshots_uri(Optional<std::string>{"https://user:pass@github.example.com:443"},
"owner/repo") ==
"https://user:pass@github.example.com:443/api/v3/repos/owner/repo/dependency-graph/snapshots");

REQUIRE(
github_dependency_graph_snapshots_uri(Optional<std::string>{"https://github.com"}, "owner/repo with space") ==
"https://api.github.com/repos/owner/repo%20with%20space/dependency-graph/snapshots");
}

/*
* To run this test:
* - Set environment variables VCPKG_TEST_AZBLOB_URL and VCPKG_TEST_AZBLOB_SAS.
Expand Down
48 changes: 35 additions & 13 deletions src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,7 @@ namespace vcpkg
const std::string& github_repository,
const Json::Object& snapshot)
{
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));
const auto uri = github_dependency_graph_snapshots_uri(maybe_github_server_url, github_repository);

CurlEasyHandle handle;
CURL* curl = handle.get();
Expand Down Expand Up @@ -334,6 +322,40 @@ namespace vcpkg
return response_code >= 200 && response_code < 300;
}

std::string github_dependency_graph_snapshots_uri(const Optional<std::string>& maybe_github_server_url,
StringView github_repository)
{
std::string uri;
constexpr StringLiteral github_com_url = "https://github.com";
constexpr StringLiteral api_github_com_url = "https://api.github.com";
if (auto github_server_url = maybe_github_server_url.get())
{
StringView normalized_server_url = *github_server_url;
if (!normalized_server_url.empty() && normalized_server_url[normalized_server_url.size() - 1] == '/')
{
normalized_server_url = normalized_server_url.substr(0, normalized_server_url.size() - 1);
}

if (normalized_server_url == github_com_url)
{
uri.assign(api_github_com_url.data(), api_github_com_url.size());
}
else
{
uri.assign(normalized_server_url.data(), normalized_server_url.size());
uri.append("/api/v3");
}
}
else
{
uri.assign(api_github_com_url.data(), api_github_com_url.size());
}

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

static size_t read_file_callback(char* buffer, size_t size, size_t nitems, void* param)
{
auto* file = static_cast<ReadFilePointer*>(param);
Expand Down