-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: validate contents of ndc-hub (#433)
This PR introduces - a `validate` command in the registry-automation program - a GitHub action workflow that runs the validate command on every commit in the main branch and in pull requests Additionally, I have made the CI checks to be needed for merging a PR.
- Loading branch information
1 parent
0a02163
commit 4ada5fd
Showing
12 changed files
with
249 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Validate | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
contents: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: 1.21.x | ||
|
||
- name: Run Validations | ||
run: | | ||
export NDC_HUB_GIT_REPO_FILE_PATH=$(pwd) | ||
cd registry-automation | ||
go run main.go validate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/hasura/ndc-hub/registry-automation/pkg/ndchub" | ||
"github.com/hasura/ndc-hub/registry-automation/pkg/validate" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var validateCmd = &cobra.Command{ | ||
Use: "validate", | ||
Short: "Validate the contents of ndc-hub", | ||
Run: executeValidateCmd, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(validateCmd) | ||
} | ||
|
||
func executeValidateCmd(cmd *cobra.Command, args []string) { | ||
ndcHubGitRepoFilePath := os.Getenv("NDC_HUB_GIT_REPO_FILE_PATH") | ||
if ndcHubGitRepoFilePath == "" { | ||
fmt.Println("please set a value for NDC_HUB_GIT_REPO_FILE_PATH env var") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
registryFolder := filepath.Join(ndcHubGitRepoFilePath, "registry") | ||
_, err := os.Stat(registryFolder) | ||
if err != nil { | ||
fmt.Println("error while finding the registry folder", err) | ||
os.Exit(1) | ||
return | ||
} | ||
if os.IsNotExist(err) { | ||
fmt.Println("registry folder does not exist") | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
type connectorPackaging struct { | ||
filePath string | ||
connectorPackage *ndchub.ConnectorPackaging | ||
} | ||
var connectorPkgs []connectorPackaging | ||
err = filepath.WalkDir(registryFolder, func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if filepath.Base(path) == ndchub.ConnectorPackagingJSON { | ||
cp, err := ndchub.GetConnectorPackaging(path) | ||
if err != nil { | ||
return err | ||
} | ||
if cp != nil { | ||
connectorPkgs = append(connectorPkgs, connectorPackaging{filePath: path, connectorPackage: cp}) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println("error while walking the registry folder", err) | ||
os.Exit(1) | ||
return | ||
} | ||
|
||
hasError := false | ||
|
||
fmt.Println("Validating `connector-packaging.json` contents") | ||
for _, cp := range connectorPkgs { | ||
err := validate.ConnectorPackaging(cp.connectorPackage) | ||
if err != nil { | ||
fmt.Println("error validating connector packaging", cp.filePath, err) | ||
hasError = true | ||
} | ||
} | ||
fmt.Println("Completed validating `connector-packaging.json` contents") | ||
|
||
if hasError { | ||
fmt.Println("Exiting with a non-zero error code due to the error(s) in validation") | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package ndchub | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
MetadataJSON = "metadata.json" | ||
ConnectorPackagingJSON = "connector-packaging.json" | ||
) | ||
|
||
type Checksum struct { | ||
Type string `json:"type"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type Source struct { | ||
Hash string `json:"hash"` | ||
} | ||
|
||
type ConnectorPackaging struct { | ||
Namespace string `json:"-"` | ||
Name string `json:"-"` | ||
|
||
Version string `json:"version"` | ||
URI string `json:"uri"` | ||
Checksum Checksum `json:"checksum"` | ||
Source Source `json:"source"` | ||
} | ||
|
||
func GetConnectorPackaging(path string) (*ConnectorPackaging, error) { | ||
if strings.Contains(path, "aliased_connectors") { | ||
// It should be safe to ignore aliased_connectors | ||
// as their slug is not used in the connector init process | ||
return nil, nil | ||
} | ||
|
||
// path looks like this: /some/folder/ndc-hub/registry/hasura/turso/releases/v0.1.0/connector-packaging.json | ||
versionFolder := filepath.Dir(path) | ||
releasesFolder := filepath.Dir(versionFolder) | ||
connectorFolder := filepath.Dir(releasesFolder) | ||
namespaceFolder := filepath.Dir(connectorFolder) | ||
|
||
connectorPackagingContent, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var connectorPackaging ConnectorPackaging | ||
err = json.Unmarshal(connectorPackagingContent, &connectorPackaging) | ||
if err != nil { | ||
return nil, err | ||
} | ||
connectorPackaging.Namespace = filepath.Base(namespaceFolder) | ||
connectorPackaging.Name = filepath.Base(connectorFolder) | ||
|
||
return &connectorPackaging, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hasura/ndc-hub/registry-automation/pkg/ndchub" | ||
"golang.org/x/mod/semver" | ||
) | ||
|
||
func ConnectorPackaging(cp *ndchub.ConnectorPackaging) error { | ||
// validate version field | ||
if !strings.HasPrefix(cp.Version, "v") { | ||
return fmt.Errorf("version must start with 'v': but got %s", cp.Version) | ||
} | ||
if !semver.IsValid(cp.Version) { | ||
return fmt.Errorf("invalid semantic version: %s", cp.Version) | ||
} | ||
|
||
return nil | ||
} |
39 changes: 39 additions & 0 deletions
39
registry-automation/pkg/validate/connector_packaging_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package validate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hasura/ndc-hub/registry-automation/pkg/ndchub" | ||
) | ||
|
||
func TestConnectorPackaging(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
version string | ||
wantErr bool | ||
}{ | ||
{"Valid version", "v1.0.0", false}, | ||
{"Valid version with pre-release", "v1.0.0-alpha.1", false}, | ||
{"Valid version with build metadata", "v1.0.0+build.1", false}, | ||
{"Missing v prefix", "1.0.0", true}, | ||
{"Empty version", "", true}, | ||
{"Invalid characters", "vabc.1.0", true}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
cp := &ndchub.ConnectorPackaging{ | ||
Version: tc.version, | ||
} | ||
|
||
err := ConnectorPackaging(cp) | ||
|
||
if tc.wantErr && err == nil { | ||
t.Errorf("ConnectorPackaging() error = nil, wantErr %v", tc.wantErr) | ||
} | ||
if !tc.wantErr && err != nil { | ||
t.Errorf("ConnectorPackaging() error = %v, wantErr %v", err, tc.wantErr) | ||
} | ||
}) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
registry/hasura/cassandra/releases/v1.0.7/connector-packaging.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
registry/hasura/clickhouse/releases/v1.0.5/connector-packaging.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
registry/hasura/duckdb/releases/v0.1.3/connector-packaging.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
registry/hasura/duckdb/releases/v0.1.4/connector-packaging.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
registry/hasura/postgres/releases/v1.2.0/connector-packaging.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters