Skip to content

Commit 8e57417

Browse files
authored
Merge pull request #322 from fabn/feature/parse-params
Add parsed_params output
2 parents e9ac229 + 6e86eb8 commit 8e57417

File tree

14 files changed

+1475
-12
lines changed

14 files changed

+1475
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ As seen above, we have two steps. One for a noop deploy, and one for a regular d
309309
| `actor` | The GitHub handle of the actor that invoked the IssueOps command |
310310
| `environment` | The environment that has been selected for a deployment |
311311
| `params` | The raw parameters that were passed into the deployment command (see param_separator) - Further [documentation](docs/parameters.md) |
312+
| `parsed_params` | A stringified JSON object of the parsed parameters that were passed into the deployment command - Further [documentation](docs/parameters.md) |
312313
| `noop` | The string "true" if the noop trigger was found, otherwise the string "false" - Use this to conditionally control whether your deployment runs as a noop or not |
313314
| `sha` | The sha of the branch to be deployed |
314315
| `default_branch_tree_sha` | The sha of the default branch tree (useful for subsequent workflow steps if they need to do commit comparisons) |

__tests__/functions/environment-targets.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,39 @@ test('checks the comment body and finds an explicit environment target and an ex
174174
)
175175
})
176176

177+
test('checks the comment body and finds an explicit environment target and an explicit sha (sha1) for development with parsed params style params on a noop command', async () => {
178+
expect(
179+
await environmentTargets(
180+
environment,
181+
'.noop 82c238c277ca3df56fe9418a5913d9188eafe3bc development | --cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue',
182+
trigger,
183+
noop_trigger,
184+
stable_branch
185+
)
186+
).toStrictEqual({
187+
environment: 'development',
188+
environmentUrl: null,
189+
environmentObj: {
190+
target: 'development',
191+
noop: true,
192+
stable_branch_used: false,
193+
params:
194+
'--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue',
195+
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
196+
}
197+
})
198+
expect(debugMock).toHaveBeenCalledWith(
199+
'found environment target for noop trigger: development'
200+
)
201+
expect(infoMock).toHaveBeenCalledWith(
202+
`🧮 detected parameters in command: ${COLORS.highlight}--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue`
203+
)
204+
expect(setOutputMock).toHaveBeenCalledWith(
205+
'params',
206+
'--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue'
207+
)
208+
})
209+
177210
test('checks the comment body and finds an explicit environment target and an explicit sha (sha1) for development with params on a noop command and the sha is a sha256 hash (64 characters)', async () => {
178211
expect(
179212
await environmentTargets(

__tests__/functions/params.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import * as core from '@actions/core'
2+
import {parseParams} from '../../src/functions/params'
3+
4+
beforeEach(() => {
5+
jest.clearAllMocks()
6+
jest.spyOn(core, 'debug').mockImplementation(() => {})
7+
})
8+
9+
test('it parses positional parameters', async () => {
10+
expect(parseParams('foo')).toHaveProperty('_', ['foo'])
11+
})
12+
13+
test('it parses arguments using the default settings of library', async () => {
14+
const parsed = parseParams('--foo bar --env.foo=bar')
15+
expect(parsed).toHaveProperty('foo', 'bar')
16+
expect(parsed).toHaveProperty('env', {foo: 'bar'})
17+
expect(parsed).toHaveProperty('_', [])
18+
})
19+
20+
test('it works with empty string', async () => {
21+
expect(parseParams('')).toHaveProperty('_', [])
22+
})
23+
24+
test('it parses multiple positional parameters', async () => {
25+
expect(parseParams('foo bar baz')).toHaveProperty('_', ['foo', 'bar', 'baz'])
26+
})
27+
28+
test('it parses flags correctly', async () => {
29+
const parsed = parseParams('--foo --bar')
30+
expect(parsed).toHaveProperty('foo', true)
31+
expect(parsed).toHaveProperty('bar', true)
32+
expect(parsed).toHaveProperty('_', [])
33+
})
34+
35+
test('it parses numeric values correctly', async () => {
36+
const parsed = parseParams('--count 42')
37+
expect(parsed).toHaveProperty('count', 42)
38+
expect(parsed).toHaveProperty('_', [])
39+
})
40+
41+
test('it parses plain values', async () => {
42+
const parsed = parseParams('count 42')
43+
expect(parsed).toHaveProperty('_', ['count', 42])
44+
})
45+
46+
test('it parses string values with comma separation', async () => {
47+
const parsed = parseParams('LOG_LEVEL=debug,CPU_CORES=4')
48+
expect(parsed).toHaveProperty('_', ['LOG_LEVEL=debug,CPU_CORES=4'])
49+
})
50+
51+
test('it parses boolean values correctly', async () => {
52+
const parsed = parseParams('--enabled=true --disabled false')
53+
expect(parsed).toHaveProperty('enabled', 'true')
54+
expect(parsed).toHaveProperty('disabled', 'false')
55+
expect(parsed).toHaveProperty('_', [])
56+
})
57+
58+
test('it parses nested objects correctly', async () => {
59+
const parsed = parseParams('--config.db.host=localhost --config.db.port=5432')
60+
expect(parsed).toHaveProperty('config', {db: {host: 'localhost', port: 5432}})
61+
expect(parsed).toHaveProperty('_', [])
62+
})
63+
64+
test('it parses a real world example correctly', async () => {
65+
const parsed = parseParams(
66+
'--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue'
67+
)
68+
expect(parsed).toHaveProperty('cpu', 2)
69+
expect(parsed).toHaveProperty('memory', '4G')
70+
expect(parsed).toHaveProperty('env', 'development')
71+
expect(parsed).toHaveProperty('port', 8080)
72+
expect(parsed).toHaveProperty('name', 'my-app')
73+
expect(parsed).toHaveProperty('q', 'my-queue')
74+
expect(parsed).toHaveProperty('_', [])
75+
})

__tests__/main.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,38 @@ test('successfully runs the action on a deployment to an exact sha in developmen
780780
expect(debugMock).toHaveBeenCalledWith('production_environment: false')
781781
})
782782

783+
test('successfully runs the action on a deployment and parse the given parameters', async () => {
784+
process.env.INPUT_ALLOW_SHA_DEPLOYMENTS = 'true'
785+
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
786+
return {
787+
ref: 'test-ref',
788+
status: true,
789+
message: '✔️ PR is approved and all CI checks passed - OK',
790+
noopMode: false,
791+
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
792+
}
793+
})
794+
795+
github.context.payload.comment.body =
796+
'.deploy | --cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue'
797+
const expectedParams = {
798+
_: [],
799+
cpu: 2, // Parser automatically cast to number
800+
memory: '4G',
801+
env: 'development',
802+
port: 8080, // Same here
803+
name: 'my-app',
804+
q: 'my-queue'
805+
}
806+
807+
expect(await run()).toBe('success')
808+
expect(setOutputMock).toHaveBeenCalledWith(
809+
'params',
810+
'--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue'
811+
)
812+
expect(setOutputMock).toHaveBeenCalledWith('parsed_params', expectedParams)
813+
})
814+
783815
test('successfully runs the action after trimming the body', async () => {
784816
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
785817
return {

__tests__/schemas/action.schema.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,7 @@ outputs:
599599
description:
600600
type: string
601601
required: true
602+
parsed_params:
603+
description:
604+
type: string
605+
required: true

action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ outputs:
192192
description: The environment that has been selected for a deployment
193193
params:
194194
description: The raw parameters that were passed into the deployment command (see param_separator)
195+
parsed_params:
196+
description: A stringified JSON object of the parsed parameters that were passed into the deployment command
195197
noop:
196198
description: 'The string "true" if the noop trigger was found, otherwise the string "false" - Use this to conditionally control whether your deployment runs as a noop or not'
197199
sha:
@@ -216,7 +218,7 @@ outputs:
216218
description: 'The console command presented in the GitHub UI to checkout a given fork locally'
217219
fork_full_name:
218220
description: 'The full name of the fork in "org/repo" format'
219-
deployment_id:
221+
deployment_id:
220222
description: The ID of the deployment created by running this action
221223
environment_url:
222224
description: The environment URL detected and used for the deployment (sourced from the environment_urls input)

0 commit comments

Comments
 (0)