Skip to content

Commit

Permalink
chore: aligned release retrieval with v2 api models
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Zedel <[email protected]>
  • Loading branch information
mzedel committed Sep 25, 2023
1 parent 6bd051c commit 92be11f
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 106 deletions.
2 changes: 1 addition & 1 deletion src/js/actions/appActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('app actions', () => {
{ type: RECEIVE_RELEASES, releases: defaultState.releases.byId },
{
type: SET_RELEASES_LIST_STATE,
value: { ...defaultState.releases.releasesList, releaseIds: [defaultState.releases.byId.r1.Name], page: 42 }
value: { ...defaultState.releases.releasesList, releaseIds: [defaultState.releases.byId.r1.name], page: 42 }
},
{ type: SET_DEVICE_LIMIT, limit: 500 },
{ type: RECEIVED_PERMISSION_SETS, value: receivedPermissionSets },
Expand Down
48 changes: 24 additions & 24 deletions src/js/actions/releaseActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,34 @@ import { convertDeviceListStateToFilters, getSearchEndpoint } from './deviceActi
const { page: defaultPage, perPage: defaultPerPage } = DEVICE_LIST_DEFAULTS;

const flattenRelease = (release, stateRelease) => {
const updatedArtifacts = release.Artifacts?.sort(customSort(1, 'modified')) || [];
const { Artifacts, deviceTypes, modified } = updatedArtifacts.reduce(
const updatedArtifacts = release.artifacts?.sort(customSort(1, 'modified')) || [];
const { artifacts, deviceTypes, modified } = updatedArtifacts.reduce(
(accu, item) => {
accu.deviceTypes.push(...item.device_types_compatible);
const stateArtifact = stateRelease.Artifacts?.find(releaseArtifact => releaseArtifact.id === item.id) || {};
const stateArtifact = stateRelease.artifacts?.find(releaseArtifact => releaseArtifact.id === item.id) || {};
accu.modified = accu.modified ? accu.modified : item.modified;
accu.Artifacts.push({
accu.artifacts.push({
...stateArtifact,
...item
});
return accu;
},
{ Artifacts: [], deviceTypes: [], modified: undefined }
{ artifacts: [], deviceTypes: [], modified: undefined }
);
return { ...stateRelease, ...release, Artifacts, device_types_compatible: deviceTypes.filter(duplicateFilter), modified };
return { ...stateRelease, ...release, artifacts, device_types_compatible: deviceTypes.filter(duplicateFilter), modified };
};

const reduceReceivedReleases = (releases, stateReleasesById) =>
releases.reduce((accu, release) => {
const stateRelease = stateReleasesById[release.Name] || {};
accu[release.Name] = flattenRelease(release, stateRelease);
const stateRelease = stateReleasesById[release.name] || {};
accu[release.name] = flattenRelease(release, stateRelease);
return accu;
}, {});

const findArtifactIndexInRelease = (releases, id) =>
Object.values(releases).reduce(
(accu, item) => {
let index = item.Artifacts.findIndex(releaseArtifact => releaseArtifact.id === id);
let index = item.artifacts.findIndex(releaseArtifact => releaseArtifact.id === id);
if (index > -1) {
accu = { release: item, index };
}
Expand All @@ -69,7 +69,7 @@ export const getArtifactInstallCount = id => (dispatch, getState) => {
if (!release || index === -1) {
return;
}
const releaseArtifacts = [...release.Artifacts];
const releaseArtifacts = [...release.artifacts];
const artifact = releaseArtifacts[index];
const { key, name, version } = extractSoftwareItem(artifact.artifact_provides) ?? {};
const attribute = `${key}${name ? `.${name}` : ''}.version`;
Expand All @@ -89,11 +89,11 @@ export const getArtifactInstallCount = id => (dispatch, getState) => {
return;
}
const installCount = Number(headers[headerNames.total]);
const releaseArtifacts = [...release.Artifacts];
const releaseArtifacts = [...release.artifacts];
releaseArtifacts[index] = { ...releaseArtifacts[index], installCount };
release = {
...release,
Artifacts: releaseArtifacts
artifacts: releaseArtifacts
};
return dispatch({ type: ReleaseConstants.RECEIVE_RELEASE, release });
});
Expand All @@ -106,14 +106,14 @@ export const getArtifactUrl = id => (dispatch, getState) =>
if (!release || index === -1) {
return dispatch(getReleases());
}
const releaseArtifacts = [...release.Artifacts];
const releaseArtifacts = [...release.artifacts];
releaseArtifacts[index] = {
...releaseArtifacts[index],
url: response.data.uri
};
release = {
...release,
Artifacts: releaseArtifacts
artifacts: releaseArtifacts
};
return dispatch({ type: ReleaseConstants.ARTIFACTS_SET_ARTIFACT_URL, release });
});
Expand Down Expand Up @@ -212,12 +212,12 @@ export const editArtifact = (id, body) => (dispatch, getState) =>
if (!release || index === -1) {
return dispatch(getReleases());
}
release.Artifacts[index].description = body.description;
release.artifacts[index].description = body.description;
return Promise.all([
dispatch({ type: ReleaseConstants.UPDATED_ARTIFACT, release }),
dispatch(setSnackbar('Artifact details were updated successfully.', TIMEOUTS.fiveSeconds, '')),
dispatch(getRelease(release.Name)),
dispatch(selectRelease(release.Name))
dispatch(getRelease(release.name)),
dispatch(selectRelease(release.name))
]);
});

Expand All @@ -226,13 +226,13 @@ export const removeArtifact = id => (dispatch, getState) =>
.then(() => {
const state = getState();
let { release, index } = findArtifactIndexInRelease(state.releases.byId, id);
const releaseArtifacts = [...release.Artifacts];
const releaseArtifacts = [...release.artifacts];
releaseArtifacts.splice(index, 1);
if (!releaseArtifacts.length) {
const { releasesList } = state.releases;
const releaseIds = releasesList.releaseIds.filter(id => release.Name !== id);
const releaseIds = releasesList.releaseIds.filter(id => release.name !== id);
return Promise.all([
dispatch({ type: ReleaseConstants.RELEASE_REMOVED, release: release.Name }),
dispatch({ type: ReleaseConstants.RELEASE_REMOVED, release: release.name }),
dispatch(
setReleasesListState({
releaseIds,
Expand All @@ -250,10 +250,10 @@ export const removeArtifact = id => (dispatch, getState) =>
.catch(err => commonErrorHandler(err, `Error removing artifact:`, dispatch));

export const removeRelease = id => (dispatch, getState) =>
Promise.all(getState().releases.byId[id].Artifacts.map(({ id }) => dispatch(removeArtifact(id)))).then(() => dispatch(selectRelease()));
Promise.all(getState().releases.byId[id].artifacts.map(({ id }) => dispatch(removeArtifact(id)))).then(() => dispatch(selectRelease()));

export const selectRelease = release => dispatch => {
const name = release ? release.Name || release : null;
const name = release ? release.name || release : null;
let tasks = [dispatch({ type: ReleaseConstants.SELECTED_RELEASE, release: name })];
if (name) {
tasks.push(dispatch(getRelease(name)));
Expand Down Expand Up @@ -298,7 +298,7 @@ const deductSearchState = (receivedReleases, config, total, state) => {
let releaseListState = { ...state.releasesList };
const { searchTerm, searchOnly, sort = {}, tags = [], type } = config;
const flattenedReleases = Object.values(receivedReleases).sort(customSort(sort.direction === SORTING_OPTIONS.desc, sort.key));
const releaseIds = flattenedReleases.map(item => item.Name);
const releaseIds = flattenedReleases.map(item => item.name);
const isFiltering = !!(tags.length || type || searchTerm);
if (searchOnly) {
releaseListState = { ...releaseListState, searchedIds: releaseIds };
Expand Down Expand Up @@ -335,7 +335,7 @@ export const getReleases =
export const getRelease = name => (dispatch, getState) =>
GeneralApi.get(`${deploymentsApiUrl}/deployments/releases?name=${name}`).then(({ data: releases }) => {
if (releases.length) {
const stateRelease = getState().releases.byId[releases[0].Name] || {};
const stateRelease = getState().releases.byId[releases[0].name] || {};
return Promise.resolve(dispatch({ type: ReleaseConstants.RECEIVE_RELEASE, release: flattenRelease(releases[0], stateRelease) }));
}
return Promise.resolve(null);
Expand Down
34 changes: 17 additions & 17 deletions src/js/actions/releaseActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('release actions', () => {
const store = mockStore({ ...defaultState });
store.clearActions();
const expectedActions = [{ type: ReleaseConstants.RECEIVE_RELEASE, release: defaultState.releases.byId.r1 }];
await store.dispatch(getRelease(defaultState.releases.byId.r1.Name));
await store.dispatch(getRelease(defaultState.releases.byId.r1.name));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
Expand All @@ -80,7 +80,7 @@ describe('release actions', () => {
value: { ...defaultState.releases.releasesList, releaseIds: ['release-1'], total: 5000 }
}
];
await store.dispatch(getReleases({ perPage: 1, sort: { direction: 'asc', key: 'Name' } }));
await store.dispatch(getReleases({ perPage: 1, sort: { direction: 'asc', key: 'name' } }));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
Expand Down Expand Up @@ -138,7 +138,7 @@ describe('release actions', () => {
type: ReleaseConstants.RECEIVE_RELEASE,
release: {
...defaultState.releases.byId.r1,
Artifacts: [{ ...defaultState.releases.byId.r1.Artifacts[0], installCount: 0 }]
artifacts: [{ ...defaultState.releases.byId.r1.artifacts[0], installCount: 0 }]
}
}
];
Expand All @@ -155,9 +155,9 @@ describe('release actions', () => {
type: ReleaseConstants.ARTIFACTS_SET_ARTIFACT_URL,
release: {
...defaultState.releases.byId.r1,
Artifacts: [
artifacts: [
{
...defaultState.releases.byId.r1.Artifacts[0],
...defaultState.releases.byId.r1.artifacts[0],
url: 'https://testlocation.com/artifact.mender'
}
]
Expand All @@ -172,9 +172,9 @@ describe('release actions', () => {
});
it('should select a release by name', async () => {
const store = mockStore({ ...defaultState });
await store.dispatch(selectRelease(defaultState.releases.byId.r1.Name));
await store.dispatch(selectRelease(defaultState.releases.byId.r1.name));
const expectedActions = [
{ type: ReleaseConstants.SELECTED_RELEASE, release: defaultState.releases.byId.r1.Name },
{ type: ReleaseConstants.SELECTED_RELEASE, release: defaultState.releases.byId.r1.name },
{ type: ReleaseConstants.RECEIVE_RELEASE, release: defaultState.releases.byId.r1 }
];
const storeActions = store.getActions();
Expand Down Expand Up @@ -206,15 +206,15 @@ describe('release actions', () => {
type: ReleaseConstants.UPDATED_ARTIFACT,
release: {
...defaultState.releases.byId.r1,
Artifacts: [{ ...defaultState.releases.byId.r1.Artifacts[0], description: 'something new' }]
artifacts: [{ ...defaultState.releases.byId.r1.artifacts[0], description: 'something new' }]
}
},
{ type: AppConstants.SET_SNACKBAR, snackbar: { message: 'Artifact details were updated successfully.' } },
{ type: ReleaseConstants.SELECTED_RELEASE, release: defaultState.releases.byId.r1.Name },
{ type: ReleaseConstants.SELECTED_RELEASE, release: defaultState.releases.byId.r1.name },
{ type: ReleaseConstants.RECEIVE_RELEASE, release: defaultState.releases.byId.r1 },
{ type: ReleaseConstants.RECEIVE_RELEASE, release: defaultState.releases.byId.r1 }
];
await store.dispatch(editArtifact(defaultState.releases.byId.r1.Artifacts[0].id, { description: 'something new' }));
await store.dispatch(editArtifact(defaultState.releases.byId.r1.artifacts[0].id, { description: 'something new' }));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
Expand All @@ -228,21 +228,21 @@ describe('release actions', () => {
uploads: { 'mock-uuid': { cancelSource: mockAbortController, name: undefined, size: 1234, uploadProgress: 0 } }
},
{ type: AppConstants.SET_SNACKBAR, snackbar: { message: 'Upload successful' } },
{ type: ReleaseConstants.SELECTED_RELEASE, release: defaultState.releases.byId.r1.Name },
{ type: ReleaseConstants.SELECTED_RELEASE, release: defaultState.releases.byId.r1.name },
{ type: ReleaseConstants.RECEIVE_RELEASES, releases: defaultState.releases.byId },
{ type: ReleaseConstants.SET_RELEASES_LIST_STATE, value: { ...defaultState.releases.releasesList, releaseIds: retrievedReleaseIds, total: 5000 } },
{ type: ReleaseConstants.RECEIVE_RELEASE, release: defaultState.releases.byId.r1 },
{ type: AppConstants.UPLOAD_PROGRESS, uploads: {} }
];
await store.dispatch(uploadArtifact({ description: 'new artifact to upload', name: defaultState.releases.byId.r1.Name }, { size: 1234 }));
await store.dispatch(uploadArtifact({ description: 'new artifact to upload', name: defaultState.releases.byId.r1.name }, { size: 1234 }));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
});
it('should remove an artifact by name', async () => {
const store = mockStore({ ...defaultState });
const expectedActions = [
{ type: ReleaseConstants.RELEASE_REMOVED, release: defaultState.releases.byId.r1.Name },
{ type: ReleaseConstants.RELEASE_REMOVED, release: defaultState.releases.byId.r1.name },
{ type: ReleaseConstants.SET_RELEASES_LIST_STATE, value: { ...defaultState.releases.releasesList, isLoading: true, releaseIds: [], total: 0 } },
{ type: ReleaseConstants.RECEIVE_RELEASES, releases: defaultState.releases.byId },
{
Expand All @@ -259,7 +259,7 @@ describe('release actions', () => {
it('should remove a release by name', async () => {
const store = mockStore({ ...defaultState });
const expectedActions = [
{ type: ReleaseConstants.RELEASE_REMOVED, release: defaultState.releases.byId.r1.Name },
{ type: ReleaseConstants.RELEASE_REMOVED, release: defaultState.releases.byId.r1.name },
{ type: ReleaseConstants.SET_RELEASES_LIST_STATE, value: { ...defaultState.releases.releasesList, isLoading: true, releaseIds: [], total: 0 } },
{ type: ReleaseConstants.RECEIVE_RELEASES, releases: defaultState.releases.byId },
{
Expand All @@ -269,7 +269,7 @@ describe('release actions', () => {
{ type: ReleaseConstants.SET_RELEASES_LIST_STATE, value: { ...defaultState.releases.releasesList } },
{ type: ReleaseConstants.SELECTED_RELEASE, release: null }
];
await store.dispatch(removeRelease(defaultState.releases.byId.r1.Name));
await store.dispatch(removeRelease(defaultState.releases.byId.r1.name));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
Expand Down Expand Up @@ -299,7 +299,7 @@ describe('release actions', () => {
},
{ type: AppConstants.SET_SNACKBAR, snackbar: { message: 'Release tags were set successfully.' } }
];
await store.dispatch(setReleaseTags(defaultState.releases.byId.r1.Name, ['foo', 'bar']));
await store.dispatch(setReleaseTags(defaultState.releases.byId.r1.name, ['foo', 'bar']));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
Expand All @@ -313,7 +313,7 @@ describe('release actions', () => {
},
{ type: AppConstants.SET_SNACKBAR, snackbar: { message: 'Release details were updated successfully.' } }
];
await store.dispatch(updateReleaseInfo(defaultState.releases.byId.r1.Name, { notes: 'this & that' }));
await store.dispatch(updateReleaseInfo(defaultState.releases.byId.r1.name, { notes: 'this & that' }));
const storeActions = store.getActions();
expect(storeActions.length).toEqual(expectedActions.length);
expectedActions.map((action, index) => expect(storeActions[index]).toMatchObject(action));
Expand Down
6 changes: 3 additions & 3 deletions src/js/components/deployments/createdeployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export const CreateDeployment = props => {
}
setDeploymentSettings(nextDeploymentObject);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [acceptedDeviceCount, deploymentObject.group, deploymentObject.release?.Name, dispatch, JSON.stringify(groups), setDeploymentSettings]);
}, [acceptedDeviceCount, deploymentObject.group, deploymentObject.release?.name, dispatch, JSON.stringify(groups), setDeploymentSettings]);

useEffect(() => {
let { deploymentDeviceCount: deviceCount, deploymentDeviceIds: deviceIds = [], devices = [] } = deploymentObject;
Expand Down Expand Up @@ -204,7 +204,7 @@ export const CreateDeployment = props => {
const startTime = phases?.length ? phases[0].start_ts : undefined;
const retrySetting = canRetry && retries ? { retries } : {};
const newDeployment = {
artifact_name: release.Name,
artifact_name: release.name,
autogenerate_delta: delta,
devices: (filterId || group) && !devices.length ? undefined : deploymentDeviceIds,
filter_id: filterId,
Expand Down Expand Up @@ -323,7 +323,7 @@ export const CreateDeployment = props => {
classes="confirmation-overlay"
cancel={() => setIsChecking(false)}
action={() => onScheduleSubmitClick(deploymentSettings)}
message={`This will deploy ${deploymentSettings.release?.Name} to ${deploymentDeviceCount} ${pluralize(
message={`This will deploy ${deploymentSettings.release?.name} to ${deploymentDeviceCount} ${pluralize(
'device',
deploymentDeviceCount
)}. Are you sure?`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const RolloutStepConnector = ({ disabled, step, onStepChange, release = {

const pauseChip = <Chip className={classes.chip} icon={<PauseIcon />} label="Pause" {...stepModifier.props} />;
const stepPauseChip =
step.state === defaultSteps.ArtifactReboot_Enter.state && release.Name?.includes(menderDemoArtifactName) ? (
step.state === defaultSteps.ArtifactReboot_Enter.state && release.name?.includes(menderDemoArtifactName) ? (
<MenderTooltip
arrow
leaveDelay={TIMEOUTS.oneSecond}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ export const Software = ({ commonClasses, deploymentObject, releaseRef, releases
<div className={commonClasses.columns}>
<div ref={releaseRef} className={classes.selection}>
{releaseSelectionLocked ? (
<TextField value={deploymentRelease?.Name} label="Release" disabled={true} className={classes.infoStyle} />
<TextField value={deploymentRelease?.name} label="Release" disabled={true} className={classes.infoStyle} />
) : (
<AsyncAutocomplete
id="deployment-release-selection"
initialValue={deploymentRelease?.Name}
labelAttribute="Name"
initialValue={deploymentRelease?.name}
labelAttribute="name"
placeholder="Select a Release"
selectionAttribute="Name"
selectionAttribute="name"
options={releaseItems}
onChange={onReleaseInputChange}
onChangeSelection={onReleaseSelectionChange}
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/deployments/deployments.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export const Deployments = () => {
}
const { deploymentObject = {}, id: selectedId = [], ...remainder } = locationParams;
const { devices: selectedDevices = [], release: releaseName } = deploymentObject;
const release = releaseName ? { ...(releases[releaseName] ?? { Name: releaseName }) } : undefined;
const release = releaseName ? { ...(releases[releaseName] ?? { name: releaseName }) } : undefined;
const devices = selectedDevices.length ? selectedDevices.map(device => ({ ...device, ...devicesById[device.id] })) : [];
setDeploymentObject({ devices, release, releaseSelectionLocked: !!release });
dispatch(setDeploymentsState({ selectedId: selectedId[0], ...remainder }));
Expand Down
Loading

0 comments on commit 92be11f

Please sign in to comment.