Skip to content

Commit 3c25661

Browse files
authored
feat(environment-templates): add latestOnly query param [COPS-1022] (#2790)
* feat(environment-templates): add latestOnly query param * test: add test * fix: pass latestOnly separately from query
1 parent b4a7878 commit 3c25661

File tree

6 files changed

+82
-17
lines changed

6 files changed

+82
-17
lines changed

lib/adapters/REST/endpoints/environment-template-installation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ const apiPath = (organizationId: string, ...pathSegments: (number | string)[]) =
77

88
export const getMany: RestEndpoint<'EnvironmentTemplateInstallation', 'getMany'> = (
99
http,
10-
{ organizationId, environmentTemplateId, spaceId, environmentId, ...paginationProps },
10+
{ organizationId, environmentTemplateId, spaceId, environmentId, ...otherProps },
1111
headers?: RawAxiosRequestHeaders,
1212
) =>
1313
raw.get(http, apiPath(organizationId, environmentTemplateId, 'template_installations'), {
1414
params: {
15-
...paginationProps,
15+
...otherProps,
1616
...(environmentId && { 'environment.sys.id': environmentId }),
1717
...(spaceId && { 'space.sys.id': spaceId }),
1818
},

lib/common-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,7 @@ export type MRActions = {
16001600
environmentTemplateId: string
16011601
organizationId: string
16021602
spaceId?: string
1603+
latestOnly?: boolean
16031604
}
16041605
return: CursorPaginatedCollectionProp<EnvironmentTemplateInstallationProps>
16051606
}

lib/create-environment-template-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export function createEnvironmentTemplateApi(makeRequest: MakeRequest, organizat
138138
* Gets a collection of all installations for the environment template
139139
* @param [installationParams.spaceId] - Space ID to filter installations by space and environment
140140
* @param [installationParams.environmentId] - Environment ID to filter installations by space and environment
141+
* @param [installationParams.latestOnly] - Boolean flag to only return the latest installation per environment
141142
* @return Promise for a collection of EnvironmentTemplateInstallations
142143
* ```javascript
143144
* const contentful = require('contentful-management')
@@ -157,10 +158,12 @@ export function createEnvironmentTemplateApi(makeRequest: MakeRequest, organizat
157158
getInstallations: function getEnvironmentTemplateInstallations({
158159
spaceId,
159160
environmentId,
161+
latestOnly,
160162
...query
161163
}: {
162164
spaceId?: string
163165
environmentId?: string
166+
latestOnly?: boolean
164167
} & BasicCursorPaginationOptions = {}) {
165168
const raw = this.toPlainObject() as EnvironmentTemplateProps
166169
return makeRequest({
@@ -172,6 +175,7 @@ export function createEnvironmentTemplateApi(makeRequest: MakeRequest, organizat
172175
query: { ...createRequestConfig({ query }).params },
173176
spaceId,
174177
environmentId,
178+
latestOnly,
175179
},
176180
}).then((data) => wrapEnvironmentTemplateInstallationCollection(makeRequest, data))
177181
},

lib/plain/common-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ export type PlainClientAPI = {
220220
environmentTemplateId: string
221221
organizationId: string
222222
spaceId?: string
223+
latestOnly?: boolean
223224
},
224225
headers?: RawAxiosRequestHeaders,
225226
): Promise<CursorPaginatedCollectionProp<EnvironmentTemplateInstallationProps>>

test/integration/environment-template-integration.test.ts

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import type {
1717
Space,
1818
} from '../../lib/export-types'
1919

20-
type InstallTemplate = () => Promise<EnvironmentTemplateInstallationProps>
20+
type InstallTemplate = (versionsCount?: number) => Promise<EnvironmentTemplateInstallationProps>
2121

2222
describe('Environment template API', () => {
2323
const client = defaultClient
@@ -213,14 +213,26 @@ describe('Environment template API', () => {
213213
expect(installation.sys.id).toBe(installations[0].sys.id)
214214
})
215215

216-
it('gets all installations of an environment template for an environment', async () => {
217-
const installation = await installTemplate()
216+
it('gets all installations of an environment template for a given environment', async () => {
217+
const installation = await installTemplate(2)
218218
const template = await client.getEnvironmentTemplate({
219219
organizationId: orgId,
220220
environmentTemplateId: installation.sys.template.sys.id,
221221
})
222222

223223
const { items: installations } = await template.getInstallations()
224+
expect(installations).toHaveLength(2)
225+
expect(installation.sys.id).toBe(installations[0].sys.id)
226+
})
227+
228+
it('gets only the latest installation of an environment template for a given environment', async () => {
229+
const installation = await installTemplate(2)
230+
const template = await client.getEnvironmentTemplate({
231+
organizationId: orgId,
232+
environmentTemplateId: installation.sys.template.sys.id,
233+
})
234+
235+
const { items: installations } = await template.getInstallations({ latestOnly: true })
224236
expect(installations).toHaveLength(1)
225237
expect(installation.sys.id).toBe(installations[0].sys.id)
226238
})
@@ -292,23 +304,41 @@ function createInstallTemplate({
292304
environment: Environment
293305
createDraftTemplate: () => CreateEnvironmentTemplateProps
294306
}): InstallTemplate {
295-
return async () => {
296-
const template = await client.createEnvironmentTemplate(orgId, createDraftTemplate())
297-
const installation = await template.install({
298-
spaceId: space.sys.id,
299-
environmentId: environment.sys.id,
300-
installation: {
301-
version: template.sys.version,
302-
},
303-
})
304-
305-
expect(installation.sys.template.sys.id).toBe(template.sys.id)
307+
return async (versionsCount: number = 1) => {
308+
let template = await client.createEnvironmentTemplate(orgId, createDraftTemplate())
309+
let installation = await installNewTemplateVersion(client, space, environment, template)
310+
311+
for (let version = 2; version <= versionsCount; version++) {
312+
template.name = `Updated name for version ${version}`
313+
template = await template.update()
314+
installation = await installNewTemplateVersion(client, space, environment, template)
315+
}
306316

307-
await waitForPendingInstallation(client, environment, template.sys.id)
308317
return installation
309318
}
310319
}
311320

321+
async function installNewTemplateVersion(
322+
client: ClientAPI,
323+
space: Space,
324+
environment: Environment,
325+
template: EnvironmentTemplate,
326+
): Promise<EnvironmentTemplateInstallationProps> {
327+
const installation = await template.install({
328+
spaceId: space.sys.id,
329+
environmentId: environment.sys.id,
330+
installation: {
331+
version: template.sys.version,
332+
},
333+
})
334+
335+
expect(installation.sys.template.sys.id).toBe(template.sys.id)
336+
337+
await waitForPendingInstallation(client, environment, template.sys.id)
338+
339+
return installation
340+
}
341+
312342
async function enableSpace(client: ClientAPI, space: Space): Promise<void> {
313343
const previousEnablements = await client.rawRequest({
314344
method: 'get',

test/unit/create-environment-template-api.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ describe('createEnvironmentTemplateApi', () => {
146146
})
147147
})
148148

149+
test('API call installations with latestOnly', async () => {
150+
const installations = [
151+
environmentTemplateInstallationMock,
152+
{
153+
sys: {
154+
...environmentTemplateInstallationMock.sys,
155+
space: makeLink('Space', 'anothermock-space-id'),
156+
},
157+
},
158+
]
159+
160+
const { api, makeRequest } = setup(Promise.resolve({ items: installations }))
161+
const [installation1, installation2] = (await api.getInstallations({ latestOnly: true })).items
162+
expect(installation1.toPlainObject()).to.eql(installations[0])
163+
expect(installation2.toPlainObject()).to.eql(installations[1])
164+
expect(makeRequest).toHaveBeenCalledWith({
165+
entityType: 'EnvironmentTemplateInstallation',
166+
action: 'getMany',
167+
params: {
168+
organizationId,
169+
environmentTemplateId: environmentTemplateMock.sys.id,
170+
spaceId: undefined,
171+
environmentId: undefined,
172+
latestOnly: true,
173+
query: {},
174+
},
175+
})
176+
})
177+
149178
test('API call validate', async () => {
150179
const version = 1
151180
const { api, makeRequest } = setup(Promise.resolve(environmentTemplateValidationMock))

0 commit comments

Comments
 (0)