Skip to content

Commit

Permalink
feat: add code quality api group codequality.pkg.katanomi.dev/v1alpha1 (
Browse files Browse the repository at this point in the history
#270)

* feat: add code quality api group codequality.pkg.katanomi.dev/v1alpha1

Adds code analysis and unit tests related struct to create standard
inputs and outputs for related business models

* feat: add artifacts api group artifacts.pkg.katanomi.dev/v1alpha1

Adds artifacts related struct to create standardized structures
inputs and outputs for related business models
  • Loading branch information
danielfbm authored Sep 2, 2022
1 parent 9e16d0a commit 302060e
Show file tree
Hide file tree
Showing 23 changed files with 1,466 additions and 0 deletions.
53 changes: 53 additions & 0 deletions apis/artifacts/v1alpha1/artifact_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2022 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"
)

// ArtifactVersionCollection collection of artifacts versions
type ArtifactVersionCollection struct {
// ArtifactVersions all the artifacts
ArtifactVersions []ArtifactVersion `json:"artifactVersions"`
}

// ArtifactVersion artifacts
type ArtifactVersion struct {
// Type of artifact
Type ArtifactType `json:"type"`
// URL of artifact
URL string `json:"url"`
// Digest means artifact digest
// can be used to store a unique identifier
// of the artifact version
Digest string `json:"digest,omitempty"`

// Versions of current artifact
// +optional
Versions []string `json:"versions,omitempty"`
}

func (ArtifactVersion) GetBinaryObjectFromValues(ctx context.Context, array []string) (versions []ArtifactVersion) {
for _, item := range array {
versions = append(versions, ArtifactVersion{
Type: ArtifactTypeBinary,
URL: item,
})
}
return
}
61 changes: 61 additions & 0 deletions apis/artifacts/v1alpha1/artifact_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2022 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
. "github.com/katanomi/pkg/testing"
"github.com/onsi/gomega"
)

func TestArtifactVersionGetBinaryObjectFromValues(t *testing.T) {
table := map[string]struct {
ctx context.Context
values []string

expected *[]ArtifactVersion
}{
"full values with prefix": {
context.Background(),
[]string{"https://binary.katanomi.dev/abc/def.jpg", "https://binary.katanomi.dev/abc/anotherfolder"},
MustLoadReturnObjectFromYAML("testdata/ArtifactVersion.GetBinaryObjectFromValues.golden.yaml", &[]ArtifactVersion{}).(*[]ArtifactVersion),
},
"nil values": {
context.Background(),
nil,
nil,
},
}

for test, values := range table {
t.Run(test, func(t *testing.T) {
g := gomega.NewGomegaWithT(t)
result := ArtifactVersion{}.GetBinaryObjectFromValues(values.ctx, values.values)

if values.expected == nil {
g.Expect(result).To(gomega.BeNil())
} else {
diff := cmp.Diff(*values.expected, result)
g.Expect(diff).To(gomega.BeEmpty())
}

})
}
}
34 changes: 34 additions & 0 deletions apis/artifacts/v1alpha1/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Copyright 2022 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

type ArtifactType string

const (
// ArtifactTypeContainerImage artifact type as container image
ArtifactTypeContainerImage ArtifactType = "ContainerImage"
DeprecatedArtifactTypeOCIContainerImage = "OCIContainerImage"

// ArtifactTypeHelmChart artifact type helm chart
ArtifactTypeHelmChart ArtifactType = "HelmChart"
DeprecatedArtifactTypeOCIHelmChart = "OCIHelmChart"

// ArtifactTypeBinary binary artifact
ArtifactTypeBinary ArtifactType = "Binary"
// ArtifactTypeMaven maven artifact
ArtifactTypeMaven ArtifactType = "Maven"
)
38 changes: 38 additions & 0 deletions apis/artifacts/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2022 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 stores definitions and methods to handle
// artifact definitions and methods
// +kubebuilder:object:generate=true
// +k8s:deepcopy-gen=package
// +groupName=artifacts.pkg.katanomi.dev
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "artifacts.pkg.katanomi.dev", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- url: "https://binary.katanomi.dev/abc/def.jpg"
type: Binary
- url: "https://binary.katanomi.dev/abc/anotherfolder"
type: Binary
64 changes: 64 additions & 0 deletions apis/artifacts/v1alpha1/zz_generated.deepcopy.go

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

120 changes: 120 additions & 0 deletions apis/codequality/v1alpha1/code_analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2022 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

// AnalysisResult stores the result of a code analysis performed
// storing the specific Result, the remote report address,
// the specific task id used for the project id, with optional
// metrics
type AnalysisResult struct {
// Result of the code analysis:
// - Succeeded: successful code analysis with passing quality gates
// - Failed: failed code analysis
// - Canceled: canceled code analysis due to canceled task
// +optional
Result string `json:"result,omitempty"`
// ReportURL for analyzed code revision
// +optional
ReportURL string `json:"reportURL,omitempty"`
// TaskID of the performed analysis
// +optional
TaskID string `json:"taskID,omitempty"`
// ProjectID for code analysis tool
// +optional
ProjectID string `json:"projectID,omitempty"`

// Metrics for code analysis
Metrics *AnalisysMetrics `json:"metrics,omitempty"`
}

// AnalisysMetrics b
type AnalisysMetrics struct {
// Branch store rate metrics for current
Branch *CodeChangeMetrics `json:"branch"`

// Target stores target branch data
// only populated when using a Pull Request
// +optional
Target *CodeChangeMetrics `json:"target,omitempty"`

// Ratings resulted from code scan analysis
// stores one rating type as an item
// using the key as unique identifier for the rating type
Ratings map[string]AnalysisRating `json:"ratings,omitempty"`

// Languages detected in code base
// +optional
Languages []string `json:"languages"`

// CodeSize based on scanned code base
CodeSize *CodeSize `json:"codeSize"`
}

// CodeChangeMetrics common metrics for code such as:
// CoverageRate: test coverage rate based on automated tests
// DuplicationRate: code duplication rate detected by code analysis tool
type CodeChangeMetrics struct {
// CoverageRate by tests during code analysis
// +optional
CoverageRate CodeChangeRates `json:"coverage"`

// DuplicationRate discovered during code analysis
// +optional
DuplicationRate CodeChangeRates `json:"duplications"`
}

// CodeChangeRates stores changes in a specific rate
// for new and already existing code
// used as code duplications, test coverage, etc.
type CodeChangeRates struct {
// New code rate
// calculated for new code only
// +optional
New string `json:"new"`
// Total code rate
// measured over existing code
Total string `json:"total"`
}

// AnalysisRating ratings by type
// supports adding customized types
// Rate describe the analysis rate, such as:
// - A
// - B
// - C
// - D
// - E
// IssuesCount stores the related issues number
type AnalysisRating struct {
// Rate describe the analysis rate, such as:
// - A
// - B
// - C
// - D
// - E
Rate string `json:"rate"`

// IssuesCount stores the related issues number
// for the analyzed metric
IssuesCount int `json:"issues"`
}

// CodeSize metrics of code base
type CodeSize struct {
// LinesOfCode inside the project
LinesOfCode int `json:"linesOfCode"`
}
Loading

0 comments on commit 302060e

Please sign in to comment.