Skip to content

Commit

Permalink
rename the url attribute to instance and add it to nix flake info
Browse files Browse the repository at this point in the history
  • Loading branch information
kloenk committed Jun 4, 2020
1 parent ab54031 commit 3de8554
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/libexpr/flake/flakeref.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const static std::string subDirRegex = subDirElemRegex + "(?:/" + subDirElemRege

std::string FlakeRef::to_string() const
{
auto instance = maybeGetStrAttr(input.toAttrs(), "instance");
auto url = input.toURL();
if (subdir != "")
url.query.insert_or_assign("dir", subdir);

return url.to_string();
}

Expand Down
16 changes: 8 additions & 8 deletions src/libfetchers/github.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct GitArchiveInputScheme : InputScheme
throw BadURL("URL '%s' contains multiple branch/tag names", url.url);
ref = value;
}
else if (name == "url") {
else if (name == "instance") {
if (!std::regex_match(value, urlRegex))
throw BadURL("URL '%s' contains an invalid instance url", url.url);
host_url = value;
Expand All @@ -67,7 +67,7 @@ struct GitArchiveInputScheme : InputScheme
input.attrs.insert_or_assign("repo", path[1]);
if (rev) input.attrs.insert_or_assign("rev", rev->gitRev());
if (ref) input.attrs.insert_or_assign("ref", *ref);
if (host_url) input.attrs.insert_or_assign("url", *host_url);
if (host_url) input.attrs.insert_or_assign("instance", *host_url);

return input;
}
Expand Down Expand Up @@ -182,7 +182,7 @@ struct GitHubInputScheme : GitArchiveInputScheme

Hash getRevFromRef(nix::ref<Store> store, const Input & input) const override
{
auto host_url = maybeGetStrAttr(input.attrs, "url").value_or("github.com");
auto host_url = maybeGetStrAttr(input.attrs, "instance").value_or("github.com");
auto url = fmt("https://api.%s/repos/%s/%s/commits/%s", // FIXME: check
host_url, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"), *input.getRef());
auto json = nlohmann::json::parse(
Expand All @@ -198,7 +198,7 @@ struct GitHubInputScheme : GitArchiveInputScheme
{
// FIXME: use regular /archive URLs instead? api.github.com
// might have stricter rate limits.
auto host_url = maybeGetStrAttr(input.attrs, "url").value_or("github.com");
auto host_url = maybeGetStrAttr(input.attrs, "instance").value_or("github.com");
auto url = fmt("https://api.%s/repos/%s/%s/tarball/%s", // FIXME: check if this is correct for self hosted instances
host_url, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"),
input.getRev()->to_string(Base16, false));
Expand All @@ -212,7 +212,7 @@ struct GitHubInputScheme : GitArchiveInputScheme

void clone(const Input & input, const Path & destDir) override
{
auto host_url = maybeGetStrAttr(input.attrs, "url").value_or("github.com");
auto host_url = maybeGetStrAttr(input.attrs, "instance").value_or("github.com");
Input::fromURL(fmt("git+ssh://git@%s/%s/%s.git",
host_url, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo")))
.applyOverrides(input.getRef().value_or("master"), input.getRev())
Expand All @@ -226,7 +226,7 @@ struct GitLabInputScheme : GitArchiveInputScheme

Hash getRevFromRef(nix::ref<Store> store, const Input & input) const override
{
auto host_url = maybeGetStrAttr(input.attrs, "url").value_or("gitlab.com");
auto host_url = maybeGetStrAttr(input.attrs, "instance").value_or("gitlab.com");
auto url = fmt("https://%s/api/v4/projects/%s%%2F%s/repository/branches/%s",
host_url, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"), *input.getRef());
auto json = nlohmann::json::parse(
Expand All @@ -241,7 +241,7 @@ struct GitLabInputScheme : GitArchiveInputScheme
std::string getDownloadUrl(const Input & input) const override
{
// FIXME: This endpoint has a rate limit threshold of 5 requests per minute
auto host_url = maybeGetStrAttr(input.attrs, "url").value_or("gitlab.com");
auto host_url = maybeGetStrAttr(input.attrs, "instance").value_or("gitlab.com");
auto url = fmt("https://%s/api/v4/projects/%s%%2F%s/repository/archive.tar.gz?sha=%s",
host_url, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo"),
input.getRev()->to_string(Base16, false));
Expand All @@ -256,7 +256,7 @@ struct GitLabInputScheme : GitArchiveInputScheme

void clone(const Input & input, const Path & destDir) override
{
auto host_url = maybeGetStrAttr(input.attrs, "url").value_or("gitlab.com");
auto host_url = maybeGetStrAttr(input.attrs, "instance").value_or("gitlab.com");
// FIXME: get username somewhere
Input::fromURL(fmt("git+ssh://git@%s/%s/%s.git",
host_url, getStrAttr(input.attrs, "owner"), getStrAttr(input.attrs, "repo")))
Expand Down

0 comments on commit 3de8554

Please sign in to comment.