Skip to content

Commit

Permalink
feat: allow the use of if condition to trigger github events
Browse files Browse the repository at this point in the history
Signed-off-by: Tsubasa Nagasawa <[email protected]>
  • Loading branch information
toVersus committed Sep 28, 2023
1 parent 7e9cc05 commit c83ac9f
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
**actionlint reports 7 errors:**

```
test.yaml:3:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:3:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
|
3 | branch: main
| ^~~~~~~
Expand Down
6 changes: 6 additions & 0 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ type WebhookEvent struct {
// Types is list of types of the webhook event. Only the types enumerated here will trigger
// the workflow.
Types []*String
// If is `if` filter. This value is nil when it is omitted.
If *String
// Branches is 'branches' filter. This value is nil when it is omitted.
Branches *WebhookEventFilter
// BranchesIgnore is 'branches-ignore' filter. This value is nil when it is omitted.
Expand Down Expand Up @@ -214,6 +216,8 @@ type DispatchInput struct {
// WorkflowDispatchEvent is event on dispatching workflow manually.
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#workflow_dispatch
type WorkflowDispatchEvent struct {
// If is `if` filter. This value is nil when it is omitted.
If *String
// Inputs is map from input names to input attributes. Keys are in lower case since they are case insensitive.
Inputs map[string]*DispatchInput
// Pos is a position in source.
Expand All @@ -228,6 +232,8 @@ func (e *WorkflowDispatchEvent) EventName() string {
// RepositoryDispatchEvent is repository_dispatch event configuration.
// https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#repository_dispatch
type RepositoryDispatchEvent struct {
// If is `if` filter. This value is nil when it is omitted.
If *String
// Types is list of types which can trigger workflow.
Types []*String
// Pos is a position in source.
Expand Down
2 changes: 1 addition & 1 deletion docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ jobs:
Output:

```
test.yaml:4:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:4:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
|
4 | branch: foo
| ^~~~~~~
Expand Down
14 changes: 12 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,13 @@ func (p *parser) parseWorkflowDispatchEvent(pos *Pos, n *yaml.Node) *WorkflowDis
ret := &WorkflowDispatchEvent{Pos: pos}

