Skip to content

Commit 073fe80

Browse files
committed
better readme example, do not prepend/append regexp with ^ and $
1 parent b16e5bf commit 073fe80

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

README.md

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,39 @@ Single-Variable-Mapper action maps variable by regular expressions.
77
### Map using regular expression
88

99
```yaml
10-
on: [push]
1110
name: single-variable-mapper example
11+
on:
12+
workflow_dispatch:
13+
inputs:
14+
env:
15+
description: 'Where to deploy'
16+
required: true
17+
default: 'staging.project.com'
18+
type: choice
19+
options:
20+
- 'staging.project.com'
21+
- 'staging-1.project.com'
22+
- 'staging-2.project.com'
23+
- 'sandbox.project.com'
24+
- 'sandbox-1.project.com'
25+
- 'sandbox-2.project.com'
26+
- 'project.com'
1227
jobs:
1328
build:
1429
runs-on: ubuntu-latest
1530
steps:
16-
- uses: caseycs/single-variable-mapper@master
31+
- name: Map deployment target to environment config
32+
uses: caseycs/single-variable-mapper@master
1733
id: mapper
1834
with:
19-
key: staging-5
35+
key: '${{ github.event.inputs.env }}'
2036
map: |
21-
sandbox-\d+: sandbox
22-
staging-\d+: staging
37+
staging(-\d+)?: staging
38+
sandbox(-\d+)?: sandbox
39+
project.com: prod
2340
- name: Print mapped value
2441
run: echo ${{ steps.mapper.outputs.value }}
25-
# staging
42+
# staging for staging-X.project.com or staging.project.com
2643
```
2744

2845
### Remap string value

__tests__/main.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('correct input values, successful cases', () => {
7070
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'value', 'v2')
7171
})
7272

73-
it('regex string', () => {
73+
it('regex full string', () => {
7474
const input: { [name: string]: string } = {
7575
key: 'staging-23',
7676
map: 'staging-\\d+:staging',
@@ -90,6 +90,49 @@ describe('correct input values, successful cases', () => {
9090
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'value', 'staging')
9191
})
9292

93+
it('regex partial string', () => {
94+
const input: { [name: string]: string } = {
95+
key: 'staging-23.project.com',
96+
map: 'staging-\\d+:staging',
97+
separator: ':',
98+
export_to: 'output',
99+
mode: 'strict'
100+
}
101+
getInputMock.mockImplementation(name => input[name])
102+
103+
main.run()
104+
expect(runMock).toHaveReturned()
105+
106+
expect(errorMock).not.toHaveBeenCalled()
107+
expect(setFailedMock).not.toHaveBeenCalled()
108+
expect(exportVariableMock).not.toHaveBeenCalled()
109+
110+
expect(setOutputMock).toHaveBeenNthCalledWith(1, 'value', 'staging')
111+
})
112+
113+
it('regex partial string with ^ and $', () => {
114+
const input: { [name: string]: string } = {
115+
key: 'staging-23.project.com',
116+
map: '^staging-\\d+$:staging',
117+
separator: ':',
118+
export_to: 'output',
119+
mode: 'strict'
120+
}
121+
getInputMock.mockImplementation(name => input[name])
122+
123+
main.run()
124+
expect(runMock).toHaveReturned()
125+
126+
expect(errorMock).not.toHaveBeenCalled()
127+
expect(setOutputMock).not.toHaveBeenCalled()
128+
expect(exportVariableMock).not.toHaveBeenCalled()
129+
130+
expect(setFailedMock).toHaveBeenNthCalledWith(
131+
1,
132+
'No suitable mapping found'
133+
)
134+
})
135+
93136
it('mode fallback-to-original', () => {
94137
const input: { [name: string]: string } = {
95138
key: 'sandbox-25',

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function run(): void {
6262

6363
for (const pair of input.map) {
6464
core.debug(`Map pair: ${JSON.stringify(pair)}`)
65-
if (new RegExp(`^${pair[0]}$`).test(input.key)) {
65+
if (new RegExp(pair[0]).test(input.key)) {
6666
result = pair[1]
6767
}
6868
}

0 commit comments

Comments
 (0)