Skip to content

Commit 88246e8

Browse files
authored
Merge pull request #3 from github/action-configuration
Add package for reading the Action configuration.
2 parents 93695fd + a201753 commit 88246e8

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module github.com/github/codeql-action-sync
22

33
go 1.14
44

5-
require github.com/spf13/cobra v1.0.0
5+
require (
6+
github.com/pkg/errors v0.8.0
7+
github.com/spf13/cobra v1.0.0
8+
github.com/stretchr/testify v1.6.1
9+
)

go.sum

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package actionconfiguration
2+
3+
import (
4+
"encoding/json"
5+
6+
"github.com/pkg/errors"
7+
)
8+
9+
const errorBundleVersionNotSet = "The property \"bundleVersion\" was not set in the Action default configuration."
10+
11+
type ActionConfiguration struct {
12+
BundleVersion string
13+
}
14+
15+
func Parse(contents string) (*ActionConfiguration, error) {
16+
var result ActionConfiguration
17+
err := json.Unmarshal([]byte(contents), &result)
18+
if err != nil {
19+
return nil, errors.Wrap(err, "Error decoding Action default configuration.")
20+
}
21+
if result.BundleVersion == "" {
22+
return nil, errors.New(errorBundleVersionNotSet)
23+
}
24+
return &result, nil
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package actionconfiguration
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestStandardConfiguration(t *testing.T) {
10+
result, err := Parse("{\"bundleVersion\": \"test\"}")
11+
require.NoError(t, err)
12+
require.Equal(t, "test", result.BundleVersion)
13+
}
14+
15+
func TestExtraPropertiesIgnored(t *testing.T) {
16+
result, err := Parse("{\"bundleVersion\": \"test\", \"someOtherThing\": \"blah\"}")
17+
require.NoError(t, err)
18+
require.Equal(t, "test", result.BundleVersion)
19+
}
20+
21+
func TestErrorIfInvalidJSON(t *testing.T) {
22+
_, err := Parse("{")
23+
require.Error(t, err)
24+
}
25+
26+
func TestErrorIfMissingProperties(t *testing.T) {
27+
_, err := Parse("{\"someOtherThing\": \"blah\"}")
28+
require.EqualError(t, err, errorBundleVersionNotSet)
29+
}

0 commit comments

Comments
 (0)