Skip to content

Commit

Permalink
Initialize the Submission data structure
Browse files Browse the repository at this point in the history
This is the first step towards solving #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 <[email protected]>
  • Loading branch information
mgoerens committed Mar 22, 2024
1 parent af6cbb5 commit f5836a1
Show file tree
Hide file tree
Showing 7 changed files with 838 additions and 1 deletion.
1 change: 1 addition & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pytest-forked==1.3.0
pytest-xdist==2.4.0
PyYAML==6.0.1
requests==2.26.0
responses==0.23.1
retrying==1.3.3
semantic-version==2.8.5
semver==2.13.0
Expand Down
2 changes: 1 addition & 1 deletion scripts/src/checkprcontent/checkpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_file_match_compiled_patterns():

pattern = re.compile(base + r"/.*")
reportpattern = re.compile(base + r"/report.yaml")
tarballpattern = re.compile(base + r"/(.*\.tgz$)")
tarballpattern = re.compile(base + r"/(.*\.tgz)")
return pattern, reportpattern, tarballpattern


Expand Down
Empty file.
47 changes: 47 additions & 0 deletions scripts/src/precheck/serializer.py
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
95 changes: 95 additions & 0 deletions scripts/src/precheck/serializer_test.py
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
)
Loading

0 comments on commit f5836a1

Please sign in to comment.