From 0382f429cf7f87955c57ae1a571dec509402666d Mon Sep 17 00:00:00 2001 From: wanhose Date: Sat, 3 Aug 2024 15:02:26 +0200 Subject: [PATCH] fix(api): reopen issues instead of creating new ones even if they are closed --- packages/api/src/routes/v3/report.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/api/src/routes/v3/report.ts b/packages/api/src/routes/v3/report.ts index 1f93bb9..225e360 100644 --- a/packages/api/src/routes/v3/report.ts +++ b/packages/api/src/routes/v3/report.ts @@ -39,6 +39,7 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done: const issues = await octokit.request('GET /repos/{owner}/{repo}/issues', { owner: environment.github.owner, repo: environment.github.repo, + state: 'all', }); const ua = new UAParser(request.body.userAgent ?? '').getResult(); const url = new URL(request.body.url).hostname @@ -49,8 +50,19 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done: const existingIssue = issues.data.find((issue) => issue.title.includes(url)); try { - if (existingIssue) { - throw new Error('Issue already exists'); + if (existingIssue?.state === 'closed') { + await octokit.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', { + owner: environment.github.owner, + repo: environment.github.repo, + issue_number: existingIssue.number, + state: 'open', + }); + + reply.send({ + data: existingIssue.html_url, + success: true, + }); + return; } const response = await octokit.request('POST /repos/{owner}/{repo}/issues', { @@ -82,7 +94,6 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done: }); } catch (error) { reply.send({ - data: existingIssue?.html_url, errors: [error.message], success: false, });