diff --git a/request-review.js b/request-review.js index c1f3936f..7f325924 100644 --- a/request-review.js +++ b/request-review.js @@ -1,86 +1,47 @@ const axios = require('axios'); -// Validate environment variables -const githubToken = process.env.GITHUB_TOKEN; -if (!githubToken) { - throw new Error('GitHub token is required'); -} - -const owner = process.env.OWNER; -if (!owner) { - throw new Error('GitHub owner is required'); -} - -const repo = process.env.REPO; -if (!repo) { - throw new Error('GitHub repository name is required'); -} - -const pullNumber = process.env.PULL_NUMBER; -if (!pullNumber) { - throw new Error('GitHub pull request number is required'); -} - -const commitId = process.env.COMMIT_ID; -if (!commitId) { - throw new Error('Commit ID is required'); -} - -const body = process.env.BODY; -if (!body) { - throw new Error('Body is required'); -} - -const path = process.env.PATH; -if (!path) { - throw new Error('Path is required'); -} - -const position = process.env.POSITION; -if (!position) { - throw new Error('Position is required'); -} - - -const comment = process.env.COMMENT; -if (!comment) { - throw new Error('Comment is required'); -} - -async function sendPostRequest() { - const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`; - const headers = { - Authorization: `token ${githubToken}`, - }; +export async function sendPostRequest({githubToken, owner, repo, pullNumber, commitId, body, path, comment, position }) { + const url = `/repos/${owner}/${repo}/pulls/${pullNumber}/reviews`; const data = { commit_id: commitId, - body: body, + body, event: 'REQUEST_CHANGES', comments: [ { - path: path, + path, body: comment, side: 'RIGHT', - position: position, - } - ] + position, + }, + ], }; console.log('Sending request with data:', data); + const axiosInstance = axios.create({ + baseURL: 'https://api.github.com', + headers: { Authorization: `token ${githubToken}` }, + }); + try { - const response = await axios.post(url, data, { headers }); + const response = await axiosInstance.post(url, data); console.log('Response:', response.data); + return response.data; // Return response for further processing if needed } catch (error) { - if (error.response) { - console.error('GitHub API Error:', error.response.status, error.response.data); - } else if (error.request) { - console.error('No response received:', error.request); - } else { - console.error('Error during POST request:', error.message); - } + handleError(error); } } -sendPostRequest(); +function handleError(error) { + if (error.response) { + // HTTP response error (e.g., 404, 500) + console.error(`GitHub API Error: ${error.response.status}`, error.response.data); + } else if (error.request) { + // No response received + console.error('No response received:', error.request); + } else { + // Other errors (e.g., setup issues) + console.error('Error during POST request:', error.message); + } +} diff --git a/test-config.js b/test-config.js index fc5eb06a..cc785c7a 100644 --- a/test-config.js +++ b/test-config.js @@ -1,13 +1,11 @@ +import { sendPostRequest } from 'request-review.js'; + const typoOutput = process.env.TYPO_OUTPUT; const githubToken = process.env.GITHUB_TOKEN; const owner = process.env.OWNER; const repo = process.env.REPO; const pullNumber = process.env.PULL_NUMBER; const commitId = process.env.COMMIT_ID; -const body = process.env.BODY; -const filePath = process.env.PATH; -const position = process.env.POSITION; -const comment = process.env.COMMENT; console.log('typoOutput', typoOutput); console.log(`GITHUB_TOKEN: ${githubToken}`); @@ -15,11 +13,6 @@ console.log(`OWNER: ${owner}`); console.log(`REPO: ${repo}`); console.log(`PULL_NUMBER: ${pullNumber}`); console.log(`COMMIT_ID: ${commitId}`); -console.log(`BODY: ${body}`); -console.log(`FILE PATH: ${filePath}`); -console.log(`POSITION: ${position}`); -console.log(`COMMENT: ${comment}`); - if (typoOutput) { const typoArray = typoOutput.split('\n').map(line => line.trim()).filter(line => line); @@ -43,6 +36,30 @@ if (typoOutput) { }); console.log('parsedTypos', parsedTypos); + + if (parsedTypos) { + for (const typo of parsedTypos) { + console.log(`File: ${typo.file}`); + console.log(`Line: ${typo.line}`); + console.log(`Column: ${typo.column}`); + console.log(`Incorrect Word: ${typo.incorrectWord}`); + console.log(`Correct Word: ${typo.correctWord}`); + console.log('------------------------'); + + const response = await sendPostRequest({ + owner, + repo, + pullNumber, + commitId, + body: 'This is a review comment.', + path: typo.file, + comment: 'Please check this code.', + position: typo.line + }); + + console.log('response', response); + } + } } else { console.log('No typos found.'); } \ No newline at end of file