Merge "Use ProjectNotFoundError"

This commit is contained in:
Zuul 2024-03-19 08:22:24 +00:00 committed by Gerrit Code Review
commit fb796cc59a
3 changed files with 27 additions and 12 deletions

View File

@ -2536,7 +2536,8 @@ class TestInRepoConfig(ZuulTestCase):
self.assertEqual(A.data['status'], 'NEW') self.assertEqual(A.data['status'], 'NEW')
self.assertEqual(A.reported, 1, self.assertEqual(A.reported, 1,
"A should report failure") "A should report failure")
self.assertIn('Unknown projects: does-not-exist', A.messages[0], self.assertIn('The project "does-not-exist" was not found',
A.messages[0],
"A should have a syntax error reported") "A should have a syntax error reported")
def test_required_project_not_found_multiple_error(self): def test_required_project_not_found_multiple_error(self):
@ -2561,8 +2562,10 @@ class TestInRepoConfig(ZuulTestCase):
self.assertEqual(A.data['status'], 'NEW') self.assertEqual(A.data['status'], 'NEW')
self.assertEqual(A.reported, 1, self.assertEqual(A.reported, 1,
"A should report failure") "A should report failure")
self.assertIn('Unknown projects: does-not-exist, also-does-not-exist', self.assertIn('The projects "does-not-exist", '
A.messages[0], "A should have a syntax error reported") '"also-does-not-exist" were not found.',
A.messages[0],
"A should have a syntax error reported")
def test_template_not_found_error(self): def test_template_not_found_error(self):
in_repo_conf = textwrap.dedent( in_repo_conf = textwrap.dedent(

View File

@ -851,8 +851,7 @@ class JobParser(object):
# exception only once to capture all of them in the # exception only once to capture all of them in the
# error message. # error message.
if unknown_projects: if unknown_projects:
names = ", ".join(unknown_projects) raise ProjectNotFoundError(unknown_projects)
raise Exception("Unknown projects: %s" % (names,))
job.required_projects = new_projects job.required_projects = new_projects
@ -925,7 +924,7 @@ class JobParser(object):
for p in as_list(allowed_projects): for p in as_list(allowed_projects):
(trusted, project) = self.pcontext.tenant.getProject(p) (trusted, project) = self.pcontext.tenant.getProject(p)
if project is None: if project is None:
raise Exception("Unknown project %s" % (p,)) raise ProjectNotFoundError(p)
allowed.append(project.name) allowed.append(project.name)
job.allowed_projects = frozenset(allowed) job.allowed_projects = frozenset(allowed)

View File

@ -216,12 +216,25 @@ class ProjectNotFoundError(ConfigurationSyntaxError):
zuul_error_name = 'Project Not Found' zuul_error_name = 'Project Not Found'
def __init__(self, project): def __init__(self, project):
message = textwrap.dedent("""\ projects = None
The project "{project}" was not found. All projects if isinstance(project, (list, tuple)):
referenced within a Zuul configuration must first be if len(project) > 1:
added to the main configuration file by the Zuul projects = ', '.join(f'"{p}"' for p in project)
administrator.""") else:
message = textwrap.fill(message.format(project=project)) project = project[0]
if projects:
message = textwrap.dedent(f"""\
The projects {projects} were not found. All projects
referenced within a Zuul configuration must first be
added to the main configuration file by the Zuul
administrator.""")
else:
message = textwrap.dedent(f"""\
The project "{project}" was not found. All projects
referenced within a Zuul configuration must first be
added to the main configuration file by the Zuul
administrator.""")
message = textwrap.fill(message)
super(ProjectNotFoundError, self).__init__(message) super(ProjectNotFoundError, self).__init__(message)