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

Sast rescoring update #270

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Sast rescoring update #270

wants to merge 10 commits into from

Conversation

TuanAnh17N
Copy link
Collaborator

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Release note:


@TuanAnh17N TuanAnh17N requested a review from a team as a code owner November 27, 2024 15:55
@ocm-ci-robot-0 ocm-ci-robot-0 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) needs/ok-to-test and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Nov 27, 2024
test/test_rescoring.py Outdated Show resolved Hide resolved
test/test_rescoring.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
rescore/utility.py Outdated Show resolved Hide resolved
rescore/utility.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from 79f7529 to 5106da9 Compare December 2, 2024 16:08
@ocm-ci-robot-0 ocm-ci-robot-0 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Dec 2, 2024
@TuanAnh17N TuanAnh17N requested review from zkdev and ccwienk December 2, 2024 16:10
config.py Outdated Show resolved Hide resolved
config.py Outdated Show resolved Hide resolved
config.py Outdated Show resolved Hide resolved
config.py Outdated Show resolved Hide resolved
features/__init__.py Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from 5106da9 to a3c0361 Compare December 10, 2024 19:01
@ocm-ci-robot-0 ocm-ci-robot-0 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Dec 10, 2024
cm06/scan.py Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Show resolved Hide resolved
cm06/scan.py Show resolved Hide resolved
cm06/scan.py Show resolved Hide resolved
cm06/scan.py Show resolved Hide resolved
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from a3c0361 to 2f3b3ba Compare December 11, 2024 16:23
@ocm-ci-robot-0 ocm-ci-robot-0 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Dec 11, 2024
@TuanAnh17N TuanAnh17N requested a review from 8R0WNI3 December 11, 2024 16:24
@ocm-ci-robot-0 ocm-ci-robot-0 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Dec 11, 2024
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
config.py Outdated Show resolved Hide resolved
config.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
return None


def rescore_sast_severity(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function should work for all types of rescoring-rule no?
can we merge / factor out with existing bdba rescoring?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems you decided against my suggestion, which of course is fine, but could you please elaborate what lead to this decision?

looking only at the PR I do not see a good reason against 🤔

test/test_rescoring.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
rescore/model.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from 2f3b3ba to 9895c5f Compare December 16, 2024 14:54
@ocm-ci-robot-0 ocm-ci-robot-0 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Dec 16, 2024
@TuanAnh17N TuanAnh17N requested a review from zkdev December 16, 2024 15:17
cm06/scan.py Outdated Show resolved Hide resolved
rescore/utility.py Outdated Show resolved Hide resolved
rescore/utility.py Outdated Show resolved Hide resolved
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from 9895c5f to 13f4f76 Compare December 16, 2024 16:44
@ocm-ci-robot-0 ocm-ci-robot-0 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Dec 16, 2024
@ocm-ci-robot-0 ocm-ci-robot-0 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Dec 16, 2024
@TuanAnh17N TuanAnh17N requested a review from zkdev December 16, 2024 16:48
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from 13f4f76 to 2bf0120 Compare December 17, 2024 09:10
@ocm-ci-robot-0 ocm-ci-robot-0 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Dec 17, 2024
@ccwienk
Copy link
Collaborator

ccwienk commented Dec 17, 2024

@TuanAnh17N : I assume test-errors stem from dependency against (unreleased) changes in cc-utils? If so, then I suggest to first merge and release change for cc-utils + rerun tests here to be sure.

cm06/scan.py Outdated Show resolved Hide resolved
cm06/scan.py Outdated Show resolved Hide resolved
features/__init__.py Outdated Show resolved Hide resolved
test/test_rescoring.py Outdated Show resolved Hide resolved
@TuanAnh17N TuanAnh17N force-pushed the sast-rescoring-update branch from 2bf0120 to 4ca5d08 Compare December 19, 2024 17:53
@ocm-ci-robot-0 ocm-ci-robot-0 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Dec 19, 2024
sast_config: config.SASTConfig,
component_descriptor_lookup: cnudie.retrieve.ComponentDescriptorLookupById,
) -> list[str]:
if sast_config.component_version:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mv to caller, this function should only get versions from time range -> SoC
also, can we please use a more meaningful function name, e.g. version_from_time_range?