for _, kv := range p.parseSectionMapping("workflow_dispatch", n, true, true) {
if kv.id != "inputs" {
p.unexpectedKey(kv.key, "workflow_dispatch", []string{"inputs"})
if kv.id != "if" && kv.id != "inputs" {
p.unexpectedKey(kv.key, "workflow_dispatch", []string{"inputs", "if"})
continue
}

if kv.id == "if" {
ret.If = p.parseString(kv.val, false)
continue
}

Expand Down Expand Up @@ -399,6 +404,8 @@ func (p *parser) parseRepositoryDispatchEvent(pos *Pos, n *yaml.Node) *Repositor
for _, kv := range p.parseSectionMapping("repository_dispatch", n, true, true) {
if kv.id == "types" {
ret.Types = p.parseStringOrStringSequence("types", kv.val, false, false)
} else if kv.id == "if" {
ret.If = p.parseString(kv.val, false)
} else {
p.unexpectedKey(kv.key, "repository_dispatch", []string{"types"})
}
Expand Down Expand Up @@ -434,6 +441,8 @@ func (p *parser) parseWebhookEvent(name *String, n *yaml.Node) *WebhookEvent {
ret.Branches = p.parseWebhookEventFilter(kv.key, kv.val)
case "branches-ignore":
ret.BranchesIgnore = p.parseWebhookEventFilter(kv.key, kv.val)
case "if":
ret.If = p.parseString(kv.val, false)
case "tags":
ret.Tags = p.parseWebhookEventFilter(kv.key, kv.val)
case "tags-ignore":
Expand All @@ -449,6 +458,7 @@ func (p *parser) parseWebhookEvent(name *String, n *yaml.Node) *WebhookEvent {
"types",
"branches",
"branches-ignore",
"if",
"tags",
"tags-ignore",
"paths",
Expand Down
2 changes: 1 addition & 1 deletion testdata/err/case_sensitive_keys.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test.yaml:11:5: expected "types" key for "repository_dispatch" section but got "
test.yaml:15:9: unexpected key "DESCRIPTION" for "inputs at workflow_call event" section. expected one of "default", "description", "required", "type" [syntax-check]
test.yaml:19:9: unexpected key "DESCRIPTION" for "secrets" section. expected one of "description", "required" [syntax-check]
test.yaml:22:9: unexpected key "DESCRIPTION" for "outputs at workflow_call event" section. expected one of "description", "value" [syntax-check]
test.yaml:25:5: unexpected key "BRANCHES" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:25:5: unexpected key "BRANCHES" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:28:3: expected "run" key for "defaults" section but got "RUN" [syntax-check]
test.yaml:30:5: unexpected key "SHELL" for "run" section. expected one of "shell", "working-directory" [syntax-check]
test.yaml:33:3: unexpected key "GROUP" for "concurrency" section. expected one of "cancel-in-progress", "group" [syntax-check]
Expand Down
4 changes: 2 additions & 2 deletions testdata/err/unexpected_keys.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test.yaml:3:5: expected "inputs" key for "workflow_dispatch" section but got "invalid_key" [syntax-check]
test.yaml:3:5: unexpected key "invalid_key" for "workflow_dispatch" section. expected one of "if", "inputs" [syntax-check]
test.yaml:6:9: unexpected key "invalid_key" for "inputs" section. expected one of "default", "description", "required" [syntax-check]
test.yaml:8:5: expected "types" key for "repository_dispatch" section but got "invalid_key" [syntax-check]
test.yaml:10:5: unexpected key "invalid_key" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:10:5: unexpected key "invalid_key" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:15:9: unexpected key "invalid_key" for "inputs at workflow_call event" section. expected one of "default", "description", "required", "type" [syntax-check]
test.yaml:18:9: unexpected key "invalid_key" for "secrets" section. expected one of "description", "required" [syntax-check]
test.yaml:22:9: unexpected key "invalid_key" for "outputs at workflow_call event" section. expected one of "description", "value" [syntax-check]
Expand Down
2 changes: 1 addition & 1 deletion testdata/examples/main.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test.yaml:3:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:3:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:5:11: character '\' is invalid for branch and tag names. only special characters [, ?, +, *, \ ! can be escaped with \. see `man git-check-ref-format` for more details. note that regular expression is unavailable. note: filter pattern syntax is explained at https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#filter-pattern-cheat-sheet [glob]
/test\.yaml:10:28: label "linux-latest" is unknown\. available labels are .+\. if it is a custom label for self-hosted runner, set list of labels in actionlint\.yaml config file \[runner-label\]/
test.yaml:13:41: "github.event.head_commit.message" is potentially untrusted. avoid using it directly in inline scripts. instead, pass it through an environment variable. see https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions for more details [expression]
Expand Down
2 changes: 1 addition & 1 deletion testdata/examples/webhook_checks.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test.yaml:4:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:4:5: unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows" [syntax-check]
test.yaml:7:5: both "paths" and "paths-ignore" filters cannot be used for the same event "push". note: use '!' to negate patterns [events]
test.yaml:10:12: invalid activity type "created" for "issues" Webhook event. available types are "assigned", "closed", "deleted", "demilestoned", "edited", "labeled", "locked", "milestoned", "opened", "pinned", "reopened", "transferred", "unassigned", "unlabeled", "unlocked", "unpinned" [events]
test.yaml:13:5: "tags" filter is not available for release event. it is only for push event [events]
Expand Down
2 changes: 1 addition & 1 deletion testdata/format/test.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"message":"unexpected key \"branch\" for \"push\" section. expected one of \"branches\", \"branches-ignore\", \"paths\", \"paths-ignore\", \"tags\", \"tags-ignore\", \"types\", \"workflows\"","filepath":"testdata/format/test.yaml","line":3,"column":5,"kind":"syntax-check","snippet":" branch: main\n ^~~~~~~","end_column":11},{"message":"label \"linux-latest\" is unknown. available labels are \"windows-latest\", \"windows-2022\", \"windows-2019\", \"windows-2016\", \"ubuntu-latest\", \"ubuntu-22.04\", \"ubuntu-20.04\", \"ubuntu-18.04\", \"macos-latest\", \"macos-latest-xl\", \"macos-13-xl\", \"macos-13\", \"macos-13.0\", \"macos-12-xl\", \"macos-12\", \"macos-12.0\", \"macos-11\", \"macos-11.0\", \"macos-10.15\", \"self-hosted\", \"x64\", \"arm\", \"arm64\", \"linux\", \"macos\", \"windows\". if it is a custom label for self-hosted runner, set list of labels in actionlint.yaml config file","filepath":"testdata/format/test.yaml","line":6,"column":14,"kind":"runner-label","snippet":" runs-on: linux-latest\n ^~~~~~~~~~~~","end_column":25}]
[{"message":"unexpected key \"branch\" for \"push\" section. expected one of \"branches\", \"branches-ignore\", \"if\", \"paths\", \"paths-ignore\", \"tags\", \"tags-ignore\", \"types\", \"workflows\"","filepath":"testdata/format/test.yaml","line":3,"column":5,"kind":"syntax-check","snippet":" branch: main\n ^~~~~~~","end_column":11},{"message":"label \"linux-latest\" is unknown. available labels are \"windows-latest\", \"windows-2022\", \"windows-2019\", \"windows-2016\", \"ubuntu-latest\", \"ubuntu-22.04\", \"ubuntu-20.04\", \"ubuntu-18.04\", \"macos-latest\", \"macos-latest-xl\", \"macos-13-xl\", \"macos-13\", \"macos-13.0\", \"macos-12-xl\", \"macos-12\", \"macos-12.0\", \"macos-11\", \"macos-11.0\", \"macos-10.15\", \"self-hosted\", \"x64\", \"arm\", \"arm64\", \"linux\", \"macos\", \"windows\". if it is a custom label for self-hosted runner, set list of labels in actionlint.yaml config file","filepath":"testdata/format/test.yaml","line":6,"column":14,"kind":"runner-label","snippet":" runs-on: linux-latest\n ^~~~~~~~~~~~","end_column":25}]
2 changes: 1 addition & 1 deletion testdata/format/test.jsonl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"message":"unexpected key \"branch\" for \"push\" section. expected one of \"branches\", \"branches-ignore\", \"paths\", \"paths-ignore\", \"tags\", \"tags-ignore\", \"types\", \"workflows\"","filepath":"testdata/format/test.yaml","line":3,"column":5,"kind":"syntax-check","snippet":" branch: main\n ^~~~~~~","end_column":11}
{"message":"unexpected key \"branch\" for \"push\" section. expected one of \"branches\", \"branches-ignore\", \"if\", \"paths\", \"paths-ignore\", \"tags\", \"tags-ignore\", \"types\", \"workflows\"","filepath":"testdata/format/test.yaml","line":3,"column":5,"kind":"syntax-check","snippet":" branch: main\n ^~~~~~~","end_column":11}
{"message":"label \"linux-latest\" is unknown. available labels are \"windows-latest\", \"windows-2022\", \"windows-2019\", \"windows-2016\", \"ubuntu-latest\", \"ubuntu-22.04\", \"ubuntu-20.04\", \"ubuntu-18.04\", \"macos-latest\", \"macos-latest-xl\", \"macos-13-xl\", \"macos-13\", \"macos-13.0\", \"macos-12-xl\", \"macos-12\", \"macos-12.0\", \"macos-11\", \"macos-11.0\", \"macos-10.15\", \"self-hosted\", \"x64\", \"arm\", \"arm64\", \"linux\", \"macos\", \"windows\". if it is a custom label for self-hosted runner, set list of labels in actionlint.yaml config file","filepath":"testdata/format/test.yaml","line":6,"column":14,"kind":"runner-label","snippet":" runs-on: linux-latest\n ^~~~~~~~~~~~","end_column":25}
2 changes: 1 addition & 1 deletion testdata/format/test.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Error at line 3, col 5 of `testdata/format/test.yaml`

unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows"
unexpected key "branch" for "push" section. expected one of "branches", "branches-ignore", "if", "paths", "paths-ignore", "tags", "tags-ignore", "types", "workflows"

```
branch: main
Expand Down
2 changes: 1 addition & 1 deletion testdata/format/test.sarif
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
{
"ruleId": "syntax-check",
"message": {
"text": "unexpected key \"branch\" for \"push\" section. expected one of \"branches\", \"branches-ignore\", \"paths\", \"paths-ignore\", \"tags\", \"tags-ignore\", \"types\", \"workflows\""
"text": "unexpected key \"branch\" for \"push\" section. expected one of \"branches\", \"branches-ignore\", \"if\", \"paths\", \"paths-ignore\", \"tags\", \"tags-ignore\", \"types\", \"workflows\""
},
"locations": [
{
Expand Down
75 changes: 75 additions & 0 deletions testdata/ok/github_events_if.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
on:
branch_protection_rule:
if: true
check_run:
if: true
check_suite:
if: true
create:
if: true
delete:
if: true
deployment:
if: true
deployment_status:
if: true
discussion:
if: true
discussion_comment:
if: true
fork:
if: true
gollum:
if: true
issues:
if: true
issue_comment:
if: true
label:
if: true
merge_group:
if: true
milestone:
if: true
page_build:
if: true
project:
if: true
project_card:
if: true
project_column:
if: true
public:
if: true
pull_request:
if: true
pull_request_review:
if: true
pull_request_review_comment:
if: true
pull_request_target:
if: true
push:
if: true
registry_package:
if: true
release:
if: true
repository_dispatch:
if: true
status:
if: true
watch:
if: true
workflow_dispatch:
if: true
workflow_run:
types: [completed]
if: true
workflows: [foo]

jobs:
hello:
runs-on: ubuntu-latest
steps:
- run: echo 'hello'

0 comments on commit c83ac9f

Please sign in to comment.