Commit 7b9b2ddc authored by Zamir Martins Filho's avatar Zamir Martins Filho

Remove empty approvers in regards to

user_approvers_ids and group_approvers_ids.

Changelog: fixed
EE: true
parent b1088a03
import { omitBy, isEmpty } from 'lodash';
export const USER_TYPE = 'user'; export const USER_TYPE = 'user';
const GROUP_TYPE = 'group'; const GROUP_TYPE = 'group';
...@@ -41,12 +43,13 @@ export function groupApprovers(existingApprovers) { ...@@ -41,12 +43,13 @@ export function groupApprovers(existingApprovers) {
Convert approvers into yaml fields (user_approvers, users_approvers_ids) in relation to action. Convert approvers into yaml fields (user_approvers, users_approvers_ids) in relation to action.
*/ */
export function decomposeApprovers(action, approvers) { export function decomposeApprovers(action, approvers) {
const newAction = { ...action }; const newAction = { type: action.type, approvals_required: action.approvals_required };
delete newAction.group_approvers; const approversInfo = omitBy(
delete newAction.user_approvers; {
return { user_approvers_ids: userIds(approvers),
...newAction, group_approvers_ids: groupIds(approvers),
user_approvers_ids: userIds(approvers), },
group_approvers_ids: groupIds(approvers), isEmpty,
}; );
return { ...newAction, ...approversInfo };
} }
...@@ -25,12 +25,19 @@ const groupApprover = { ...@@ -25,12 +25,19 @@ const groupApprover = {
web_url: null, web_url: null,
}; };
const actionDataWithoutApprovers = {
approvals_required: 1,
type: 'require_approval',
};
const unknownApprover = { id: 3, name: null }; const unknownApprover = { id: 3, name: null };
const allApprovers = [userApprover, groupApprover]; const allApprovers = [userApprover, groupApprover];
const groupedApprovers = groupApprovers(allApprovers); const groupedApprovers = groupApprovers(allApprovers);
const userOnlyGroupedApprovers = groupApprovers([userApprover]);
describe('groupApprovers', () => { describe('groupApprovers', () => {
describe('with mixed approvers', () => { describe('with mixed approvers', () => {
it('returns a copy of the input values with their proper type attribute', () => { it('returns a copy of the input values with their proper type attribute', () => {
...@@ -104,7 +111,8 @@ describe('groupApprovers', () => { ...@@ -104,7 +111,8 @@ describe('groupApprovers', () => {
describe('decomposeApprovers', () => { describe('decomposeApprovers', () => {
it('returns a copy of approvers adding id fields for both group and users', () => { it('returns a copy of approvers adding id fields for both group and users', () => {
expect(decomposeApprovers({}, groupedApprovers)).toStrictEqual({ expect(decomposeApprovers(actionDataWithoutApprovers, groupedApprovers)).toStrictEqual({
...actionDataWithoutApprovers,
group_approvers_ids: [groupApprover.id], group_approvers_ids: [groupApprover.id],
user_approvers_ids: [userApprover.id], user_approvers_ids: [userApprover.id],
}); });
...@@ -112,27 +120,41 @@ describe('decomposeApprovers', () => { ...@@ -112,27 +120,41 @@ describe('decomposeApprovers', () => {
it('removes group_approvers and user_approvers keys only keeping the id fields', () => { it('removes group_approvers and user_approvers keys only keeping the id fields', () => {
expect( expect(
decomposeApprovers({ user_approvers: null, group_approvers: null }, groupedApprovers), decomposeApprovers(
{ ...actionDataWithoutApprovers, user_approvers: null, group_approvers: null },
groupedApprovers,
),
).toStrictEqual({ ).toStrictEqual({
...actionDataWithoutApprovers,
group_approvers_ids: [groupApprover.id], group_approvers_ids: [groupApprover.id],
user_approvers_ids: [userApprover.id], user_approvers_ids: [userApprover.id],
}); });
}); });
it('preserves any other keys in addition to the id fields', () => { it('returns only user info when group info is empty', () => {
expect(decomposeApprovers({ existingKey: null }, groupedApprovers)).toStrictEqual({ expect(
group_approvers_ids: [groupApprover.id], decomposeApprovers({ ...actionDataWithoutApprovers }, userOnlyGroupedApprovers),
).toStrictEqual({
...actionDataWithoutApprovers,
user_approvers_ids: [userApprover.id], user_approvers_ids: [userApprover.id],
existingKey: null,
}); });
}); });
it('returns empty id fields if there is only unknown types', () => { it('removes unrelated keys', () => {
expect(decomposeApprovers({}, [unknownApprover])).toStrictEqual({ expect(
group_approvers_ids: [], decomposeApprovers({ ...actionDataWithoutApprovers, existingKey: null }, groupedApprovers),
user_approvers_ids: [], ).toStrictEqual({
...actionDataWithoutApprovers,
group_approvers_ids: [groupApprover.id],
user_approvers_ids: [userApprover.id],
}); });
}); });
it('does not returns any approvers for unknown types', () => {
expect(decomposeApprovers(actionDataWithoutApprovers, [unknownApprover])).toStrictEqual(
actionDataWithoutApprovers,
);
});
}); });
describe('userIds', () => { describe('userIds', () => {
......
...@@ -117,7 +117,6 @@ describe('PolicyActionBuilder', () => { ...@@ -117,7 +117,6 @@ describe('PolicyActionBuilder', () => {
{ {
approvals_required: ACTION.approvals_required, approvals_required: ACTION.approvals_required,
user_approvers_ids: [APPROVER_2.id], user_approvers_ids: [APPROVER_2.id],
group_approvers_ids: [],
}, },
], ],
]); ]);
...@@ -140,7 +139,6 @@ describe('PolicyActionBuilder', () => { ...@@ -140,7 +139,6 @@ describe('PolicyActionBuilder', () => {
{ {
approvals_required: ACTION.approvals_required, approvals_required: ACTION.approvals_required,
user_approvers_ids: [APPROVER_1.id, APPROVER_2.id, NEW_APPROVER.id], user_approvers_ids: [APPROVER_1.id, APPROVER_2.id, NEW_APPROVER.id],
group_approvers_ids: [],
}, },
], ],
]); ]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment