Skip to content

Commit

Permalink
update actions
Browse files Browse the repository at this point in the history
  • Loading branch information
datpmt committed Nov 29, 2024
1 parent b59c904 commit 50f5839
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 75 deletions.
93 changes: 27 additions & 66 deletions request-review.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
35 changes: 26 additions & 9 deletions test-config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
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}`);
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);
Expand All @@ -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.');
}

0 comments on commit 50f5839

Please sign in to comment.