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 { ...@@ -73,6 +73,10 @@ export default {
mutation: addProjectToSecurityDashboard, mutation: addProjectToSecurityDashboard,
variables: { id: project.id }, variables: { id: project.id },
update(store, { data: results }) { update(store, { data: results }) {
if (!results.addProjectToSecurityDashboard.project) {
return;
}
const data = store.readQuery({ query: projectsQuery }); const data = store.readQuery({ query: projectsQuery });
const newProject = results.addProjectToSecurityDashboard.project; const newProject = results.addProjectToSecurityDashboard.project;
data.instanceSecurityDashboard.projects.nodes.push({ data.instanceSecurityDashboard.projects.nodes.push({
...@@ -82,23 +86,49 @@ export default { ...@@ -82,23 +86,49 @@ export default {
store.writeQuery({ query: projectsQuery, data }); store.writeQuery({ query: projectsQuery, data });
}, },
}) })
.then(({ data }) => {
return {
error: data?.addProjectToSecurityDashboard?.errors?.[0],
project: data?.addProjectToSecurityDashboard?.project ?? project,
};
})
.catch(() => { .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) return Promise.all(addProjectsPromises)
.then(response => { .then(response => {
const invalidProjects = response.filter(value => value.error).map(value => value.project); const invalidProjects = response.filter(value => value.error);
this.$emit('handleProjectManipulation', false); this.$emit('handleProjectManipulation', false);
if (invalidProjects.length) { if (invalidProjects.length) {
const invalidProjectsMessage = createInvalidProjectMessage(invalidProjects); const invalidProjectsByErrorMessage = response.reduce((acc, value) => {
createFlash( acc[value.error] = acc[value.error] ?? [];
sprintf(s__('SecurityReports|Unable to add %{invalidProjectsMessage}'), { acc[value.error].push(value.project);
invalidProjectsMessage,
}), 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(() => { .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', () => { ...@@ -161,7 +161,9 @@ describe('Project Manager component', () => {
findAddProjectsButton().vm.$emit('click'); findAddProjectsButton().vm.$emit('click');
return waitForPromises().then(() => { return waitForPromises().then(() => {
expect(createFlash).toHaveBeenCalledTimes(1); 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', () => { ...@@ -176,7 +178,7 @@ describe('Project Manager component', () => {
return waitForPromises().then(() => { return waitForPromises().then(() => {
expect(createFlash).toHaveBeenCalledTimes(1); expect(createFlash).toHaveBeenCalledTimes(1);
expect(createFlash).toHaveBeenCalledWith( 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 "" ...@@ -21924,6 +21924,9 @@ msgstr ""
msgid "SecurityReports|Project" msgid "SecurityReports|Project"
msgstr "" 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" msgid "SecurityReports|Projects added"
msgstr "" msgstr ""
...@@ -21999,7 +22002,7 @@ msgstr "" ...@@ -21999,7 +22002,7 @@ msgstr ""
msgid "SecurityReports|To widen your search, change or remove filters above" msgid "SecurityReports|To widen your search, change or remove filters above"
msgstr "" msgstr ""
msgid "SecurityReports|Unable to add %{invalidProjectsMessage}" msgid "SecurityReports|Unable to add %{invalidProjectsMessage}: %{errorMessage}"
msgstr "" msgstr ""
msgid "SecurityReports|Unable to add %{invalidProjects}" 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