Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
23 changes: 23 additions & 0 deletions src/vcpkg-test/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ 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://github.com//api/v3/repos/owner/repo/dependency-graph/snapshots");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this one become api.? (Should it use starts with rather than ==?)


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:[email protected]:443"},
"owner/repo") ==
"https://user:[email protected]: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
42 changes: 29 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,34 @@ 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())
{
if (*github_server_url == github_com_url)
{
uri = api_github_com_url.data();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uri = api_github_com_url.data();
uri.assign(api_github_com_url.data(), api_github_com_url.size());

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
else
{
uri = *github_server_url;
uri.append("/api/v3");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this defend against github_server_url ending with /?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I applied.

}
}
else
{
uri = api_github_com_url.data();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
uri = api_github_com_url.data();
uri.assign(api_github_com_url.data(), api_github_com_url.size());

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

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