Skip to content

Commit dd80aa5

Browse files
authoredJan 1, 2025
Merge pull request #11 from ossf/feat/version-checker
feat: err if not schema v2.0.0
2 parents 5d48a32 + 043aa7b commit dd80aa5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
 

‎v2/si/import.go

+37
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"io"
77
"net/http"
8+
"strconv"
9+
"strings"
810

911
"gopkg.in/yaml.v3"
1012
)
@@ -49,6 +51,36 @@ func getGitHubSourceFile(endpoint string) (response FileAPIResponse, err error)
4951
return
5052
}
5153

54+
func parseVersion(version string) (major int, minor int, patch int) {
55+
splitVersion := strings.Split(version, ".")
56+
if len(splitVersion) == 3 {
57+
major, _ = strconv.Atoi(splitVersion[0])
58+
minor, _ = strconv.Atoi(splitVersion[1])
59+
patch, _ = strconv.Atoi(splitVersion[2])
60+
return
61+
}
62+
if len(splitVersion) == 2 {
63+
major, _ = strconv.Atoi(splitVersion[0])
64+
minor, _ = strconv.Atoi(splitVersion[1])
65+
return
66+
}
67+
if len(splitVersion) == 1 {
68+
major, _ = strconv.Atoi(splitVersion[0])
69+
return
70+
}
71+
return
72+
}
73+
74+
func checkVersion(version string) error {
75+
// This is a placeholder to determine behavior for different schema versions
76+
// but currently only v2.0.0 is supported
77+
major, minor, patch := parseVersion(version)
78+
if major != 2 || minor+patch != 0 {
79+
return fmt.Errorf("unsupported schema version specified by target: %s", version)
80+
}
81+
return nil
82+
}
83+
5284
func Read(owner, repo, path string) (si SecurityInsights, err error) {
5385
var builder SIBuilder
5486
// Get Target SI
@@ -64,6 +96,11 @@ func Read(owner, repo, path string) (si SecurityInsights, err error) {
6496
return
6597
}
6698

99+
err = checkVersion(builder.TargetSI.Header.SchemaVersion)
100+
if err != nil {
101+
return
102+
}
103+
67104
// check for parent SI, read if exists
68105
if builder.TargetSI.Header.ProjectSISource != "" {
69106
response, err = getGitHubSourceFile(builder.TargetSI.Header.ProjectSISource)

0 commit comments

Comments
 (0)
Please sign in to comment.