Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include package URL in package_info #1917

Merged
merged 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bazel_dep(name = "bazel_features", version = "1.9.1")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "protobuf", version = "3.19.6", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_go", version = "0.47.0", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_license", version = "0.0.8")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_proto", version = "4.0.0")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
Expand Down
6 changes: 3 additions & 3 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ def gazelle_dependencies(
http_archive,
name = "rules_license",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/0.0.8/rules_license-0.0.8.tar.gz",
"https://github.com/bazelbuild/rules_license/releases/download/0.0.8/rules_license-0.0.8.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/rules_license/releases/download/1.0.0/rules_license-1.0.0.tar.gz",
"https://github.com/bazelbuild/rules_license/releases/download/1.0.0/rules_license-1.0.0.tar.gz",
],
sha256 = "241b06f3097fd186ff468832150d6cc142247dc42a32aaefb56d0099895fd229",
sha256 = "26d4021f6898e23b82ef953078389dd49ac2b5618ac564ade4ef87cced147b38",
)

if go_sdk:
Expand Down
13 changes: 12 additions & 1 deletion internal/go_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ def _go_repository_impl(ctx):
# Do not override a REPO.bazel patched in by users. This also provides a
# way for users to opt out of Gazelle-generated package_info.
repo_file = ctx.path("REPO.bazel")
print("REPO.bazel exists?", repo_file.exists)
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
if not repo_file.exists:
ctx.file("REPO.bazel", """\
repo(default_package_metadata = ["//:gazelle_generated_package_info"])
Expand All @@ -387,8 +388,16 @@ def _generate_package_info(*, importpath, version):
package_name = importpath

# TODO: Consider adding support for custom remotes.
package_url = "https://" + importpath if version else None
package_url = "https://{}".format(importpath) if version else None
package_version = version.removeprefix("v") if version else None
# See specification:
# https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#golang
# scheme:type/namespace/name@version?qualifiers#subpath
purl = "pkg:golang/{namespace_and_name}{version}".format(
alexeagle marked this conversation as resolved.
Show resolved Hide resolved
namespace_and_name = importpath,
version = "@{}".format(version) if version else "",
fmeum marked this conversation as resolved.
Show resolved Hide resolved
)

return """
load("@rules_license//rules:package_info.bzl", "package_info")

Expand All @@ -397,12 +406,14 @@ package_info(
package_name = {package_name},
package_url = {package_url},
package_version = {package_version},
purl = {purl},
visibility = ["//:__subpackages__"],
)
""".format(
package_name = repr(package_name),
package_url = repr(package_url),
package_version = repr(package_version),
purl = repr(purl),
)

go_repository = repository_rule(
Expand Down
2 changes: 1 addition & 1 deletion tests/bcr/go_mod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local_path_override(
bazel_dep(name = "bazel_features", version = "1.14.0")
bazel_dep(name = "protobuf", version = "23.1", repo_name = "my_protobuf")
bazel_dep(name = "rules_go", version = "0.42.0", repo_name = "my_rules_go")
bazel_dep(name = "rules_license", version = "0.0.8")
bazel_dep(name = "rules_license", version = "1.0.0")
bazel_dep(name = "rules_proto", version = "6.0.0-rc2", repo_name = "my_rules_proto")
bazel_dep(name = "rules_testing", version = "0.6.0")

Expand Down
2 changes: 2 additions & 0 deletions tests/bcr/go_mod/tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _test_package_info_impl(env, target):
subject.package_name().equals("github.com/fmeum/dep_on_gazelle")
subject.package_version().equals("1.0.0")
subject.package_url().equals("https://github.com/fmeum/dep_on_gazelle")
subject.purl().equals("pkg:golang/github.com/fmeum/[email protected]")

def _package_info_aspect_impl(_, ctx):
if hasattr(ctx.rule.attr, "applicable_licenses"):
Expand All @@ -51,6 +52,7 @@ _PackageInfoSubjectFactory = struct(
"package_name": subjects.str,
"package_version": subjects.str,
"package_url": subjects.str,
"purl": subjects.str,
},
),
)
Expand Down