-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpr-functions.yml
More file actions
114 lines (105 loc) · 2.79 KB
/
expr-functions.yml
File metadata and controls
114 lines (105 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
name: Expr-Function Example
jobs:
- name: Run match json example
steps:
- name: Match Regexp
uses: shell
with:
cmd: echo '{"id":12345,"name":"probe"}'
vars:
result:
id: 12345
name: probe
expected:
id: /^\d{5}$/
name: /^\w+$/
test: |
res.code == 0 &&
match_json(vars.result, vars.expected)
echo: "{{diff_json(vars.result, vars.expected)}}"
- name: Mismatch Regexp
uses: shell
with:
cmd: echo '{"id":"ABC","name":null}'
vars:
result:
id: ABC
name: null
expected:
id: /^\d{5}$/
name: /^\w+$/
test: |
res.code == 0
echo: "{{diff_json(vars.result, vars.expected)}}"
- name: Test string functions
steps:
- name: hasSuffix()
uses: shell
with:
cmd: echo "test.json"
test: hasSuffix(trim(res.stdout), ".json")
outputs:
filename: trim(res.stdout)
- name: hasPrefix()
uses: shell
with:
cmd: echo "https://example.com"
test: hasPrefix(trim(res.stdout), "https://")
outputs:
url: trim(res.stdout)
- name: indexOf()
uses: shell
with:
cmd: echo "hello world"
test: indexOf(res.stdout, "world") >= 0
outputs:
text: trim(res.stdout)
position: indexOf(trim(res.stdout), "world")
- name: verify indexOf() position
uses: shell
with:
cmd: echo "hello world"
test: indexOf(trim(res.stdout), "world") == 6
echo: |
The word "world" is at position {{indexOf(trim(res.stdout), "world")}}
- name: Test parse_json function
steps:
- name: Parse JSON from stdout and extract fields
id: get_user
uses: shell
with:
cmd: echo '{"data":{"id":12345,"name":"probe","tags":["cli","testing"]}}'
outputs:
user_id: parse_json(res.stdout).data.id
user_name: parse_json(res.stdout).data.name
test: |
res.code == 0 &&
parse_json(res.stdout).data.id == 12345 &&
parse_json(res.stdout).data.name == "probe"
- name: Use parsed outputs in next step
uses: shell
with:
cmd: echo "User {{outputs.get_user.user_name}} has ID {{outputs.get_user.user_id}}"
test: res.code == 0
echo: "{{trim(res.stdout)}}"
- name: Combine parse_json with match_json
uses: shell
with:
cmd: echo '{"status":"ok","result":{"count":3,"items":["a","b","c"]}}'
vars:
expected:
status: ok
result:
count: 3
items: ["a", "b", "c"]
test: |
res.code == 0 &&
match_json(parse_json(res.stdout), vars.expected)
echo: "{{diff_json(parse_json(res.stdout), vars.expected)}}"
- name: Parse JSON array from stdout
uses: shell
with:
cmd: echo '[{"id":1,"name":"foo"},{"id":2,"name":"bar"}]'
test: |
res.code == 0 &&
len(parse_json(res.stdout)) == 2