Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOP-3903: API Changelog Support to omit entries #919

Merged
merged 21 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
50 changes: 48 additions & 2 deletions plugins/utils/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,37 @@ const fetchChangelogData = async (runId, versions, s3Prefix) => {
try {
/* Using metadata runId, fetch OpenAPI Changelog full change list */
const changelogResp = await fetch(`${s3Prefix}/${runId}/changelog.json`);
const changelog = await changelogResp.json();
const unfilteredChangelog = await changelogResp.json();

const hideChanges = (changelog) => {
const versionUpdate = (version) => {
const updatedVersion = { ...version };
if (version?.changes) {
updatedVersion.changes = version.changes.filter((change) => !change.hideFromChangelog);
}
return updatedVersion;
};

//pathUpdate takes the array of versions from the specific path passed in and takes each version and runs versionUpdate on it
const pathUpdate = (path) => {
const updatedPath = { ...path, versions: path.versions.map(versionUpdate) };
updatedPath.versions = updatedPath.versions.filter((version) => version.changes?.length);
return updatedPath;
};

//dateUpdate takes the array of paths from the specific date section passed in and takes each path and runs pathUpdate on it
const dateUpdate = (dateSection) => {
const updatedDateSection = { ...dateSection, paths: dateSection.paths.map(pathUpdate) };
updatedDateSection.paths = updatedDateSection.paths.filter((path) => path.versions?.length);
return updatedDateSection;
};

//changelog is the json file with everything in it. Map at this level takes each date section and runs dateUpdate on it
const updatedChangelog = changelog.map(dateUpdate);
return updatedChangelog.filter((dateSection) => dateSection.paths?.length);
};

const changelog = hideChanges(unfilteredChangelog);

/* Aggregate all Resources in changelog for frontend filter */
const resourcesListSet = new Set();
Expand All @@ -18,7 +48,23 @@ const fetchChangelogData = async (runId, versions, s3Prefix) => {
const mostRecentResourceVersions = versions.slice(-2);
const mostRecentDiffLabel = mostRecentResourceVersions.join('_');
const mostRecentDiffResp = await fetch(`${s3Prefix}/${runId}/${mostRecentDiffLabel}.json`);
const mostRecentDiffData = await mostRecentDiffResp.json();
const mostRecentDiffDataUnfiltered = await mostRecentDiffResp.json();

//nested filtering of Diff changes with hideFromChangelog
const hideDiffChanges = (diffData) => {
const pathUpdate = (path) => {
const updatedPath = { ...path };
if (path?.changes) {
updatedPath.changes = path.changes.filter((change) => !change.hideFromChangelog);
}
return updatedPath;
};

const updatedDiffData = diffData.map(pathUpdate);
return updatedDiffData.filter((path) => path.changes?.length);
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and reminder to have this function somewhere where it can be exported so we can reuse it rather than declaring it twice!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the functions, let me know what you think!


const mostRecentDiffData = hideDiffChanges(mostRecentDiffDataUnfiltered);

return {
changelog,
Expand Down
1 change: 1 addition & 0 deletions src/components/OpenAPIChangelog/OpenAPIChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const StyledLoadingSkeleton = styled.div`
const OpenAPIChangelog = () => {
const { snootyEnv } = useSiteMetadata();
const { index = {}, changelog = [], changelogResourcesList = [] } = useChangelogData();

const resourceVersions = index.versions?.length ? index.versions.slice().reverse() : [];
const downloadChangelogUrl = useMemo(() => getDownloadChangelogUrl(index.runId, snootyEnv), [index, snootyEnv]);

Expand Down
17 changes: 16 additions & 1 deletion src/components/OpenAPIChangelog/utils/useFetchDiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ import { fetchOADiff } from '../../../utils/realm';
import useChangelogData from '../../../utils/use-changelog-data';
import { getDiffRequestFormat } from './getDiffRequestFormat';

//nested filtering of Diff changes with hideFromChangelog
const hideDiffChanges = (diffData) => {
const pathUpdate = (path) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic looks like it's the same from the filtering of the changelog in gatsby-node.js. Maybe this function could be put into another file and then imported where needed.

const updatedPath = { ...path };
if (path?.changes) {
updatedPath.changes = path.changes.filter((change) => !change.hideFromChangelog);
}
return updatedPath;
};

const updatedDiffData = diffData.map(pathUpdate);
return updatedDiffData.filter((path) => path.changes?.length);
};

export const useFetchDiff = (resourceVersionOne, resourceVersionTwo, setIsLoading, setToastOpen, snootyEnv) => {
const { index = {}, mostRecentDiff = {} } = useChangelogData();
const [diff, setDiff] = useState([]);
Expand All @@ -20,7 +34,8 @@ export const useFetchDiff = (resourceVersionOne, resourceVersionTwo, setIsLoadin
setIsLoading(true);
fetchOADiff(index.runId, fromAndToDiffLabel, snootyEnv)
.then((response) => {
setDiff(response);
const filteredDiff = hideDiffChanges(response);
setDiff(filteredDiff);
setIsLoading(false);
})
.catch((err) => {
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/ChangeList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,38 @@ describe('OpenAPIChangelog ChangeList', () => {
expect(queryByText(change.change)).toBeNull();
});
});

it('does not render all version changelog changes with "hideFromChangelog=true"', () => {
const hiddenChanges = mockChangelog.reduce((acc, date) => {
date.paths.forEach((path) =>
path.versions.forEach((version) =>
version.changes.forEach((change) => change.hideFromChangelog && acc.push(change))
)
);
return acc;
}, []);

const { queryByText } = render(<ChangeList changes={mockDiff} versionMode={COMPARE_VERSIONS} />);

expect(hiddenChanges).toHaveLength(2);

hiddenChanges.forEach((change) => {
expect(queryByText(change.change)).toBeNull();
});
});

it('does not render Diff changelog changes with "hideFromChangelog=true"', () => {
const hiddenChanges = mockDiff.reduce((acc, resource) => {
resource.changes.forEach((change) => change.hideFromChangelog && acc.push(change));
return acc;
}, []);

const { queryByText } = render(<ChangeList changes={mockDiff} versionMode={COMPARE_VERSIONS} />);

expect(hiddenChanges).toHaveLength(2);

hiddenChanges.forEach((change) => {
expect(queryByText(change.change)).toBeNull();
});
});
});
Loading