All checks were successful
Dependabot Auto-Merge / rennovatebot (pull_request) Has been skipped
Dependabot Auto-Merge / dependabot (pull_request) Has been skipped
Dependabot Auto-Merge / devopsbot (pull_request) Has been skipped
Unit Tests / unittest (pull_request) Successful in 49s
COMMIT LINT / commitlint (pull_request) Successful in 1m2s
124 lines
3.4 KiB
JavaScript
124 lines
3.4 KiB
JavaScript
const core = require('@actions/core');
|
|
const github = require('@actions/github');
|
|
|
|
const sanitizeTask = (text) => (text || '').replace(/@bot\b[:]?\s*/gi, '').trim();
|
|
|
|
async function fetchFirstIssueComment(repoFullName, issueNumber, githubToken, serverUrl) {
|
|
if (!repoFullName || !issueNumber) return null;
|
|
|
|
const url = `${serverUrl}/api/v1/repos/${repoFullName}/issues/${issueNumber}/comments?limit=1`;
|
|
const headers = { Accept: 'application/vnd.github+json' };
|
|
|
|
if (githubToken) {
|
|
headers.Authorization = `Bearer ${githubToken}`;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(url, { headers });
|
|
if (!response.ok) {
|
|
core.warning(`Failed to fetch issue comments (${response.status}): ${response.statusText}`);
|
|
return null;
|
|
}
|
|
|
|
const comments = await response.json();
|
|
if (!Array.isArray(comments) || comments.length === 0) return null;
|
|
|
|
return sanitizeTask(comments[0]?.body);
|
|
} catch (error) {
|
|
core.warning(`Error fetching issue comments: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function run() {
|
|
const apiUrl = core.getInput('api_url') || 'http://agents-automation-api.agents:3000';
|
|
const apiToken = core.getInput('api_token');
|
|
const botRoute = core.getInput('bot_route') || 'codebot';
|
|
const debug = (core.getInput('debug') || 'false').toLowerCase() === 'true';
|
|
const serverUrl = (process.env.GITHUB_SERVER_URL || github.context.serverUrl || 'https://git.yusufali.ca').replace(/\/$/, '');
|
|
|
|
const payload = github.context.payload;
|
|
const commentBody = payload?.comment?.body || '';
|
|
const issueNumber = payload?.issue?.number;
|
|
const repoName = payload?.repository?.full_name;
|
|
const repoUrl = payload?.repository?.clone_url;
|
|
const githubToken = process.env.GITHUB_TOKEN;
|
|
|
|
if (!commentBody) {
|
|
if (!debug) {
|
|
core.setFailed('No comment body found in the event payload');
|
|
return;
|
|
}
|
|
|
|
core.info('No comment body found in the event payload, continuing because debug is enabled');
|
|
}
|
|
|
|
let task =
|
|
(await fetchFirstIssueComment(repoName, issueNumber, githubToken, serverUrl)) ||
|
|
sanitizeTask(commentBody);
|
|
|
|
if (!task) {
|
|
if (!debug) {
|
|
core.setFailed('Unable to determine the task to send to the agent');
|
|
return;
|
|
}
|
|
|
|
core.info('Unable to determine the task, using debug placeholder');
|
|
task = '[debug] empty task';
|
|
}
|
|
|
|
const requestPayload = {
|
|
type: 'input',
|
|
route: botRoute,
|
|
argumentId: 'plain',
|
|
force: true,
|
|
instanceId: null,
|
|
inputs: {
|
|
method: 'run_task',
|
|
inputs: {
|
|
repo_name: repoName,
|
|
repo_url: repoUrl,
|
|
issue_number: String(issueNumber),
|
|
task
|
|
}
|
|
}
|
|
};
|
|
|
|
if (debug) {
|
|
core.info(`API URL: ${apiUrl}`);
|
|
core.info(`Route: ${botRoute}`);
|
|
core.info(`Server URL: ${serverUrl}`);
|
|
core.info(`Using auth: ${Boolean(apiToken)}`);
|
|
core.info(`Request payload: ${JSON.stringify(requestPayload)}`);
|
|
}
|
|
|
|
try {
|
|
const headers = {
|
|
'Content-Type': 'application/json'
|
|
};
|
|
|
|
if (apiToken) {
|
|
headers.Apitoken = apiToken;
|
|
}
|
|
|
|
const response = await fetch(apiUrl, {
|
|
method: 'POST',
|
|
headers,
|
|
body: JSON.stringify(requestPayload)
|
|
});
|
|
|
|
const responseText = await response.text();
|
|
|
|
if (!response.ok) {
|
|
core.setFailed(`Agent API request failed (${response.status}): ${responseText}`);
|
|
return;
|
|
}
|
|
|
|
core.info(`Agent response: ${responseText}`);
|
|
} catch (error) {
|
|
core.setFailed(`Error sending task to agent: ${error}`);
|
|
}
|
|
}
|
|
|
|
run();
|