Commit d77cbf85 authored by Phil Hughes's avatar Phil Hughes

Merge branch '236179-pass-error-from-backend-to-security-dashboard' into 'master'

Pass error message from GraphQL for Add Project to Security Dashbard

See merge request gitlab-org/gitlab!40692
parents e21c1a49 f84d1889
......@@ -73,6 +73,10 @@ export default {
mutation: addProjectToSecurityDashboard,
variables: { id: project.id },
update(store, { data: results }) {
if (!results.addProjectToSecurityDashboard.project) {
return;
}
const data = store.readQuery({ query: projectsQuery });
const newProject = results.addProjectToSecurityDashboard.project;
data.instanceSecurityDashboard.projects.nodes.push({
......@@ -82,23 +86,49 @@ export default {
store.writeQuery({ query: projectsQuery, data });
},
})
.then(({ data }) => {
return {
error: data?.addProjectToSecurityDashboard?.errors?.[0],
project: data?.addProjectToSecurityDashboard?.project ?? project,
};
})
.catch(() => {
return { error: true, project };
return {
error: s__(
'SecurityReports|Project was not found or you do not have permission to add this project to Security Dashboards.',
),
project,
};
});
});
return Promise.all(addProjectsPromises)
.then(response => {
const invalidProjects = response.filter(value => value.error).map(value => value.project);
const invalidProjects = response.filter(value => value.error);
this.$emit('handleProjectManipulation', false);
if (invalidProjects.length) {
const invalidProjectsMessage = createInvalidProjectMessage(invalidProjects);
createFlash(
sprintf(s__('SecurityReports|Unable to add %{invalidProjectsMessage}'), {
invalidProjectsMessage,
}),
const invalidProjectsByErrorMessage = response.reduce((acc, value) => {
acc[value.error] = acc[value.error] ?? [];
acc[value.error].push(value.project);
return acc;
}, {});
const errorMessages = Object.entries(invalidProjectsByErrorMessage).map(
([errorMessage, projects]) => {
const invalidProjectsMessage = createInvalidProjectMessage(projects);
return sprintf(
s__('SecurityReports|Unable to add %{invalidProjectsMessage}: %{errorMessage}'),
{
invalidProjectsMessage,
errorMessage,
},
);
},
);
createFlash(errorMessages.join('<br/>'));
}
})
.finally(() => {
......
---
title: Enhance error messages when Adding Projects to Security Dashboard
merge_request: 40692
author:
type: changed
......@@ -161,7 +161,9 @@ describe('Project Manager component', () => {
findAddProjectsButton().vm.$emit('click');
return waitForPromises().then(() => {
expect(createFlash).toHaveBeenCalledTimes(1);
expect(createFlash).toHaveBeenCalledWith('Unable to add Sample Project 1');
expect(createFlash).toHaveBeenCalledWith(
'Unable to add Sample Project 1: Project was not found or you do not have permission to add this project to Security Dashboards.',
);
});
});
......@@ -176,7 +178,7 @@ describe('Project Manager component', () => {
return waitForPromises().then(() => {
expect(createFlash).toHaveBeenCalledTimes(1);
expect(createFlash).toHaveBeenCalledWith(
'Unable to add Sample Project 2 and Sample Project 3',
'Unable to add Sample Project 2 and Sample Project 3: Project was not found or you do not have permission to add this project to Security Dashboards.',
);
});
});
......
......@@ -21924,6 +21924,9 @@ msgstr ""
msgid "SecurityReports|Project"
msgstr ""
msgid "SecurityReports|Project was not found or you do not have permission to add this project to Security Dashboards."
msgstr ""
msgid "SecurityReports|Projects added"
msgstr ""
......@@ -21999,7 +22002,7 @@ msgstr ""
msgid "SecurityReports|To widen your search, change or remove filters above"
msgstr ""
msgid "SecurityReports|Unable to add %{invalidProjectsMessage}"
msgid "SecurityReports|Unable to add %{invalidProjectsMessage}: %{errorMessage}"
msgstr ""
msgid "SecurityReports|Unable to add %{invalidProjects}"
......
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