Skip to content

Commit

Permalink
feat: create new Go packages
Browse files Browse the repository at this point in the history
Signed-off-by: Gareth Jones <[email protected]>
  • Loading branch information
G-Rath committed Oct 9, 2024
1 parent 5af796f commit 7b945ad
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 0 deletions.
78 changes: 78 additions & 0 deletions packages/go/constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package constants

type Ecosystem string

const (
EcosystemGo Ecosystem = "Go"
EcosystemNPM Ecosystem = "npm"
EcosystemOSSFuzz Ecosystem = "OSS-Fuzz"
EcosystemPyPI Ecosystem = "PyPI"
EcosystemRubyGems Ecosystem = "RubyGems"
EcosystemCratesIO Ecosystem = "crates.io"
EcosystemPackagist Ecosystem = "Packagist"
EcosystemMaven Ecosystem = "Maven"
EcosystemNuGet Ecosystem = "NuGet"
EcosystemLinux Ecosystem = "Linux"
EcosystemDebian Ecosystem = "Debian"
EcosystemAlpine Ecosystem = "Alpine"
EcosystemHex Ecosystem = "Hex"
EcosystemAndroid Ecosystem = "Android"
EcosystemGitHubActions Ecosystem = "GitHub Actions"
EcosystemPub Ecosystem = "Pub"
EcosystemConanCenter Ecosystem = "ConanCenter"
EcosystemRockyLinux Ecosystem = "Rocky Linux"
EcosystemAlmaLinux Ecosystem = "AlmaLinux"
EcosystemBitnami Ecosystem = "Bitnami"
EcosystemPhotonOS Ecosystem = "Photon OS"
EcosystemCRAN Ecosystem = "CRAN"
EcosystemBioconductor Ecosystem = "Bioconductor"
EcosystemSwiftURL Ecosystem = "SwiftURL"
EcosystemRedHat Ecosystem = "Red Hat"
EcosystemUbuntu Ecosystem = "Ubuntu"
)

type SeverityType string

const (
SeverityCVSSV2 SeverityType = "CVSS_V2"
SeverityCVSSV3 SeverityType = "CVSS_V3"
SeverityCVSSV4 SeverityType = "CVSS_V4"
)

type RangeType string

const (
RangeSemVer RangeType = "SEMVER"
RangeEcosystem RangeType = "ECOSYSTEM"
RangeGit RangeType = "GIT"
)

type ReferenceType string

const (
ReferenceAdvisory ReferenceType = "ADVISORY"
ReferenceArticle ReferenceType = "ARTICLE"
ReferenceDetection ReferenceType = "DETECTION"
ReferenceDiscussion ReferenceType = "DISCUSSION"
ReferenceReport ReferenceType = "REPORT"
ReferenceFix ReferenceType = "FIX"
ReferenceIntroduced ReferenceType = "INTRODUCED"
ReferencePackage ReferenceType = "PACKAGE"
ReferenceEvidence ReferenceType = "EVIDENCE"
ReferenceWeb ReferenceType = "WEB"
)

type CreditType string

const (
CreditFinder CreditType = "FINDER"
CreditReporter CreditType = "REPORTER"
CreditAnalyst CreditType = "ANALYST"
CreditCoordinator CreditType = "COORDINATOR"
CreditRemediationDeveloper CreditType = "REMEDIATION_DEVELOPER" //nolint:gosec
CreditRemediationReviewer CreditType = "REMEDIATION_REVIEWER" //nolint:gosec
CreditRemediationVerifier CreditType = "REMEDIATION_VERIFIER" //nolint:gosec
CreditTool CreditType = "TOOL"
CreditSponsor CreditType = "SPONSOR"
CreditOther CreditType = "OTHER"
)
29 changes: 29 additions & 0 deletions packages/go/ecosystem/ecosystem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ecosystem

import (
"strings"

"github.com/ossf/osv-schema/constants"
)

type Parsed struct {
Ecosystem constants.Ecosystem
Suffix string
}

func (p *Parsed) String() string {
str := string(p.Ecosystem)

if p.Suffix != "" {
str += ":" + p.Suffix
}

return str
}

// Parse parses a string into a constants.Ecosystem and an optional suffix specified with a ":"
func Parse(str string) Parsed {
ecosystem, suffix, _ := strings.Cut(str, ":")

return Parsed{constants.Ecosystem(ecosystem), suffix}
}
185 changes: 185 additions & 0 deletions packages/go/ecosystem/ecosystem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package ecosystem_test

import (
"reflect"
"testing"

"github.com/ossf/osv-schema/constants"
"github.com/ossf/osv-schema/ecosystem"
)

type testCase struct {
string string
parsed ecosystem.Parsed
}

func buildCases() []testCase {
return []testCase{
{
string: "crates.io",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemCratesIO,
Suffix: "",
},
},
{
string: "crates.io: ",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemCratesIO,
Suffix: " ",
},
},
{
string: "crates.io::",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemCratesIO,
Suffix: ":",
},
},
{
string: "npm",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemNPM,
Suffix: "",
},
},
{
string: "npm:abc",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemNPM,
Suffix: "abc",
},
},
{
string: "Alpine",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemAlpine,
Suffix: "",
},
},
{
string: "Alpine:v",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemAlpine,
Suffix: "v",
},
},
{
string: "Alpine:v3.16",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemAlpine,
Suffix: "v3.16",
},
},
{
string: "Alpine:3.16",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemAlpine,
Suffix: "3.16",
},
},
{
string: "Maven",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemMaven,
Suffix: "",
},
},
{
string: "Maven:https://maven.google.com",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemMaven,
Suffix: "https://maven.google.com",
},
},
{
string: "Photon OS",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemPhotonOS,
Suffix: "",
},
},
{
string: "Photon OS:abc",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemPhotonOS,
Suffix: "abc",
},
},
{
string: "Photon OS:3.0",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemPhotonOS,
Suffix: "3.0",
},
},
{
string: "Red Hat",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemRedHat,
Suffix: "",
},
},
{
string: "Red Hat:abc",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemRedHat,
Suffix: "abc",
},
},
{
string: "Red Hat:rhel_aus:8.4::appstream",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemRedHat,
Suffix: "rhel_aus:8.4::appstream",
},
},
{
string: "Ubuntu",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemUbuntu,
Suffix: "",
},
},
{
string: "Ubuntu:Pro",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemUbuntu,
Suffix: "Pro",
},
},
{
string: "Ubuntu:Pro:18.04:LTS",
parsed: ecosystem.Parsed{
Ecosystem: constants.EcosystemUbuntu,
Suffix: "Pro:18.04:LTS",
},
},
}
}

func TestParsed_String(t *testing.T) {
t.Parallel()

tests := buildCases()
for _, tt := range tests {
t.Run(tt.string, func(t *testing.T) {
if got := tt.parsed.String(); got != tt.string {
t.Errorf("String() = %v, want %v", got, tt.string)
}
})
}
}

func TestParse(t *testing.T) {
t.Parallel()

tests := buildCases()
for _, tt := range tests {
t.Run(tt.string, func(t *testing.T) {
if got := ecosystem.Parse(tt.string); !reflect.DeepEqual(got, tt.parsed) {
t.Errorf("Parse() = %v, want %v", got, tt.parsed)
}
})
}
}
3 changes: 3 additions & 0 deletions packages/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/ossf/osv-schema

go 1.22.7

0 comments on commit 7b945ad

Please sign in to comment.