From 89938360583741d36106feda06ed27baceff3b16 Mon Sep 17 00:00:00 2001 From: Mica <25252229+MicaLovesKPOP@users.noreply.github.com> Date: Sun, 6 Oct 2024 21:53:00 +0200 Subject: [PATCH] Update script.js --- blob/main/script.js | 122 ++++++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 37 deletions(-) diff --git a/blob/main/script.js b/blob/main/script.js index e3789a0..a0fcf18 100644 --- a/blob/main/script.js +++ b/blob/main/script.js @@ -1,30 +1,65 @@ const form = document.getElementById('bug-report-form'); -const token = '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'; // replace with your personal access token +const token = 'YOUR_NEW_TOKEN_HERE'; // replace with your new token const projectNumber = 2; // replace with your project number const repoOwner = 'MicaLovesKPOP'; // replace with your GitHub username const repoName = 'crashdayhub'; // replace with your repository name +const debug = console.debug; +const info = console.info; +const warn = console.warn; +const error = console.error; + +const logRequest = (method, url, headers, body) => { + debug(`Request: ${method} ${url}`); + debug(`Headers: ${JSON.stringify(headers)}`); + debug(`Body: ${JSON.stringify(body)}`); +}; + +const logResponse = (response) => { + debug(`Response: ${response.status} ${response.statusText}`); + debug(`Headers: ${JSON.stringify(response.headers)}`); + debug(`Body: ${response.body}`); +}; + +const logError = (error) => { + error(`Error: ${error.message}`); + error(`Stack: ${error.stack}`); +}; + form.addEventListener('submit', async (event) => { event.preventDefault(); - const formData = new FormData(form); - const title = formData.get('title'); - const description = formData.get('description'); - const reproductionSteps = formData.get('reproduction-steps'); - const expectedBehavior = formData.get('expected-behavior'); - const actualBehavior = formData.get('actual-behavior'); - const systemInfo = formData.get('system-info'); - - const apiEndpoint = `https://api.github.com/repos/${repoOwner}/${repoName}/issues`; - const headers = { - 'Authorization': `Bearer ${token}`, - 'Content-Type': 'application/json', - 'Accept': 'application/json' - }; - - const issueBody = { - title, - body: ` + try { + const formData = new FormData(form); + const title = formData.get('title'); + const description = formData.get('description'); + const reproductionSteps = formData.get('reproduction-steps'); + const expectedBehavior = formData.get('expected-behavior'); + const actualBehavior = formData.get('actual-behavior'); + const systemInfo = formData.get('system-info'); + + info('Form data:', { + title, + description, + reproductionSteps, + expectedBehavior, + actualBehavior, + systemInfo + }); + + const apiEndpoint = `https://api.github.com/repos/${repoOwner}/${repoName}/issues`; + info('API endpoint:', apiEndpoint); + + const headers = { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json', + 'Accept': 'application/json' + }; + info('Headers:', headers); + + const issueBody = { + title, + body: ` **Description:** ${description} @@ -40,24 +75,37 @@ ${actualBehavior} **System Information:** ${systemInfo} `, - labels: ['bug'], - repository_project: { - project_id: projectNumber + labels: ['bug'], + repository_project: { + id: projectNumber + } + }; + info('Issue body:', issueBody); + + try { + logRequest('POST', apiEndpoint, headers, issueBody); + const response = await fetch(apiEndpoint, { + method: 'POST', + headers, + body: JSON.stringify(issueBody) + }); + logResponse(response); + + if (response.ok) { + info('Issue created successfully!'); + alert('Bug report submitted successfully!'); + form.reset(); + } else { + warn(`Error creating issue: ${response.statusText}`); + logError(new Error(`Error creating issue: ${response.statusText}`)); + alert(`Error submitting bug report: ${response.statusText}`); + } + } catch (error) { + logError(error); + alert(`Error submitting bug report: ${error.message}`); } - }; - - const response = await fetch(apiEndpoint, { - method: 'POST', - headers, - body: JSON.stringify(issueBody) - }); - - if (response.ok) { - console.log('Issue created successfully!'); - alert('Bug report submitted successfully!'); - form.reset(); - } else { - console.error('Error creating issue:', response.statusText); - alert('Error submitting bug report. Please try again.'); + } catch (error) { + logError(error); + alert(`Error submitting bug report: ${error.message}`); } });