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 a7a1624
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 10 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
2 changes: 2 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
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
3 changes: 3 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,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 +451,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
2 changes: 1 addition & 1 deletion 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: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 a7a1624

Please sign in to comment.