if sast_config.component_version:
return [sast_config.component_version]

version_lookup = lookups.init_version_lookup()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather pass in version-lookup (dependency injection)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also: consistency (why pass-in component-descriptor-lookup, but not version-lookup)

if not (pv := versionutil.parse_to_semver(
version=v,
invalid_semver_ok=False,
)).prerelease and not pv.build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version filtering is good, but I suggest to at least make it configurable via function parameters

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a util-function for that in cc-util's version.py (at least is_final comes to my mind)


versions = [
version for version
in filter_by_date_range(versions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather use built-in filter function

prune_unique=True,
):
if not cnode.component.sources:
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a brief comment explaining why this case is skipped to help our future selves x)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also: why not simply use note_filter.cnudie.iter.Filter.sources?`

all_findings_for_rescoring = []

source_node = cnudie.iter.SourceNode(
path=(cnudie.iter.NodePathEntry(cnode.component),),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather directly iter source nodes (node_filter in cnudie.iter.iter)

also, if constructing source_node manually, make sure to give the full path and not only the parent component

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this context, if is not a good idea to manually construct sourcenodes.

),
time_now=time_now,
)
new_metadata.extend(rescored_metadata)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: list.extend resolves a generator :-)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure - it accepts any iterable (which includes generators)

return None


def rescore_sast_severity(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems you decided against my suggestion, which of course is fine, but could you please elaborate what lead to this decision?

looking only at the PR I do not see a good reason against 🤔

if sast_config.component_version:
return [sast_config.component_version]

version_lookup = lookups.init_version_lookup()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also: consistency (why pass-in component-descriptor-lookup, but not version-lookup)

if not (pv := versionutil.parse_to_semver(
version=v,
invalid_semver_ok=False,
)).prerelease and not pv.build
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a util-function for that in cc-util's version.py (at least is_final comes to my mind)

prune_unique=True,
):
if not cnode.component.sources:
continue
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also: why not simply use note_filter.cnudie.iter.Filter.sources?`

all_findings_for_rescoring = []

source_node = cnudie.iter.SourceNode(
path=(cnudie.iter.NodePathEntry(cnode.component),),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this context, if is not a good idea to manually construct sourcenodes.

),
time_now=time_now,
)
new_metadata.extend(rescored_metadata)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure - it accepts any iterable (which includes generators)

elif rescoring_rule.rescore is rescore.model.Rescore.TO_NONE:
return github.compliance.model.Severity.NONE.name

raise NotImplementedError(rescoring_rule.rescore)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: while we do use NIE for that in our "heritage" codebase, it is actually not intended to be used for this purpose (python-documentation states NIE is intended for being raised by (abstract) methods that were not implemented). rather use ValueError

time_now: datetime.datetime
) -> typing.Generator[dso.model.ArtefactMetadata, None, None]:
for finding in findings:
matching_rule = next(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really do not like this pattern. how about rather doing sth. like

try:
 mr = next(...)
except StopIteration:
  continue

In my opinion, this makes it a lot more obvious what is being done.

) -> typing.Generator[dso.model.ArtefactMetadata, None, None]:
for finding in findings:
matching_rule = next(
matching_sast_rescore_rule(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw: is this function intended to yield more than one rule (judging from the name, I would assume: no)? if not, why not have the function either return a rule or None. This would make it less messy to use.

new_severity = rescore_sast_severity(
rescoring_rule=matching_rule,
)
if finding.data.severity == new_severity:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if severity is an enum (did not check), use is for comparison.

severity=new_severity,
user=user,
matching_rules=[matching_rule.name],
comment='Automatically rescored based on rules.',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include rulename(s) (+ ruleset-name) in comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants