-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
357 additions
and
3 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,87 @@ | ||
package action | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type Input struct { | ||
Description string `yaml:"description"` | ||
Required bool `yaml:"required"` | ||
Default string `yaml:"default"` | ||
} | ||
|
||
type Output struct { | ||
Description string `yaml:"description"` | ||
Value string `yaml:"value"` | ||
} | ||
|
||
type Config struct { | ||
Description string `yaml:"description"` | ||
Inputs map[string]Input `yaml:"inputs"` | ||
Name string `yaml:"name"` | ||
Outputs map[string]Output `yaml:"outputs"` | ||
Runs struct { | ||
Steps []struct { | ||
ID string `yaml:"id"` | ||
Run string `yaml:"run"` | ||
Shell string `yaml:"shell"` | ||
} `yaml:"steps"` | ||
Using string `yaml:"using"` | ||
} `yaml:"runs"` | ||
} | ||
|
||
func Load(yamlData []byte) (*Config, error) { | ||
var ret Config | ||
err := yaml.Unmarshal(yamlData, &ret) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ret, nil | ||
} | ||
|
||
var md = `# {{ .Name }} | ||
{{ .Description }} | ||
## Inputs | ||
| Name | Description | Default | Required | | ||
| ---- | ----------- | ------- | -------- | | ||
{{ range $key, $value := .Inputs -}} | ||
| {{ $key }} | {{$value.Description}} | {{$value.Default }} | {{ $value.Required }}| | ||
{{ end }} | ||
## Outputs | ||
| Name | Description | | ||
| ---- | ----------- | | ||
{{ range $key, $value := .Outputs -}} | ||
| {{ $key }} | {{$value.Description}} | | ||
{{ end }}` | ||
|
||
func (c *Config) Markdown() string { | ||
tmpl, err := template.New("markdown").Parse(md) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var tpl bytes.Buffer | ||
err = tmpl.Execute(&tpl, struct { | ||
Name string | ||
Description string | ||
Inputs map[string]Input | ||
Outputs map[string]Output | ||
}{ | ||
Name: c.Name, | ||
Description: c.Description, | ||
Inputs: c.Inputs, | ||
Outputs: c.Outputs, | ||
}) | ||
|
||
return tpl.String() | ||
} |
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,87 @@ | ||
package action | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var githubExample = heredoc.Doc(` | ||
# example from https://docs.github.com/en/free-pro-team@latest/actions/creating-actions/creating-a-composite-run-steps-action#creating-an-action-metadata-file | ||
name: 'Hello World' | ||
description: 'Greet someone' | ||
inputs: | ||
who-to-greet: # id of input | ||
description: 'Who to greet' | ||
required: true | ||
default: 'World' | ||
outputs: | ||
random-number: | ||
description: "Random number" | ||
value: ${{ steps.random-number-generator.outputs.random-id }} | ||
runs: | ||
using: "composite" | ||
steps: | ||
- run: echo Hello ${{ inputs.who-to-greet }}. | ||
shell: bash | ||
- id: random-number-generator | ||
run: echo "::set-output name=random-id::$(echo $RANDOM)" | ||
shell: bash | ||
- run: ${{ github.action_path }}/goodbye.sh | ||
shell: bash | ||
`) | ||
|
||
func TestLoadl(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
yaml string | ||
}{ | ||
{ | ||
name: "simple yaml config", | ||
yaml: githubExample, | ||
}, | ||
} | ||
|
||
for _, test := range cases { | ||
_, err := Load([]byte(test.yaml)) | ||
assert.Equal(t, nil, err, test.name) | ||
} | ||
} | ||
|
||
func TestMarkdown(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
yaml string | ||
expected string | ||
}{ | ||
{ | ||
name: "sample yaml config", | ||
yaml: githubExample, | ||
expected: heredoc.Doc(` | ||
# Hello World | ||
Greet someone | ||
## Inputs | ||
| Name | Description | Default | Required | | ||
| ---- | ----------- | ------- | -------- | | ||
| who-to-greet | Who to greet | World | true| | ||
## Outputs | ||
| Name | Description | | ||
| ---- | ----------- | | ||
| random-number | Random number | | ||
`), | ||
}, | ||
} | ||
|
||
for _, test := range cases { | ||
cfg, err := Load([]byte(test.yaml)) | ||
assert.Equal(t, nil, err, test.name) | ||
assert.Equal(t, test.expected, cfg.Markdown(), test.name) | ||
} | ||
} |
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
Oops, something went wrong.