Skip to content

Commit

Permalink
Merge pull request #33 from axelerant/issue-31
Browse files Browse the repository at this point in the history
Issue - Inactive environments cannot be deactivated
  • Loading branch information
zeshanziya authored Jul 5, 2024
2 parents d343f5c + 715ab99 commit c83833b
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 20 deletions.
130 changes: 124 additions & 6 deletions __tests__/clean-pr-env.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,43 @@
import * as core from '@actions/core'
import Client from 'platformsh-client'
import { getAccessToken } from './../src/utils'
import * as github from '@actions/github'
import { cleanPrEnv } from './../src/clean-pr-env'

const mockClient = {
getEnvironment: jest.fn()
}

const mockEnvironmentResult = jest
.fn()
.mockImplementation((status: string, type = 'development') => ({
name: '123/merge',
type,
status,
deactivate: jest.fn().mockResolvedValue({ wait: jest.fn() }),
delete: jest.fn().mockResolvedValue({})
}))

jest.mock('@actions/core')
jest.mock('platformsh-client')
jest.mock('../src/utils')
jest.mock('../src/utils', () => ({
getCliClient: jest.fn().mockImplementation(() => mockClient)
}))
jest.mock('@actions/github')

describe('cleanPrEnv', () => {
let getInputMock: jest.SpiedFunction<typeof core.getInput>

beforeEach(() => {
jest.clearAllMocks()
getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
getInputMock.mockImplementation(name => {
switch (name) {
case 'project-id':
return 'project-id'
case 'cli-token':
return 'cli-token'
default:
return ''
}
})
})

it('should not clean the PR environment if the PR number is not available', async () => {
Expand All @@ -24,8 +50,100 @@ describe('cleanPrEnv', () => {
expect(core.warning).toHaveBeenCalledWith(
`Unable to identify the PR. Please run this action only on pull_request closed event.`
)
expect(getAccessToken).not.toHaveBeenCalled()
expect(Client).not.toHaveBeenCalled()
expect(core.endGroup).toHaveBeenCalled()
})

it('should deactivate and delete an active environment', async () => {
const mockEnvResult = mockEnvironmentResult('active')
mockClient.getEnvironment.mockResolvedValue(mockEnvResult)
github.context.payload.pull_request = { number: 123 }

await cleanPrEnv()

expect(mockClient.getEnvironment).toHaveBeenCalledWith(
'project-id',
'123%2Fmerge'
)
expect(mockEnvResult.deactivate).toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
`Deactivating 123/merge environment...`
)
expect(mockEnvResult.delete).toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
`123/merge environment deleted successfully.`
)
expect(core.endGroup).toHaveBeenCalled()
})

it('should delete an inactive environment', async () => {
const mockEnvResult = mockEnvironmentResult('inactive')
mockClient.getEnvironment.mockResolvedValue(mockEnvResult)
github.context.payload.pull_request = { number: 123 }

await cleanPrEnv()

expect(mockClient.getEnvironment).toHaveBeenCalledWith(
'project-id',
'123%2Fmerge'
)
expect(mockEnvResult.deactivate).not.toHaveBeenCalled()
expect(mockEnvResult.delete).toHaveBeenCalled()
expect(core.info).toHaveBeenCalledWith(
`123/merge environment deleted successfully.`
)
expect(core.endGroup).toHaveBeenCalled()
})

it('should warn and not delete non-development environment', async () => {
const mockEnvResult = mockEnvironmentResult('active', 'production')
mockClient.getEnvironment.mockResolvedValue(mockEnvResult)
github.context.payload.pull_request = { number: 123 }

await cleanPrEnv()

expect(mockClient.getEnvironment).toHaveBeenCalledWith(
'project-id',
'123%2Fmerge'
)
expect(mockEnvResult.deactivate).not.toHaveBeenCalled()
expect(mockEnvResult.delete).not.toHaveBeenCalled()
expect(core.warning).toHaveBeenCalledWith(
`Not deleting 123/merge environment as it's not a development environment`
)
expect(core.endGroup).toHaveBeenCalled()
})

it('should handle unexpected environment status', async () => {
const mockEnvResult = mockEnvironmentResult('dirty')
mockClient.getEnvironment.mockResolvedValue(mockEnvResult)
github.context.payload.pull_request = { number: 123 }

await cleanPrEnv()

expect(mockClient.getEnvironment).toHaveBeenCalledWith(
'project-id',
'123%2Fmerge'
)
expect(mockEnvResult.deactivate).not.toHaveBeenCalled()
expect(mockEnvResult.delete).not.toHaveBeenCalled()
expect(core.warning).toHaveBeenCalledWith(
`Unable to delete 123/merge environment as it's already in dirty mode`
)
expect(core.endGroup).toHaveBeenCalled()
})

it('should handle error during getEnvironment call', async () => {
const errorMessage = 'Failed to get environment details'
mockClient.getEnvironment.mockRejectedValue(new Error(errorMessage))
github.context.payload.pull_request = { number: 123 }

await cleanPrEnv()

expect(mockClient.getEnvironment).toHaveBeenCalledWith(
'project-id',
'123%2Fmerge'
)
expect(core.warning).toHaveBeenCalledWith(errorMessage)
expect(core.endGroup).toHaveBeenCalled()
})
})
39 changes: 25 additions & 14 deletions src/clean-pr-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,30 @@ export async function cleanPrEnv(): Promise<void> {
return
}

// Deactivate env first.
const activity = await envResult.deactivate()
core.info(`Deactivating ${prRef} environment...`)
// @todo display activity log
await activity.wait()
core.info(`${prRef} environment deactivated successfully.`)

// Fetch again for deletion as active env can not be deleted and current resource points to active env.
const envResultDelete = await client.getEnvironment(
core.getInput('project-id'),
encodeURIComponent(prRef)
)
await envResultDelete.delete()
core.info(`${prRef} environment deleted successfully.`)
// Check the status of the environment.
if (envResult.status === 'active' || envResult.status === 'paused') {
const activity = await envResult.deactivate()
core.info(`Deactivating ${prRef} environment...`)
// @todo display activity log
await activity.wait()
core.info(`${prRef} environment deactivated successfully.`)

// Fetch again for deletion as active env can not be deleted and current resource points to active env.
const envResultDelete = await client.getEnvironment(
core.getInput('project-id'),
encodeURIComponent(prRef)
)
await envResultDelete.delete()
core.info(`${prRef} environment deleted successfully.`)
} else if (envResult.status === 'inactive') {
// Delete the environment directly if it's inactive.
await envResult.delete()
core.info(`${prRef} environment deleted successfully.`)
} else {
// Handle dirty and deleting state.
core.warning(
`Unable to delete ${prRef} environment as it's already in ${envResult.status} mode`
)
}
core.endGroup()
}

0 comments on commit c83833b

Please sign in to comment.