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

👻 Add questionnare required field to assessment #588

Merged

Conversation

ibolton336
Copy link
Member

@ibolton336 ibolton336 commented Jan 5, 2024

This is needed to aid UI in computing status for an assessment given a questionnaire is marked as not required. If an assessment exists for an archived questionnaire on an app or archetype, the assessment status is no longer valid & the assessment will need to be filtered from status consideration.

https://issues.redhat.com/browse/MTA-1956

Copy link
Collaborator

@mansam mansam left a comment

Choose a reason for hiding this comment

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

Good work! I've got some comments that might let you simplify your approach.

api/application.go Outdated Show resolved Hide resolved
api/application.go Outdated Show resolved Hide resolved
api/archetype.go Outdated Show resolved Hide resolved
api/archetype.go Outdated Show resolved Hide resolved
api/assessment.go Outdated Show resolved Hide resolved
api/assessment.go Outdated Show resolved Hide resolved
api/assessment.go Outdated Show resolved Hide resolved
@ibolton336 ibolton336 force-pushed the add-required-status-to-assessment branch 4 times, most recently from a3bbd88 to 8f38878 Compare January 12, 2024 16:48
@ibolton336 ibolton336 requested a review from mansam January 12, 2024 16:48
@@ -994,6 +994,12 @@ func (h ApplicationHandler) AssessmentList(ctx *gin.Context) {
for i := range assessments {
r := Assessment{}
r.With(&assessments[i])
if assessments[i].Questionnaire.ID != 0 {
Copy link
Member Author

Choose a reason for hiding this comment

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

@mansam Wasn't sure if this was the best way here- When trying

Cannot convert 'nil' to type 'Questionnaire'

I was seeing this type error. Is using the ID here acceptable?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're getting that error about converting nil because Assessment.Questionnaire is a struct instead of a struct pointer. Because it's a struct and not a struct pointer, when the relationship isn't preloaded the struct will be present but all the fields will be zero-valued. So checking if the Questionnaire struct's ID field is 0 like that is a reasonable way to check if it's been preloaded. (Probably that field should be changed to a struct pointer like the rest of the model relationships so that it's more obvious when it hasn't been loaded.)

There are two improvements we can make here. One is that instead of doing this check in the handler logic in all the various places where we need to do it, we can move the check and the assignment into the Assessment.With method.

The second is that the zero-value of a boolean is false, so we can take advantage of that to do away with the check. assessment.Required is going to default to false anyway if you don't assign to it because the questionnaire isn't loaded, and the unloaded questionnaire's Required will default to false, so the assignment only matters if the Questionnaire is loaded and and required. So these two expressions are equivalent:

if assessment.Questionnaire.ID != 0 {
    assessment.Required = assessment.Questionnaire.Required
}
assessment.Required = assessment.Questionnaire.Required

(If we later fix assessement.Questionnaire to be a struct pointer, then we'll need to add a check for nil, but for now this works well.)

Status string `json:"status"`
Thresholds assessment.Thresholds `json:"thresholds"`
RiskMessages assessment.RiskMessages `json:"riskMessages" yaml:"riskMessages"`
QuestionnaireRequired bool `json:"questionnaireRequired"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

When you add a field with a camelCased name, be sure to add both a json and a yaml field tag with the camelCased spelling. (see the RiskMessages field on the line above). This prevents a problem where the yaml encoder will emit it from the api as camelcased but the strict decoder will not recognize it because it expects camelCased.

nitpick: Lets just call this Required for simplicity and for symmetry with the other fields that are ultimately inherited from the questionnaire.

@ibolton336 ibolton336 force-pushed the add-required-status-to-assessment branch from 8f38878 to 28fde12 Compare January 15, 2024 21:43
@ibolton336 ibolton336 force-pushed the add-required-status-to-assessment branch from 28fde12 to 980545a Compare January 15, 2024 21:45
@ibolton336 ibolton336 requested a review from mansam January 15, 2024 21:46
Copy link
Collaborator

@mansam mansam left a comment

Choose a reason for hiding this comment

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

LGTM!

@ibolton336 ibolton336 merged commit 2552233 into konveyor:main Jan 15, 2024
11 checks passed
@ibolton336 ibolton336 deleted the add-required-status-to-assessment branch January 15, 2024 22:41
ibolton336 added a commit to ibolton336/tackle2-hub that referenced this pull request Jan 16, 2024
This is needed to aid UI in computing status for an assessment given a
questionnaire is marked as not required. If an assessment exists for an
archived questionnaire on an app or archetype, the assessment status is
no longer valid & the assessment will need to be filtered from status
consideration.

https://issues.redhat.com/browse/MTA-1956

Signed-off-by: ibolton336 <[email protected]>
Signed-off-by: ibolton336 <[email protected]>
ibolton336 added a commit to ibolton336/tackle2-hub that referenced this pull request Jan 16, 2024
This is needed to aid UI in computing status for an assessment given a
questionnaire is marked as not required. If an assessment exists for an
archived questionnaire on an app or archetype, the assessment status is
no longer valid & the assessment will need to be filtered from status
consideration.

https://issues.redhat.com/browse/MTA-1956

Signed-off-by: ibolton336 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants