Skip to content

Commit

Permalink
Copyonwrite/344/implement automatic detection of issues which can be …
Browse files Browse the repository at this point in the history
…closed (#430)

* Add dependency to gqlgen for targets test-db, test-app and test-e2e

* Add ScannerRun table

* Add/modify tables for ScannerRun and ScannerRunIssueTracker

* Add uuid to ScannerRun

* Add 'tag' to ScannerRun

* Use size of 255 for varchar fields instead of 256

* Make UUID unique

* Align SQL schema and ScannerRun with project conventions

* Add serialization/deserialization from/to ScannerRun

* Adapt ScannerRun table to heureka naming conventions

* Create an instance of ScannerRun in the database

* Fix persistance for ScannerRun and add entities for app layer

* Add skeleton for ScannerRun on the app layer

* First test for ScannerRun create

* Add licence header

* Add first draft of graphql api definition for ScannerRun

* Add supporting code to create a new ScannerRun via GraphQL

* Models for graphql for creating ScannerRun entities

* Add e2e test to create a ScannerRun

* Fix e2e test for creating ScannerRun

* Automatic application of license header

---------

Co-authored-by: License Bot <[email protected]>
  • Loading branch information
copyonwrite and License Bot authored Dec 20, 2024
1 parent 2eb43f0 commit 113e83d
Show file tree
Hide file tree
Showing 21 changed files with 581 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ GINKGO := go run github.com/onsi/ginkgo/v2/ginkgo
test-all: mockery gqlgen
$(GINKGO) -r

test-e2e:
test-e2e: gqlgen
$(GINKGO) -r internal/e2e

test-app:
test-app: gqlgen
$(GINKGO) -r internal/app

test-db:
test-db: gqlgen
$(GINKGO) -r internal/database/mariadb

fmt:
Expand Down
27 changes: 27 additions & 0 deletions internal/api/graphql/graph/model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,33 @@ func NewIssueEntity(issue *IssueInput) entity.Issue {
}
}

func NewScannerRunEntity(sr *ScannerRunInput) entity.ScannerRun {

return entity.ScannerRun{
RunID: -1,
UUID: lo.FromPtr(sr.UUID),
Tag: lo.FromPtr(sr.Tag),
Completed: false,
StartRun: time.Now(),
EndRun: time.Now(),
}
}

func NewScannerRun(sr *entity.ScannerRun) ScannerRun {
startRun := sr.StartRun.Format(time.RFC3339)
endRun := sr.EndRun.Format(time.RFC3339)

return ScannerRun{
ID: fmt.Sprintf("%d", sr.RunID),
UUID: sr.UUID,
Tag: sr.Tag,
Completed: sr.Completed,
StartRun: startRun,
EndRun:
endRun,
}
}

