Skip to content

Commit

Permalink
✨ Add Target.provider. (#541)
Browse files Browse the repository at this point in the history
closes #539

---------

Signed-off-by: Jeff Ortel <[email protected]>
  • Loading branch information
jortel authored Oct 25, 2023
1 parent 47623f1 commit 98f338c
Show file tree
Hide file tree
Showing 12 changed files with 956 additions and 4 deletions.
3 changes: 3 additions & 0 deletions api/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ type Target struct {
Resource
Name string `json:"name"`
Description string `json:"description"`
Provider string `json:"provider,omitempty" yaml:",omitempty"`
Choice bool `json:"choice,omitempty" yaml:",omitempty"`
Custom bool `json:"custom,omitempty" yaml:",omitempty"`
Labels []Label `json:"labels"`
Expand All @@ -259,6 +260,7 @@ func (r *Target) With(m *model.Target) {
r.Resource.With(&m.Model)
r.Name = m.Name
r.Description = m.Description
r.Provider = m.Provider
r.Choice = m.Choice
r.Custom = !m.Builtin()
if m.RuleSet != nil {
Expand All @@ -279,6 +281,7 @@ func (r *Target) Model() (m *model.Target) {
m = &model.Target{
Name: r.Name,
Description: r.Description,
Provider: r.Provider,
Choice: r.Choice,
}
m.ID = r.ID
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.3.0
github.com/jortel/go-utils v0.1.2
github.com/konveyor/tackle2-seed v0.0.0-20230928184719-4a383c5aa887
github.com/konveyor/tackle2-seed v0.0.0-20231025181853-8ce94f70f744
github.com/mattn/go-sqlite3 v1.14.17
github.com/onsi/gomega v1.27.6
github.com/prometheus/client_golang v1.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/konveyor/tackle2-seed v0.0.0-20230928184719-4a383c5aa887 h1:LF8F78Oz/S7K0LakDWrjMaoeBMs5tYj2ntcf73u+gt0=
github.com/konveyor/tackle2-seed v0.0.0-20230928184719-4a383c5aa887/go.mod h1:wt9Zb1l1PAd+XxW03KTp8z4dth3DkCDsRrcdZSWTyjA=
github.com/konveyor/tackle2-seed v0.0.0-20231025181853-8ce94f70f744 h1:/FkxudKacnx6eHscDiSFT5iLgJCswGFpMWmflOM/85U=
github.com/konveyor/tackle2-seed v0.0.0-20231025181853-8ce94f70f744/go.mod h1:wt9Zb1l1PAd+XxW03KTp8z4dth3DkCDsRrcdZSWTyjA=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand Down
2 changes: 2 additions & 0 deletions migration/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package migration
import (
"github.com/jortel/go-utils/logr"
v10 "github.com/konveyor/tackle2-hub/migration/v10"
v11 "github.com/konveyor/tackle2-hub/migration/v11"
"github.com/konveyor/tackle2-hub/migration/v2"
v3 "github.com/konveyor/tackle2-hub/migration/v3"
v4 "github.com/konveyor/tackle2-hub/migration/v4"
Expand Down Expand Up @@ -53,5 +54,6 @@ func All() []Migration {
v8.Migration{},
v9.Migration{},
v10.Migration{},
v11.Migration{},
}
}
20 changes: 20 additions & 0 deletions migration/v11/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package v11

import (
"github.com/jortel/go-utils/logr"
"github.com/konveyor/tackle2-hub/migration/v11/model"
"gorm.io/gorm"
)

var log = logr.WithName("migration|v10")

type Migration struct{}

func (r Migration) Apply(db *gorm.DB) (err error) {
err = db.AutoMigrate(r.Models()...)
return
}

func (r Migration) Models() []interface{} {
return model.All()
}
166 changes: 166 additions & 0 deletions migration/v11/model/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package model

import "gorm.io/gorm"

//
// Analysis report.
type Analysis struct {
Model
Effort int
Archived bool `json:"archived"`
Summary JSON `gorm:"type:json"`
Issues []Issue `gorm:"constraint:OnDelete:CASCADE"`
Dependencies []TechDependency `gorm:"constraint:OnDelete:CASCADE"`
ApplicationID uint `gorm:"index;not null"`
Application *Application
}

//
// TechDependency report dependency.
type TechDependency struct {
Model
Provider string `gorm:"uniqueIndex:depA"`
Name string `gorm:"uniqueIndex:depA"`
Version string `gorm:"uniqueIndex:depA"`
SHA string `gorm:"uniqueIndex:depA"`
Indirect bool
Labels JSON `gorm:"type:json"`
AnalysisID uint `gorm:"index;uniqueIndex:depA;not null"`
Analysis *Analysis
}

//
// Issue report issue (violation).
type Issue struct {
Model
RuleSet string `gorm:"uniqueIndex:issueA;not null"`
Rule string `gorm:"uniqueIndex:issueA;not null"`
Name string `gorm:"index"`
Description string
Category string `gorm:"index;not null"`
Incidents []Incident `gorm:"foreignKey:IssueID;constraint:OnDelete:CASCADE"`
Links JSON `gorm:"type:json"`
Facts JSON `gorm:"type:json"`
Labels JSON `gorm:"type:json"`
Effort int `gorm:"index;not null"`
AnalysisID uint `gorm:"index;uniqueIndex:issueA;not null"`
Analysis *Analysis
}

//
// Incident report an issue incident.
type Incident struct {
Model
File string `gorm:"index;not null"`
Line int
Message string
CodeSnip string
Facts JSON `gorm:"type:json"`
IssueID uint `gorm:"index;not null"`
Issue *Issue
}

//
// Link URL link.
type Link struct {
URL string `json:"url"`
Title string `json:"title,omitempty"`
}

//
// ArchivedIssue resource created when issues are archived.
type ArchivedIssue struct {
RuleSet string `json:"ruleSet"`
Rule string `json:"rule"`
Name string `json:"name,omitempty" yaml:",omitempty"`
Description string `json:"description,omitempty" yaml:",omitempty"`
Category string `json:"category"`
Effort int `json:"effort"`
Incidents int `json:"incidents"`
}

//
// RuleSet - Analysis ruleset.
type RuleSet struct {
Model
UUID *string `gorm:"uniqueIndex"`
Kind string
Name string `gorm:"uniqueIndex;not null"`
Description string
Repository JSON `gorm:"type:json"`
IdentityID *uint `gorm:"index"`
Identity *Identity
Rules []Rule `gorm:"constraint:OnDelete:CASCADE"`
DependsOn []RuleSet `gorm:"many2many:RuleSetDependencies;constraint:OnDelete:CASCADE"`
}

func (r *RuleSet) Builtin() bool {
return r.UUID != nil
}

//
// BeforeUpdate hook to avoid cyclic dependencies.
func (r *RuleSet) BeforeUpdate(db *gorm.DB) (err error) {
seen := make(map[uint]bool)
var nextDeps []RuleSet
var nextRuleSetIDs []uint
for _, dep := range r.DependsOn {
nextRuleSetIDs = append(nextRuleSetIDs, dep.ID)
}
for len(nextRuleSetIDs) != 0 {
result := db.Preload("DependsOn").Where("ID IN ?", nextRuleSetIDs).Find(&nextDeps)
if result.Error != nil {
err = result.Error
return
}
nextRuleSetIDs = nextRuleSetIDs[:0]
for _, nextDep := range nextDeps {
for _, dep := range nextDep.DependsOn {
if seen[dep.ID] {
continue
}
if dep.ID == r.ID {
err = DependencyCyclicError{}
return
}
seen[dep.ID] = true
nextRuleSetIDs = append(nextRuleSetIDs, dep.ID)
}
}
}

return
}

//
// Rule - Analysis rule.
type Rule struct {
Model
Name string
Description string
Labels JSON `gorm:"type:json"`
RuleSetID uint `gorm:"uniqueIndex:RuleA;not null"`
RuleSet *RuleSet
FileID *uint `gorm:"uniqueIndex:RuleA" ref:"file"`
File *File
}

//
// Target - analysis rule selector.
type Target struct {
Model
UUID *string `gorm:"uniqueIndex"`
Name string `gorm:"uniqueIndex;not null"`
Description string
Provider string
Choice bool
Labels JSON `gorm:"type:json"`
ImageID uint `gorm:"index" ref:"file"`
Image *File
RuleSetID *uint `gorm:"index"`
RuleSet *RuleSet
}

func (r *Target) Builtin() bool {
return r.UUID != nil
}
Loading

0 comments on commit 98f338c

Please sign in to comment.