From fb2d476388e02ca7483902bdd76dc1eeccbec14c Mon Sep 17 00:00:00 2001 From: wanhose Date: Wed, 3 Apr 2024 21:08:25 +0200 Subject: [PATCH] fix(api): return existing issue url when error --- packages/api/src/routes/v3/report.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/packages/api/src/routes/v3/report.ts b/packages/api/src/routes/v3/report.ts index 7781274..0ea08d4 100644 --- a/packages/api/src/routes/v3/report.ts +++ b/packages/api/src/routes/v3/report.ts @@ -35,18 +35,19 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done: }, }, async (request, reply) => { - try { - const issues = await octokit.request('GET /repos/{owner}/{repo}/issues', { - owner: environment.github.owner, - repo: environment.github.repo, - }); - const url = new URL(request.body.url).hostname - .split('.') - .slice(-3) - .join('.') - .replace('www.', ''); + const issues = await octokit.request('GET /repos/{owner}/{repo}/issues', { + owner: environment.github.owner, + repo: environment.github.repo, + }); + const url = new URL(request.body.url).hostname + .split('.') + .slice(-3) + .join('.') + .replace('www.', ''); + const existingIssue = issues.data.find((issue) => issue.title.includes(url)); - if (issues.data.some((issue) => issue.title.includes(url))) { + try { + if (existingIssue) { throw new Error('Issue already exists'); } @@ -73,6 +74,7 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done: reply.send({ errors: [error.message], success: false, + data: existingIssue?.html_url, }); } }