Skip to content

Commit

Permalink
fix generate-popular-actions to read/write outdated actions in JSONL
Browse files Browse the repository at this point in the history
and add more tests
  • Loading branch information
rhysd committed Nov 2, 2024
1 parent 679440f commit e09485d
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 44 deletions.
28 changes: 14 additions & 14 deletions action_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,31 +94,31 @@ func (inputs *ActionMetadataOutputs) UnmarshalYAML(n *yaml.Node) error {
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs
type ActionMetadataRuns struct {
// Using is `using` configuration of action.yaml. It defines what runner is used for the action.
Using string `yaml:"using"`
Using string `yaml:"using" json:"using"`
// Main is `main` configuration of action.yaml for JavaScript action.
Main string `yaml:"main"`
Main string `yaml:"main" json:"main"`
// Pre is `pre` configuration of action.yaml for JavaScript action.
Pre string `yaml:"pre"`
Pre string `yaml:"pre" json:"pre"`
// PreIf is `pre-if` configuration of action.yaml for JavaScript action.
PreIf string `yaml:"pre-if"`
PreIf string `yaml:"pre-if" json:"pre-if"`
// Post is `post` configuration of action.yaml for JavaScript action.
Post string `yaml:"post"`
Post string `yaml:"post" json:"post"`
// PostIf is `post-if` configuration of action.yaml for JavaScript action.
PostIf string `yaml:"post-if"`
PostIf string `yaml:"post-if" json:"post-if"`
// Steps is `steps` configuration of action.yaml for Composite action.
Steps []any `yaml:"steps"`
Steps []any `yaml:"steps" json:"steps"`
// Image is `image` of action.yaml for Docker action.
Image string `yaml:"image"`
Image string `yaml:"image" json:"image"`
// PreEntrypoint is `pre-entrypoint` of action.yaml for Docker action.
PreEntrypoint string `yaml:"pre-entrypoint"`
PreEntrypoint string `yaml:"pre-entrypoint" json:"pre-entrypoint"`
// Entrypoint is `entrypoint` of action.yaml for Docker action.
Entrypoint string `yaml:"entrypoint"`
Entrypoint string `yaml:"entrypoint" json:"entrypoint"`
// PostEntrypoint is `post-entrypoint` of action.yaml for Docker action.
PostEntrypoint string `yaml:"post-entrypoint"`
PostEntrypoint string `yaml:"post-entrypoint" json:"post-entrypoint"`
// Args is `args` of action.yaml for Docker action.
Args []any `yaml:"args"`
Args []any `yaml:"args" json:"args"`
// Env is `env` of action.yaml for Docker action.
Env map[string]any `yaml:"env"`
Env map[string]any `yaml:"env" json:"env"`
}

// ActionMetadataBranding is "branding" section of action.yaml.
Expand Down Expand Up @@ -149,7 +149,7 @@ type ActionMetadata struct {
// true, the outputs object accepts any properties along with strictly typed props.
SkipOutputs bool `yaml:"-" json:"skip_outputs"`
// Runs is "runs" field of action.yaml.
Runs ActionMetadataRuns `yaml:"runs" json:"-"`
Runs ActionMetadataRuns `yaml:"runs" json:"runs"`
// Branding is "branding" field of action.yaml.
Branding ActionMetadataBranding `yaml:"branding" json:"-"`
}
Expand Down
11 changes: 4 additions & 7 deletions scripts/generate-popular-actions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import (
)

type actionOutput struct {
Spec string `json:"spec"`
Meta *actionlint.ActionMetadata `json:"metadata"`
Spec string `json:"spec"`
Meta *actionlint.ActionMetadata `json:"metadata"`
Outdated bool `json:"outdated"`
}

type registry struct {
Expand Down Expand Up @@ -201,11 +202,7 @@ func (g *gen) fetchRemote() (map[string]*actionlint.ActionMetadata, error) {
func (g *gen) writeJSONL(out io.Writer, actions map[string]*actionlint.ActionMetadata) error {
enc := json.NewEncoder(out)
for spec, meta := range actions {
if isOutdatedRunner(meta.Runs.Using) {
g.log.Printf("Ignore outdated action %q since runner is %q", spec, meta.Runs.Using)
continue
}
j := actionOutput{spec, meta}
j := actionOutput{spec, meta, isOutdatedRunner(meta.Runs.Using)}
if err := enc.Encode(&j); err != nil {
return fmt.Errorf("could not encode action %q data into JSON: %w", spec, err)
}
Expand Down
44 changes: 30 additions & 14 deletions scripts/generate-popular-actions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ func TestDefaultPopularActions(t *testing.T) {

func TestReadWriteJSONL(t *testing.T) {
files := []string{
"test.jsonl",
"no_new_version.jsonl",
"skip_inputs.jsonl",
"skip_outputs.jsonl",
"skip_both.jsonl",
"skip_both.jsonl",
"outdated.jsonl",
}

for _, file := range files {
Expand Down Expand Up @@ -98,16 +101,24 @@ func TestWriteGoToStdout(t *testing.T) {
want string
}{
{
in: "test.jsonl",
want: "want.go",
in: "no_new_version.jsonl",
want: "no_new_version.go",
},
{
in: "skip_inputs.jsonl",
want: "skip_inputs_want.go",
want: "skip_inputs.go",
},
{
in: "skip_outputs.jsonl",
want: "skip_outputs_want.go",
want: "skip_outputs.go",
},
{
in: "skip_both.jsonl",
want: "skip_both.go",
},
{
in: "outdated.jsonl",
want: "outdated.go",
},
}

Expand Down Expand Up @@ -135,7 +146,7 @@ func TestWriteGoToStdout(t *testing.T) {
}

func TestWriteJSONLFile(t *testing.T) {
in := filepath.Join("testdata", "actions", "test.jsonl")
in := filepath.Join("testdata", "actions", "no_new_version.jsonl")
b, err := os.ReadFile(in)
if err != nil {
panic(err)
Expand Down Expand Up @@ -164,7 +175,7 @@ func TestWriteJSONLFile(t *testing.T) {
}

func TestWriteGoFile(t *testing.T) {
in := filepath.Join("testdata", "actions", "test.jsonl")
in := filepath.Join("testdata", "actions", "no_new_version.jsonl")
out := filepath.Join("testdata", "go", "out.go")
defer os.Remove(out)

Expand All @@ -175,7 +186,7 @@ func TestWriteGoFile(t *testing.T) {
t.Fatal("exit status is non-zero:", status)
}

b, err := os.ReadFile(filepath.Join("testdata", "go", "want.go"))
b, err := os.ReadFile(filepath.Join("testdata", "go", "no_new_version.go"))
if err != nil {
panic(err)
}
Expand All @@ -199,6 +210,7 @@ func TestFetchRemoteYAML(t *testing.T) {
}{
{"fetch.json", "fetched.go"},
{"outdated.json", "outdated.go"},
{"skip_both.json", "skip_both.go"},
}

for _, tc := range tests {
Expand Down Expand Up @@ -234,14 +246,18 @@ func TestWriteOutdatedActionAsJSONL(t *testing.T) {
t.Fatal("exit status is non-zero:", status)
}

out := stdout.String()
if len(out) > 0 {
t.Fatalf("empty output was expected but got %q", out)
b, err := os.ReadFile(filepath.Join("testdata", "actions", "outdated.jsonl"))
if err != nil {
panic(err)
}
want, have := string(b), stdout.String()
if want != have {
t.Fatal(cmp.Diff(want, have))
}
}

func TestLogOutput(t *testing.T) {
f := filepath.Join("testdata", "actions", "test.jsonl")
f := filepath.Join("testdata", "actions", "no_new_version.jsonl")
stdout := &bytes.Buffer{}
logged := &bytes.Buffer{}
status := newGen(stdout, io.Discard, logged).run([]string{"test", "-s", f, "-f", "jsonl"})
Expand Down Expand Up @@ -357,7 +373,7 @@ func TestCouldNotReadJSONLFile(t *testing.T) {
}

func TestCouldNotCreateOutputFile(t *testing.T) {
f := filepath.Join("testdata", "actions", "test.jsonl")
f := filepath.Join("testdata", "actions", "no_new_version.jsonl")
out := filepath.Join("testdata", "this-dir-does-not-exit", "foo.jsonl")
stdout := io.Discard
stderr := &bytes.Buffer{}
Expand All @@ -382,7 +398,7 @@ func (w testErrorWriter) Write(b []byte) (int, error) {
func TestWriteError(t *testing.T) {
for _, format := range []string{"go", "jsonl"} {
t.Run(format, func(t *testing.T) {
f := filepath.Join("testdata", "actions", "test.jsonl")
f := filepath.Join("testdata", "actions", "no_new_version.jsonl")
stdout := testErrorWriter{}
stderr := &bytes.Buffer{}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"spec":"rhysd/action-setup-vim@v1","metadata":{"name":"Setup Vim","inputs":{"configure-args":{"name":"configure-args","required":false},"neovim":{"name":"neovim","required":false},"token":{"name":"token","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":false,"skip_outputs":false,"runs":{"using":"node20","main":"src/index.js","pre":"","pre-if":"","post":"","post-if":"","steps":null,"image":"","pre-entrypoint":"","entrypoint":"","post-entrypoint":"","args":null,"env":null}},"outdated":false}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"spec":"rhysd/action-setup-vim@v1.0.0","metadata":{"name":"Setup Vim","inputs":{"github-token":{"name":"github-token","required":false},"neovim":{"name":"neovim","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":false,"skip_outputs":false,"runs":{"using":"node12","main":"src/index.js","pre":"","pre-if":"","post":"","post-if":"","steps":null,"image":"","pre-entrypoint":"","entrypoint":"","post-entrypoint":"","args":null,"env":null}},"outdated":true}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"spec":"rhysd/action-setup-vim@v1","metadata":{"name":"Setup Vim","inputs":{"configure-args":{"name":"configure-args","required":false},"neovim":{"name":"neovim","required":false},"token":{"name":"token","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":true,"skip_outputs":true,"runs":{"using":"node20","main":"src/index.js","pre":"","pre-if":"","post":"","post-if":"","steps":null,"image":"","pre-entrypoint":"","entrypoint":"","post-entrypoint":"","args":null,"env":null}},"outdated":false}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"spec":"rhysd/action-setup-vim@v1","metadata":{"name":"Setup Vim","inputs":{"neovim":{"name":"neovim","required":false},"token":{"name":"token","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":true,"skip_outputs":false}}
{"spec":"rhysd/action-setup-vim@v1","metadata":{"name":"Setup Vim","inputs":{"configure-args":{"name":"configure-args","required":false},"neovim":{"name":"neovim","required":false},"token":{"name":"token","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":true,"skip_outputs":false,"runs":{"using":"node20","main":"src/index.js","pre":"","pre-if":"","post":"","post-if":"","steps":null,"image":"","pre-entrypoint":"","entrypoint":"","post-entrypoint":"","args":null,"env":null}},"outdated":false}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"spec":"rhysd/action-setup-vim@v1","metadata":{"name":"Setup Vim","inputs":{"neovim":{"name":"neovim","required":false},"token":{"name":"token","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":false,"skip_outputs":true}}
{"spec":"rhysd/action-setup-vim@v1","metadata":{"name":"Setup Vim","inputs":{"configure-args":{"name":"configure-args","required":false},"neovim":{"name":"neovim","required":false},"token":{"name":"token","required":false},"version":{"name":"version","required":false}},"outputs":{"executable":{"name":"executable"}},"skip_inputs":false,"skip_outputs":true,"runs":{"using":"node20","main":"src/index.js","pre":"","pre-if":"","post":"","post-if":"","steps":null,"image":"","pre-entrypoint":"","entrypoint":"","post-entrypoint":"","args":null,"env":null}},"outdated":false}

This file was deleted.

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

17 changes: 17 additions & 0 deletions scripts/generate-popular-actions/testdata/go/skip_both.go

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

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

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"slug": "rhysd/action-setup-vim",
"skip_inputs": true,
"tags": ["v1"],
"next": "this-is-awesome-new-version"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"slug": "rhysd/action-setup-vim",
"tags": ["v1"],
"next": "this-is-awesome-new-version",
"skip_inputs": true,
"skip_outputs": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"slug": "rhysd/action-setup-vim",
"tags": ["v1"],
"next": "this-is-awesome-new-version",
"skip_inputs": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"slug": "rhysd/action-setup-vim",
"skip_outputs": true,
"tags": ["v1"],
"next": "this-is-awesome-new-version"
}
]

0 comments on commit e09485d

Please sign in to comment.