Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ const apiPath = (organizationId: string, ...pathSegments: (number | string)[]) =

export const getMany: RestEndpoint<'EnvironmentTemplateInstallation', 'getMany'> = (
http,
{ organizationId, environmentTemplateId, spaceId, environmentId, ...paginationProps },
{ organizationId, environmentTemplateId, spaceId, environmentId, ...otherProps },
headers?: RawAxiosRequestHeaders,
) =>
raw.get(http, apiPath(organizationId, environmentTemplateId, 'template_installations'), {
params: {
...paginationProps,
...otherProps,
...(environmentId && { 'environment.sys.id': environmentId }),
...(spaceId && { 'space.sys.id': spaceId }),
},
Expand Down
1 change: 1 addition & 0 deletions lib/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1600,6 +1600,7 @@ export type MRActions = {
environmentTemplateId: string
organizationId: string
spaceId?: string
latestOnly?: boolean
}
return: CursorPaginatedCollectionProp<EnvironmentTemplateInstallationProps>
}
Expand Down
4 changes: 4 additions & 0 deletions lib/create-environment-template-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export function createEnvironmentTemplateApi(makeRequest: MakeRequest, organizat
* Gets a collection of all installations for the environment template
* @param [installationParams.spaceId] - Space ID to filter installations by space and environment
* @param [installationParams.environmentId] - Environment ID to filter installations by space and environment
* @param [installationParams.latestOnly] - Boolean flag to only return the latest installation per environment
* @return Promise for a collection of EnvironmentTemplateInstallations
* ```javascript
* const contentful = require('contentful-management')
Expand All @@ -157,10 +158,12 @@ export function createEnvironmentTemplateApi(makeRequest: MakeRequest, organizat
getInstallations: function getEnvironmentTemplateInstallations({
spaceId,
environmentId,
latestOnly,
...query
}: {
spaceId?: string
environmentId?: string
latestOnly?: boolean
} & BasicCursorPaginationOptions = {}) {
const raw = this.toPlainObject() as EnvironmentTemplateProps
return makeRequest({
Expand All @@ -172,6 +175,7 @@ export function createEnvironmentTemplateApi(makeRequest: MakeRequest, organizat
query: { ...createRequestConfig({ query }).params },
spaceId,
environmentId,
latestOnly,
},
}).then((data) => wrapEnvironmentTemplateInstallationCollection(makeRequest, data))
},
Expand Down
1 change: 1 addition & 0 deletions lib/plain/common-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export type PlainClientAPI = {
environmentTemplateId: string
organizationId: string
spaceId?: string
latestOnly?: boolean
},
headers?: RawAxiosRequestHeaders,
): Promise<CursorPaginatedCollectionProp<EnvironmentTemplateInstallationProps>>
Expand Down
60 changes: 45 additions & 15 deletions test/integration/environment-template-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
Space,
} from '../../lib/export-types'

type InstallTemplate = () => Promise<EnvironmentTemplateInstallationProps>
type InstallTemplate = (versionsCount?: number) => Promise<EnvironmentTemplateInstallationProps>

describe('Environment template API', () => {
const client = defaultClient
Expand Down Expand Up @@ -213,14 +213,26 @@ describe('Environment template API', () => {
expect(installation.sys.id).toBe(installations[0].sys.id)
})

it('gets all installations of an environment template for an environment', async () => {
const installation = await installTemplate()
it('gets all installations of an environment template for a given environment', async () => {
const installation = await installTemplate(2)
const template = await client.getEnvironmentTemplate({
organizationId: orgId,
environmentTemplateId: installation.sys.template.sys.id,
})

const { items: installations } = await template.getInstallations()
expect(installations).toHaveLength(2)
expect(installation.sys.id).toBe(installations[0].sys.id)
})

it('gets only the latest installation of an environment template for a given environment', async () => {
const installation = await installTemplate(2)
const template = await client.getEnvironmentTemplate({
organizationId: orgId,
environmentTemplateId: installation.sys.template.sys.id,
})

const { items: installations } = await template.getInstallations({ latestOnly: true })
expect(installations).toHaveLength(1)
expect(installation.sys.id).toBe(installations[0].sys.id)
})
Expand Down Expand Up @@ -292,23 +304,41 @@ function createInstallTemplate({
environment: Environment
createDraftTemplate: () => CreateEnvironmentTemplateProps
}): InstallTemplate {
return async () => {
const template = await client.createEnvironmentTemplate(orgId, createDraftTemplate())
const installation = await template.install({
spaceId: space.sys.id,
environmentId: environment.sys.id,
installation: {
version: template.sys.version,
},
})

expect(installation.sys.template.sys.id).toBe(template.sys.id)
return async (versionsCount: number = 1) => {
let template = await client.createEnvironmentTemplate(orgId, createDraftTemplate())
let installation = await installNewTemplateVersion(client, space, environment, template)

for (let version = 2; version <= versionsCount; version++) {
template.name = `Updated name for version ${version}`
template = await template.update()
installation = await installNewTemplateVersion(client, space, environment, template)
}

await waitForPendingInstallation(client, environment, template.sys.id)
return installation
}
}

async function installNewTemplateVersion(
client: ClientAPI,
space: Space,
environment: Environment,
template: EnvironmentTemplate,
): Promise<EnvironmentTemplateInstallationProps> {
const installation = await template.install({
spaceId: space.sys.id,
environmentId: environment.sys.id,
installation: {
version: template.sys.version,
},
})

expect(installation.sys.template.sys.id).toBe(template.sys.id)

await waitForPendingInstallation(client, environment, template.sys.id)

return installation
}

async function enableSpace(client: ClientAPI, space: Space): Promise<void> {
const previousEnablements = await client.rawRequest({
method: 'get',
Expand Down
29 changes: 29 additions & 0 deletions test/unit/create-environment-template-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ describe('createEnvironmentTemplateApi', () => {
})
})

test('API call installations with latestOnly', async () => {
const installations = [
environmentTemplateInstallationMock,
{
sys: {
...environmentTemplateInstallationMock.sys,
space: makeLink('Space', 'anothermock-space-id'),
},
},
]

const { api, makeRequest } = setup(Promise.resolve({ items: installations }))
const [installation1, installation2] = (await api.getInstallations({ latestOnly: true })).items
expect(installation1.toPlainObject()).to.eql(installations[0])
expect(installation2.toPlainObject()).to.eql(installations[1])
expect(makeRequest).toHaveBeenCalledWith({
entityType: 'EnvironmentTemplateInstallation',
action: 'getMany',
params: {
organizationId,
environmentTemplateId: environmentTemplateMock.sys.id,
spaceId: undefined,
environmentId: undefined,
latestOnly: true,
query: {},
},
})
})

test('API call validate', async () => {
const version = 1
const { api, makeRequest } = setup(Promise.resolve(environmentTemplateValidationMock))
Expand Down