forked from openshift-helm-charts/development
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize the Submission data structure
This is the first step towards solving openshift-helm-charts#296. The code added by this commit is currently *not* put in use in the certification pipeline and is a partial duplicate of the "checkprcontent" package. The Submission data structure can collect and run early validation checks on all information concerning a user's submission (i.e. a GitHub Pull Request). In its current state, given an api_url (link to a given PR), it can collect the list of modified files, and extract the category, organization, chart's name and version, as well as running basic checks, such as SemVer compatibility of the provided version. This commit also implements a custom JSON Serializer / Deserializer. The Submission object can therefore easily be dumped to / read from a file. Such file can later be an GitHub workflow artifact, populated in a future "pre-check" job and read in subsequent jobs. Signed-off-by: Matthias Goerens <mgoerens@redhat.com>
- Loading branch information
Showing
7 changed files
with
838 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Contains the logic to serialize / deserialize a Submission object to / from JSON. | ||
A pair of custom JSONEncoder / JSONDecoder is required due to the fact that the Submission class | ||
contains nested classes. | ||
""" | ||
|
||
import copy | ||
import json | ||
|
||
from precheck import submission | ||
|
||
|
||
class SubmissionEncoder(json.JSONEncoder): | ||
def default(self, obj): | ||
if isinstance(obj, submission.Submission): | ||
obj_dict = copy.deepcopy(obj.__dict__) | ||
obj_dict["chart"] = obj_dict["chart"].__dict__ | ||
obj_dict["report"] = obj_dict["report"].__dict__ | ||
obj_dict["source"] = obj_dict["source"].__dict__ | ||
obj_dict["tarball"] = obj_dict["tarball"].__dict__ | ||
return obj_dict | ||
|
||
return json.JSONEncoder.default(self, obj) | ||
|
||
|
||
class SubmissionDecoder(json.JSONDecoder): | ||
def __init__(self, *args, **kwargs): | ||
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs) | ||
|
||
def object_hook(self, dct): | ||
if "chart" in dct: | ||
chart_obj = submission.Chart(**dct["chart"]) | ||
report_obj = submission.Report(**dct["report"]) | ||
source_obj = submission.Source(**dct["source"]) | ||
tarball_obj = submission.Tarball(**dct["tarball"]) | ||
|
||
to_merge_dct = { | ||
"chart": chart_obj, | ||
"report": report_obj, | ||
"source": source_obj, | ||
"tarball": tarball_obj, | ||
} | ||
|
||
new_dct = dct | to_merge_dct | ||
return submission.Submission(**new_dct) | ||
return dct |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import json | ||
|
||
from precheck import serializer | ||
from precheck import submission | ||
|
||
submission_json = """ | ||
{ | ||
"api_url": "https://api.github.com/repos/openshift-helm-charts/charts/pulls/1", | ||
"modified_files": ["charts/partners/acme/awesome/1.42.0/report.yaml"], | ||
"chart": { | ||
"category": "partners", | ||
"organization": "acme", | ||
"name": "awesome", | ||
"version": "1.42.0" | ||
}, | ||
"report": { | ||
"found": true, | ||
"signed": false, | ||
"path": "charts/partners/acme/awesome/1.42.0/report.yaml" | ||
}, | ||
"source": { | ||
"found": false, | ||
"path": null | ||
}, | ||
"tarball": { | ||
"found": false, | ||
"path": null, | ||
"provenance": null | ||
}, | ||
"modified_owners": [], | ||
"modified_unknown": [] | ||
} | ||
""" | ||
|
||
|
||
def sanitize_json_string(json_string: str): | ||
"""Remove the newlines from the JSON string. This is done by | ||
loading and dumping the string representation of the JSON object. | ||
Goal is to allow comparison with other JSON string. | ||
""" | ||
json_dict = json.loads(json_string) | ||
return json.dumps(json_dict) | ||
|
||
|
||
def test_submission_serializer(): | ||
s = json.loads(submission_json, cls=serializer.SubmissionDecoder) | ||
|
||
assert isinstance(s, submission.Submission) | ||
assert ( | ||
s.api_url == "https://api.github.com/repos/openshift-helm-charts/charts/pulls/1" | ||
) | ||
assert "charts/partners/acme/awesome/1.42.0/report.yaml" in s.modified_files | ||
assert s.chart.category == "partners" | ||
assert s.chart.organization == "acme" | ||
assert s.chart.name == "awesome" | ||
assert s.chart.version == "1.42.0" | ||
assert s.report.found | ||
assert not s.report.signed | ||
assert s.report.path == "charts/partners/acme/awesome/1.42.0/report.yaml" | ||
assert not s.source.found | ||
assert not s.source.path | ||
assert not s.tarball.found | ||
assert not s.tarball.path | ||
assert not s.tarball.provenance | ||
|
||
|
||
def test_submission_deserializer(): | ||
s = submission.Submission( | ||
api_url="https://api.github.com/repos/openshift-helm-charts/charts/pulls/1", | ||
modified_files=["charts/partners/acme/awesome/1.42.0/report.yaml"], | ||
chart=submission.Chart( | ||
category="partners", | ||
organization="acme", | ||
name="awesome", | ||
version="1.42.0", | ||
), | ||
report=submission.Report( | ||
found=True, | ||
signed=False, | ||
path="charts/partners/acme/awesome/1.42.0/report.yaml", | ||
), | ||
source=submission.Source( | ||
found=False, | ||
path=None, | ||
), | ||
tarball=submission.Tarball( | ||
found=False, | ||
path=None, | ||
provenance=None, | ||
), | ||
) | ||
|
||
assert serializer.SubmissionEncoder().encode(s) == sanitize_json_string( | ||
submission_json | ||
) |
Oops, something went wrong.