Skip to content

Commit

Permalink
Merge pull request #325 from fabn/feature/payload-arguments
Browse files Browse the repository at this point in the history
Store params and parsed params into deployment payload
  • Loading branch information
GrantBirki authored Nov 28, 2024
2 parents 7b0a1f4 + 3aa5f66 commit 0bd0654
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 18 deletions.
35 changes: 35 additions & 0 deletions __tests__/functions/environment-targets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test('checks the comment body and does not find an explicit environment target',
noop: false,
stable_branch_used: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -70,6 +71,7 @@ test('checks the comment body and finds an explicit environment target for devel
noop: false,
stable_branch_used: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -95,6 +97,7 @@ test('checks the comment body and finds an explicit environment target for devel
noop: false,
stable_branch_used: false,
params: 'something1 something2 something3',
parsed_params: {_: ['something1', 'something2', 'something3']},
sha: null
}
})
Expand Down Expand Up @@ -127,6 +130,7 @@ test('checks the comment body and finds an explicit environment target and an ex
noop: false,
stable_branch_used: false,
params: 'something1 something2 something3',
parsed_params: {_: ['something1', 'something2', 'something3']},
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
}
})
Expand Down Expand Up @@ -159,6 +163,7 @@ test('checks the comment body and finds an explicit environment target and an ex
noop: true,
stable_branch_used: false,
params: 'something1 something2 something3',
parsed_params: {_: ['something1', 'something2', 'something3']},
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
}
})
Expand Down Expand Up @@ -192,6 +197,15 @@ test('checks the comment body and finds an explicit environment target and an ex
stable_branch_used: false,
params:
'--cpu=2 --memory=4G --env=development --port=8080 --name=my-app -q my-queue',
parsed_params: {
_: [],
cpu: 2,
memory: '4G',
env: 'development',
port: 8080,
name: 'my-app',
q: 'my-queue'
},
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
}
})
Expand Down Expand Up @@ -224,6 +238,7 @@ test('checks the comment body and finds an explicit environment target and an ex
noop: true,
stable_branch_used: false,
params: 'something1 something2 something3',
parsed_params: {_: ['something1', 'something2', 'something3']},
sha: 'f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b'
}
})
Expand Down Expand Up @@ -256,6 +271,7 @@ test('checks the comment body and finds an explicit environment target and an ex
noop: true,
stable_branch_used: false,
params: null,
parsed_params: null,
sha: '82c238c277ca3df56fe9418a5913d9188eafe3bc'
}
})
Expand Down Expand Up @@ -289,6 +305,7 @@ test('checks the comment body and finds an explicit environment target for devel
noop: false,
stable_branch_used: true,
params: 'something1 | something2 something3',
parsed_params: {_: ['something1', '|', 'something2', 'something3']},
sha: null
}
})
Expand Down Expand Up @@ -321,6 +338,7 @@ test('checks the comment body and finds an explicit environment target for stagi
noop: true,
stable_branch_used: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -346,6 +364,7 @@ test('checks the comment body and finds an explicit environment target for stagi
noop: true,
stable_branch_used: true,
params: null,
parsed_params: null,
sha: null
}
})
Expand Down Expand Up @@ -376,6 +395,7 @@ test('checks the comment body and finds an explicit environment target for stagi
noop: true,
stable_branch_used: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand Down Expand Up @@ -417,6 +437,7 @@ test('checks the comment body and finds an explicit environment target for stagi
noop: true,
stable_branch_used: true,
params: 'something1 something2 something3',
parsed_params: {_: ['something1', 'something2', 'something3']},
sha: null
}
})
Expand Down Expand Up @@ -458,6 +479,7 @@ test('checks the comment body and uses the default production environment target
noop: false,
stable_branch_used: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand Down Expand Up @@ -498,6 +520,7 @@ test('checks the comment body and finds an explicit environment target for a pro
target: 'production',
noop: false,
params: null,
parsed_params: null,
stable_branch_used: false,
sha: null
}
Expand Down Expand Up @@ -534,6 +557,7 @@ test('checks the comment body and finds an explicit environment target for a pro
stable_branch_used: false,
noop: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand Down Expand Up @@ -572,6 +596,7 @@ test('checks the comment body and finds an explicit environment target for a pro
stable_branch_used: false,
noop: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand Down Expand Up @@ -602,6 +627,7 @@ test('checks the comment body and finds an explicit environment target for stagi
stable_branch_used: false,
noop: true,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -627,6 +653,7 @@ test('checks the comment body and finds a noop deploy to the stable branch and d
stable_branch_used: true,
noop: true,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -652,6 +679,7 @@ test('checks the comment body and finds a noop deploy to the stable branch and d
stable_branch_used: true,
noop: true,
params: 'foo=bar',
parsed_params: {_: ['foo=bar']},
sha: null
}
})
Expand All @@ -677,6 +705,7 @@ test('checks the comment body and finds an explicit environment target for produ
stable_branch_used: false,
noop: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -702,6 +731,7 @@ test('checks the comment body on a noop deploy and does not find an explicit env
stable_branch_used: false,
noop: true,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -725,6 +755,7 @@ test('checks the comment body on a deployment and does not find any matching env
environmentObj: {
noop: null,
params: null,
parsed_params: null,
stable_branch_used: null,
target: false,
sha: null
Expand Down Expand Up @@ -758,6 +789,7 @@ test('checks the comment body on a stable branch deployment and finds a matching
stable_branch_used: true,
noop: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -783,6 +815,7 @@ test('checks the comment body on a stable branch deployment and finds a matching
stable_branch_used: true,
noop: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -808,6 +841,7 @@ test('checks the comment body on a stable branch deployment and uses the default
stable_branch_used: true,
noop: false,
params: null,
parsed_params: null,
sha: null
}
})
Expand All @@ -831,6 +865,7 @@ test('checks the comment body on a stable branch deployment and does not find a
environmentObj: {
noop: null,
params: null,
parsed_params: null,
stable_branch_used: null,
target: false,
sha: null
Expand Down
12 changes: 9 additions & 3 deletions __tests__/functions/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ beforeEach(() => {
jest.spyOn(core, 'debug').mockImplementation(() => {})
})

test('with empty param object', async () => {
expect(parseParams('')).toStrictEqual({_: []})
expect(parseParams(null)).toStrictEqual({_: []})
expect(parseParams(undefined)).toStrictEqual({_: []})
})

test('it parses positional parameters', async () => {
expect(parseParams('foo')).toHaveProperty('_', ['foo'])
expect(parseParams('foo bar baz')).toHaveProperty('_', ['foo', 'bar', 'baz'])
})

test('it parses arguments using the default settings of library', async () => {
const parsed = parseParams('--foo bar --env.foo=bar')
const parsed = parseParams('--foo bar --env.foo=bar baz')
expect(parsed).toHaveProperty('foo', 'bar')
expect(parsed).toHaveProperty('env', {foo: 'bar'})
expect(parsed).toHaveProperty('_', [])
expect(parsed).toHaveProperty('_', ['baz'])
})

test('it works with empty string', async () => {
Expand Down
98 changes: 90 additions & 8 deletions __tests__/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const validDeploymentOrderMock = jest.spyOn(
validDeploymentOrder,
'validDeploymentOrder'
)
const createDeploymentMock = jest.fn().mockImplementation(() => {
return {
data: {id: 123}
}
})

const permissionsMsg =
'👋 __monalisa__, seems as if you have not admin/write permissions in this repo, permissions: read'
Expand Down Expand Up @@ -91,11 +96,7 @@ beforeEach(() => {
})
},
repos: {
createDeployment: jest.fn().mockImplementation(() => {
return {
data: {id: 123}
}
}),
createDeployment: createDeploymentMock,
createDeploymentStatus: jest.fn().mockImplementation(() => {
return {data: {}}
}),
Expand Down Expand Up @@ -207,7 +208,8 @@ test('successfully runs the action in noop mode', async () => {
ref: 'test-ref',
status: true,
message: '✔️ PR is approved and all CI checks passed - OK',
noopMode: true
noopMode: true,
sha: 'deadbeef'
}
})

Expand Down Expand Up @@ -818,7 +820,8 @@ test('successfully runs the action after trimming the body', async () => {
ref: 'test-ref',
status: true,
message: '✔️ PR is approved and all CI checks passed - OK',
noopMode: true
noopMode: true,
sha: 'deadbeef'
}
})
github.context.payload.comment.body = '.noop \n\t\n '
Expand Down Expand Up @@ -926,7 +929,8 @@ test('fails prechecks', async () => {
ref: 'test-ref',
status: false,
message: '### ⚠️ Cannot proceed with deployment... something went wrong',
noopMode: false
noopMode: false,
sha: 'deadbeef'
}
})
jest.spyOn(actionStatus, 'actionStatus').mockImplementation(() => {
Expand Down Expand Up @@ -1058,3 +1062,81 @@ test('handles and unexpected error and exits', async () => {
expect(setFailedMock.toHaveBeenCalled())
}
})

test('stores params and parsed params into context', async () => {
github.context.payload.comment.body = '.deploy | something1 --foo=bar'
const params = 'something1 --foo=bar'
const parsed_params = {
_: ['something1'],
foo: 'bar'
}
const data = expect.objectContaining({
auto_merge: true,
ref: 'test-ref',
environment: 'production',
owner: 'corp',
repo: 'test',
production_environment: true,
required_contexts: [],
payload: expect.objectContaining({
params,
parsed_params,
sha: null,
type: 'branch-deploy'
})
})
expect(await run()).toBe('success')
expect(createDeploymentMock).toHaveBeenCalledWith(data)
expect(setOutputMock).toHaveBeenCalledWith('params', params)
expect(setOutputMock).toHaveBeenCalledWith('parsed_params', parsed_params)
})

test('stores params and parsed params into context with complex params', async () => {
jest.spyOn(prechecks, 'prechecks').mockImplementation(() => {
return {
ref: 'test-ref',
status: true,
message: '✔️ PR is approved and all CI checks passed - OK',
noopMode: false,
sha: 'deadbeef'
}
})

github.context.payload.comment.body =
'.deploy | something1 --foo=bar --env.development=false --env.production=true LOG_LEVEL=debug,CPU_CORES=4 --config.db.host=localhost --config.db.port=5432'
const params =
'something1 --foo=bar --env.development=false --env.production=true LOG_LEVEL=debug,CPU_CORES=4 --config.db.host=localhost --config.db.port=5432'
const parsed_params = {
_: ['something1', 'LOG_LEVEL=debug,CPU_CORES=4'],
foo: 'bar',
env: {
development: 'false',
production: 'true'
},
config: {
db: {
host: 'localhost',
port: 5432
}
}
}
const data = expect.objectContaining({
auto_merge: true,
ref: 'test-ref',
environment: 'production',
owner: 'corp',
repo: 'test',
production_environment: true,
required_contexts: [],
payload: expect.objectContaining({
params,
parsed_params,
sha: 'deadbeef',
type: 'branch-deploy'
})
})
expect(await run()).toBe('success')
expect(createDeploymentMock).toHaveBeenCalledWith(data)
expect(setOutputMock).toHaveBeenCalledWith('params', params)
expect(setOutputMock).toHaveBeenCalledWith('parsed_params', parsed_params)
})
Loading

0 comments on commit 0bd0654

Please sign in to comment.