fix(api): reopen issues instead of creating new ones even if they are closed
This commit is contained in:
parent
d6f0e5c459
commit
0382f429cf
@ -39,6 +39,7 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done:
|
|||||||
const issues = await octokit.request('GET /repos/{owner}/{repo}/issues', {
|
const issues = await octokit.request('GET /repos/{owner}/{repo}/issues', {
|
||||||
owner: environment.github.owner,
|
owner: environment.github.owner,
|
||||||
repo: environment.github.repo,
|
repo: environment.github.repo,
|
||||||
|
state: 'all',
|
||||||
});
|
});
|
||||||
const ua = new UAParser(request.body.userAgent ?? '').getResult();
|
const ua = new UAParser(request.body.userAgent ?? '').getResult();
|
||||||
const url = new URL(request.body.url).hostname
|
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));
|
const existingIssue = issues.data.find((issue) => issue.title.includes(url));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (existingIssue) {
|
if (existingIssue?.state === 'closed') {
|
||||||
throw new Error('Issue already exists');
|
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', {
|
const response = await octokit.request('POST /repos/{owner}/{repo}/issues', {
|
||||||
@ -82,7 +94,6 @@ export default (server: FastifyInstance, _options: RouteShorthandOptions, done:
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
reply.send({
|
reply.send({
|
||||||
data: existingIssue?.html_url,
|
|
||||||
errors: [error.message],
|
errors: [error.message],
|
||||||
success: false,
|
success: false,
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user