func NewIssueMatch(im *entity.IssueMatch) IssueMatch {
status := IssueMatchStatusValue(im.Status.String())
targetRemediationDate := im.TargetRemediationDate.Format(time.RFC3339)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

mutation ($input: ScannerRunInput!) {
createScannerRun (
input: $input
) {
id
tag
uuid
}
}
10 changes: 10 additions & 0 deletions internal/api/graphql/graph/resolver/mutation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/api/graphql/graph/schema/mutation.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ type Mutation {
removeServiceFromActivity(activityId: ID!, serviceId: ID!): Activity!
addIssueToActivity(activityId: ID!, issueId: ID!): Activity!
removeIssueFromActivity(activityId: ID!, issueId: ID!): Activity!

createScannerRun(input: ScannerRunInput!): ScannerRun!
}
28 changes: 28 additions & 0 deletions internal/api/graphql/graph/schema/scanner_run.graphqls
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
# SPDX-License-Identifier: Apache-2.0

type ScannerRun implements Node {
id: ID!
uuid: String!
tag: String!
start_run: DateTime!
end_run: DateTime!
completed: Boolean!
metadata: Metadata
}

input ScannerRunInput {
uuid: String
tag: String
}

type ScannerRunConnection implements Connection {
totalCount: Int!
edges: [ScannerRun]
pageInfo: PageInfo
}

type ScannerRunEdge implements Edge {
node: ScannerRun!
cursor: String
}
11 changes: 7 additions & 4 deletions internal/app/heureka.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cloudoperators/heureka/internal/app/issue_match_change"
"github.com/cloudoperators/heureka/internal/app/issue_repository"
"github.com/cloudoperators/heureka/internal/app/issue_variant"
"github.com/cloudoperators/heureka/internal/app/scanner_run"
"github.com/cloudoperators/heureka/internal/app/service"
"github.com/cloudoperators/heureka/internal/app/severity"
"github.com/cloudoperators/heureka/internal/app/support_group"
Expand All @@ -26,15 +27,16 @@ import (

type HeurekaApp struct {
activity.ActivityHandler
component.ComponentHandler
component_instance.ComponentInstanceHandler
component_version.ComponentVersionHandler
component.ComponentHandler
evidence.EvidenceHandler
issue.IssueHandler
issue_match.IssueMatchHandler
issue_match_change.IssueMatchChangeHandler
issue_match.IssueMatchHandler
issue_repository.IssueRepositoryHandler
issue_variant.IssueVariantHandler
issue.IssueHandler
scanner_run.ScannerRunHandler
service.ServiceHandler
severity.SeverityHandler
support_group.SupportGroupHandler
Expand All @@ -57,10 +59,11 @@ func NewHeurekaApp(db database.Database) *HeurekaApp {
ComponentVersionHandler: component_version.NewComponentVersionHandler(db, er),
EvidenceHandler: evidence.NewEvidenceHandler(db, er),
IssueHandler: issue.NewIssueHandler(db, er),
IssueMatchHandler: issue_match.NewIssueMatchHandler(db, er, sh),
IssueMatchChangeHandler: issue_match_change.NewIssueMatchChangeHandler(db, er),
IssueMatchHandler: issue_match.NewIssueMatchHandler(db, er, sh),
IssueRepositoryHandler: rh,
IssueVariantHandler: ivh,
ScannerRunHandler: scanner_run.NewScannerRunHandler(db, er),
ServiceHandler: service.NewServiceHandler(db, er),
SeverityHandler: sh,
SupportGroupHandler: support_group.NewSupportGroupHandler(db, er),
Expand Down
22 changes: 12 additions & 10 deletions internal/app/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,30 @@ import (
"github.com/cloudoperators/heureka/internal/app/issue_match_change"
"github.com/cloudoperators/heureka/internal/app/issue_repository"
"github.com/cloudoperators/heureka/internal/app/issue_variant"
"github.com/cloudoperators/heureka/internal/app/scanner_run"
"github.com/cloudoperators/heureka/internal/app/service"
"github.com/cloudoperators/heureka/internal/app/severity"
"github.com/cloudoperators/heureka/internal/app/support_group"
"github.com/cloudoperators/heureka/internal/app/user"
)

type Heureka interface {
issue.IssueHandler
activity.ActivityHandler
service.ServiceHandler
user.UserHandler
component.ComponentHandler
component_instance.ComponentInstanceHandler
component_version.ComponentVersionHandler
support_group.SupportGroupHandler
issue_variant.IssueVariantHandler
issue_repository.IssueRepositoryHandler
issue_match.IssueMatchHandler
issue_match_change.IssueMatchChangeHandler
severity.SeverityHandler
component.ComponentHandler
evidence.EvidenceHandler
issue_match_change.IssueMatchChangeHandler
issue_match.IssueMatchHandler
issue_match.IssueMatchHandler
issue_repository.IssueRepositoryHandler
issue_variant.IssueVariantHandler
issue.IssueHandler
scanner_run.ScannerRunHandler
service.ServiceHandler
severity.SeverityHandler
support_group.SupportGroupHandler
user.UserHandler

Shutdown() error
}
21 changes: 21 additions & 0 deletions internal/app/scanner_run/scanner_run_events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

package scanner_run

import (
"github.com/cloudoperators/heureka/internal/app/event"
"github.com/cloudoperators/heureka/internal/entity"
)

const (
CreateScannerRunEventName event.EventName = "CreateScannerRun"
)

type CreateScannerRunEvent struct {
ScannerRun *entity.ScannerRun
}

func (csr *CreateScannerRunEvent) Name() event.EventName {
return CreateScannerRunEventName
}
41 changes: 41 additions & 0 deletions internal/app/scanner_run/scanner_run_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

package scanner_run

import (
"github.com/cloudoperators/heureka/internal/app/event"
"github.com/cloudoperators/heureka/internal/database"
"github.com/cloudoperators/heureka/internal/entity"
)

type scannerRunHandler struct {
database database.Database
eventRegistry event.EventRegistry
}

func NewScannerRunHandler(db database.Database, er event.EventRegistry) ScannerRunHandler {
return &scannerRunHandler{
database: db,
eventRegistry: er,
}
}

type ScannerRunHandlerError struct {
msg string
}

func (srh *scannerRunHandler) CreateScannerRun(sr *entity.ScannerRun) (*entity.ScannerRun, error) {
_, err := srh.database.CreateScannerRun(sr)

if err != nil {
return nil, &ScannerRunHandlerError{msg: "Error creating scanner run"}
}

srh.eventRegistry.PushEvent(&CreateScannerRunEvent{sr})
return sr, nil
}

func (srhe *ScannerRunHandlerError) Error() string {
return srhe.msg
}
10 changes: 10 additions & 0 deletions internal/app/scanner_run/scanner_run_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

package scanner_run

import "github.com/cloudoperators/heureka/internal/entity"

type ScannerRunHandler interface {
CreateScannerRun(*entity.ScannerRun) (*entity.ScannerRun, error)
}
49 changes: 49 additions & 0 deletions internal/app/scanner_run/scanner_run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors
// SPDX-License-Identifier: Apache-2.0

package scanner_run

import (
"testing"

"github.com/cloudoperators/heureka/internal/app/event"
"github.com/cloudoperators/heureka/internal/entity/test"

"github.com/cloudoperators/heureka/internal/entity"
"github.com/cloudoperators/heureka/internal/mocks"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestServiceHandler(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Service Service Test Suite")
}

var er event.EventRegistry

var _ = BeforeSuite(func() {
db := mocks.NewMockDatabase(GinkgoT())
er = event.NewEventRegistry(db)
})

var sre *entity.ScannerRun

var _ = Describe("When creating ScannerRun", Label("app", "CreateScannerRun"), func() {
var (
db *mocks.MockDatabase
scannerRunHandler ScannerRunHandler
)

BeforeEach(func() {
db = mocks.NewMockDatabase(GinkgoT())
sre = test.NewFakeScannerRunEntity()
})

It("creates a scannerrun", func() {
db.On("CreateScannerRun", sre).Return(sre, nil)

scannerRunHandler = NewScannerRunHandler(db, er)
scannerRunHandler.CreateScannerRun(sre)
})
})
2 changes: 1 addition & 1 deletion internal/app/user/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type UserHandlerError struct {
}

func (e *UserHandlerError) Error() string {
return fmt.Sprintf("ServiceHandlerError: %s", e.msg)
return fmt.Sprintf("UserHandlerError: %s", e.msg)
}

func NewUserHandlerError(msg string) *UserHandlerError {
Expand Down
2 changes: 2 additions & 0 deletions internal/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,7 @@ type Database interface {
UpdateComponentVersion(*entity.ComponentVersion) error
DeleteComponentVersion(int64) error

CreateScannerRun(*entity.ScannerRun) (*entity.ScannerRun, error)

CloseConnection() error
}
37 changes: 37 additions & 0 deletions internal/database/mariadb/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func GetInt64Value(v sql.NullInt64) int64 {
}
}

func GetBoolValue(v sql.NullBool) bool {
if v.Valid {
return v.Bool
} else {
return false
}
}

func GetInt16Value(v sql.NullInt16) int16 {
if v.Valid {
return v.Int16
Expand Down Expand Up @@ -972,3 +980,32 @@ type IssueRepositoryServiceRow struct {
DeletedAt sql.NullTime `db:"issuerepositoryservice_deleted_at" json:"deleted_at,omitempty"`
UpdatedAt sql.NullTime `db:"issuerepositoryservice_updated_at" json:"updated_at"`
}

type ScannerRunRow struct {
RunID sql.NullInt64 `db:"scannerrun_run_id"`
UUID sql.NullString `db:"scannerrun_uuid"`
Tag sql.NullString `db:"scannerrun_tag"`
StartRun sql.NullTime `db:"scannerrun_start_run"`
EndRun sql.NullTime `db:"scannerrun_end_run"`
IsCompleted sql.NullBool `db:"scannerrun_is_completed"`
}

func (srr *ScannerRunRow) AsScannerRun() entity.ScannerRun {
return entity.ScannerRun{
RunID: GetInt64Value(srr.RunID),
UUID: GetStringValue(srr.UUID),
Tag: GetStringValue(srr.Tag),
StartRun: GetTimeValue(srr.StartRun),
EndRun: GetTimeValue(srr.EndRun),
Completed: GetBoolValue(srr.IsCompleted),
}
}

func (srr *ScannerRunRow) FromScannerRun(sr *entity.ScannerRun) {
srr.RunID = sql.NullInt64{Int64: sr.RunID, Valid: true}
srr.UUID = sql.NullString{String: sr.UUID, Valid: true}
srr.Tag = sql.NullString{String: sr.Tag, Valid: true}
srr.StartRun = sql.NullTime{Time: sr.StartRun, Valid: true}
srr.EndRun = sql.NullTime{Time: sr.EndRun, Valid: true}
srr.IsCompleted = sql.NullBool{Bool: sr.Completed, Valid: true}
}
Loading

0 comments on commit 113e83d

Please sign in to comment.