Skip to content

Commit afaba83

Browse files
committed
Add Heroku buildpack with tests
Signed-off-by: NucleonGodX <racerpro41@gmail.com>
1 parent c1c2424 commit afaba83

File tree

6 files changed

+427
-32
lines changed

6 files changed

+427
-32
lines changed

src/packagedcode/buildpack.py

+59-32
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,55 @@ def parse(cls, location, package_only=False):
3434
api_version = data.get("api")
3535
buildpack = data.get("buildpack", {})
3636
if not buildpack:
37-
return # Skip files missing required fields
37+
return
3838

39+
name = buildpack.get("name")
40+
if not name:
41+
return
42+
43+
# Initialize common package data
44+
package_data = dict(
45+
datasource_id=cls.datasource_id,
46+
type=cls.default_package_type,
47+
name=name,
48+
version="unknown",
49+
description=None,
50+
homepage_url=None,
51+
keywords=[],
52+
declared_license_expression=None,
53+
dependencies=[],
54+
)
55+
56+
# Handle Paketo-specific fields if present
57+
if "api" in data:
58+
cls.handle_paketo_buildpack(data, buildpack, package_data)
59+
60+
# Handle Heroku-specific fields if present
61+
elif "publish" in data and "Ignore" in data["publish"]:
62+
cls.handle_heroku_buildpack(data, buildpack, package_data)
63+
64+
yield models.PackageData.from_data(package_data, package_only)
65+
66+
@staticmethod
67+
def handle_paketo_buildpack(data, buildpack, package_data):
3968
buildpack_id = buildpack.get("id")
40-
buildpack_version = buildpack.get("version", "unknown")
41-
buildpack_name = buildpack.get("name")
69+
if buildpack_id:
70+
package_data["extra_data"] = {"id": buildpack_id}
4271

43-
if not (api_version and buildpack_id and buildpack_version and buildpack_name):
44-
return # Skip incomplete data
72+
package_data.update({
73+
"version": buildpack.get("version", "unknown"),
74+
"description": buildpack.get("description"),
75+
"homepage_url": buildpack.get("homepage"),
76+
"keywords": buildpack.get("keywords", []),
77+
})
4578

46-
# Optional fields
47-
description = buildpack.get("description")
48-
homepage_url = buildpack.get("homepage")
4979
licenses = buildpack.get("licenses", [])
50-
keywords = buildpack.get("keywords", [])
51-
52-
# Parse licenses
53-
license_expressions = []
54-
for license_entry in licenses:
55-
license_type = license_entry.get("type")
56-
license_uri = license_entry.get("uri")
57-
if license_type:
58-
license_expressions.append(license_type)
80+
license_expressions = [
81+
license_entry.get("type") for license_entry in licenses if license_entry.get("type")
82+
]
83+
if license_expressions:
84+
package_data["declared_license_expression"] = " AND ".join(license_expressions)
5985

60-
# Parse dependencies from "metadata.dependencies"
6186
dependencies = []
6287
metadata = data.get("metadata", {})
6388
metadata_dependencies = metadata.get("dependencies", [])
@@ -84,7 +109,6 @@ def parse(cls, location, package_only=False):
84109
)
85110
)
86111

87-
# Parse "order" section for additional dependencies
88112
orders = data.get("order", [])
89113
for order in orders:
90114
for group in order.get("group", []):
@@ -100,17 +124,20 @@ def parse(cls, location, package_only=False):
100124
)
101125
)
102126

103-
package_data = dict(
104-
datasource_id=cls.datasource_id,
105-
type=cls.default_package_type,
106-
name=buildpack_name,
107-
version=buildpack_version,
108-
description=description,
109-
homepage_url=homepage_url,
110-
keywords=keywords,
111-
declared_license_expression=" AND ".join(license_expressions) if license_expressions else None,
112-
dependencies=dependencies,
113-
extra_data={"id": buildpack_id}, # Store the id in extra_data
114-
)
127+
package_data["dependencies"] = dependencies
128+
129+
@staticmethod
130+
def handle_heroku_buildpack(data, buildpack, package_data):
131+
publish_section = data.get("publish", {})
132+
if "Ignore" in publish_section:
133+
ignore_files = publish_section["Ignore"].get("files", [])
134+
if ignore_files: # Only add if files are found
135+
package_data["extra_data"] = {"ignore_files": ignore_files}
136+
else:
137+
package_data["extra_data"] = {"ignore_files": []}
138+
else:
139+
package_data["extra_data"] = {"ignore_files": []}
115140

116-
yield models.PackageData.from_data(package_data, package_only)
141+
# Add description for Heroku buildpack
142+
package_data["description"] = f"Heroku buildpack for {buildpack.get('name')}"
143+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[buildpack]
2+
name = "Java"
3+
4+
[publish.Ignore]
5+
files = [
6+
"etc/",
7+
"spec/",
8+
"test/",
9+
".gitignore",
10+
".github/",
11+
"hatchet.json",
12+
"Gemfile",
13+
"Gemfile.lock"
14+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
{
2+
"packages": [
3+
{
4+
"type": "buildpack",
5+
"namespace": null,
6+
"name": "Java",
7+
"version": "unknown",
8+
"qualifiers": {},
9+
"subpath": null,
10+
"primary_language": null,
11+
"description": "Heroku buildpack for Java",
12+
"release_date": null,
13+
"parties": [],
14+
"keywords": [],
15+
"homepage_url": null,
16+
"download_url": null,
17+
"size": null,
18+
"sha1": null,
19+
"md5": null,
20+
"sha256": null,
21+
"sha512": null,
22+
"bug_tracking_url": null,
23+
"code_view_url": null,
24+
"vcs_url": null,
25+
"copyright": null,
26+
"holder": null,
27+
"declared_license_expression": null,
28+
"declared_license_expression_spdx": null,
29+
"license_detections": [],
30+
"other_license_expression": null,
31+
"other_license_expression_spdx": null,
32+
"other_license_detections": [],
33+
"extracted_license_statement": null,
34+
"notice_text": null,
35+
"source_packages": [],
36+
"is_private": false,
37+
"is_virtual": false,
38+
"extra_data": {
39+
"ignore_files": [
40+
"etc/",
41+
"spec/",
42+
"test/",
43+
".gitignore",
44+
".github/",
45+
"hatchet.json",
46+
"Gemfile",
47+
"Gemfile.lock"
48+
]
49+
},
50+
"repository_homepage_url": null,
51+
"repository_download_url": null,
52+
"api_data_url": null,
53+
"package_uid": "pkg:buildpack/Java@unknown?uuid=d332d23b-1b4e-415a-96a8-141416e7e7f2",
54+
"datafile_paths": [
55+
"buildpack.toml"
56+
],
57+
"datasource_ids": [
58+
"buildpack_toml"
59+
],
60+
"purl": "pkg:buildpack/Java@unknown"
61+
}
62+
],
63+
"dependencies": [],
64+
"files": [
65+
{
66+
"path": "buildpack.toml",
67+
"type": "file",
68+
"package_data": [
69+
{
70+
"type": "buildpack",
71+
"namespace": null,
72+
"name": "Java",
73+
"version": "unknown",
74+
"qualifiers": {},
75+
"subpath": null,
76+
"primary_language": null,
77+
"description": "Heroku buildpack for Java",
78+
"release_date": null,
79+
"parties": [],
80+
"keywords": [],
81+
"homepage_url": null,
82+
"download_url": null,
83+
"size": null,
84+
"sha1": null,
85+
"md5": null,
86+
"sha256": null,
87+
"sha512": null,
88+
"bug_tracking_url": null,
89+
"code_view_url": null,
90+
"vcs_url": null,
91+
"copyright": null,
92+
"holder": null,
93+
"declared_license_expression": null,
94+
"declared_license_expression_spdx": null,
95+
"license_detections": [],
96+
"other_license_expression": null,
97+
"other_license_expression_spdx": null,
98+
"other_license_detections": [],
99+
"extracted_license_statement": null,
100+
"notice_text": null,
101+
"source_packages": [],
102+
"file_references": [],
103+
"is_private": false,
104+
"is_virtual": false,
105+
"extra_data": {
106+
"ignore_files": [
107+
"etc/",
108+
"spec/",
109+
"test/",
110+
".gitignore",
111+
".github/",
112+
"hatchet.json",
113+
"Gemfile",
114+
"Gemfile.lock"
115+
]
116+
},
117+
"dependencies": [],
118+
"repository_homepage_url": null,
119+
"repository_download_url": null,
120+
"api_data_url": null,
121+
"datasource_id": "buildpack_toml",
122+
"purl": "pkg:buildpack/Java@unknown"
123+
}
124+
],
125+
"for_packages": [
126+
"pkg:buildpack/Java@unknown?uuid=d332d23b-1b4e-415a-96a8-141416e7e7f2"
127+
],
128+
"scan_errors": []
129+
}
130+
]
131+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[buildpack]
2+
name = "PHP"
3+
4+
[publish.Ignore]
5+
files = [
6+
".github/",
7+
".gitignore",
8+
".rspec_parallel",
9+
"support/build/",
10+
"support/devcenter/",
11+
"test/",
12+
"Gemfile",
13+
"Gemfile.lock",
14+
"hatchet.json",
15+
"hatchet.lock",
16+
"requirements.txt",
17+
]

0 commit comments

Comments
 (0)