feat: 添加 Gitea PR 评审 Action 的初始实现

This commit is contained in:
ren
2026-04-12 23:25:43 +08:00
commit ef24c532b4
7 changed files with 725 additions and 0 deletions

76
src/gitea.js Normal file
View File

@@ -0,0 +1,76 @@
/**
* Gitea API Client for Pull Request Reviews
*/
class GiteaClient {
constructor(baseUrl, token) {
this.baseUrl = baseUrl.replace(/\/+$/, '');
this.token = token;
this.headers = {
'Authorization': `token ${this.token}`,
'Content-Type': 'application/json'
};
}
async request(path, options = {}) {
const url = `${this.baseUrl}/api/v1${path}`;
const fetchOptions = {
...options,
headers: {
...this.headers,
...options.headers
}
};
const response = await fetch(url, fetchOptions);
if (!response.ok) {
const errorBody = await response.text().catch(() => 'Unable to read error response');
throw new Error(`Gitea API Error: ${response.status} ${response.statusText} - ${errorBody}`);
}
if (response.status === 204) {
return null;
}
return await response.json();
}
/**
* List all review comments on a PR
*/
async listPullRequestComments(owner, repo, index) {
return await this.request(`/repos/${owner}/${repo}/pulls/${index}/comments`);
}
/**
* Create a review comment on a specific line of a file in a PR
*/
async createPullRequestComment(owner, repo, index, { body, path, new_line, old_line, commit_id }) {
return await this.request(`/repos/${owner}/${repo}/pulls/${index}/comments`, {
method: 'POST',
body: JSON.stringify({ body, path, new_line, old_line, commit_id })
});
}
/**
* Update an existing review comment
*/
async updatePullRequestComment(owner, repo, commentId, body) {
return await this.request(`/repos/${owner}/${repo}/pulls/comments/${commentId}`, {
method: 'PATCH',
body: JSON.stringify({ body })
});
}
/**
* Create a review (summary + optional comments) on a PR
*/
async createReview(owner, repo, index, { body, event, comments = [] }) {
return await this.request(`/repos/${owner}/${repo}/pulls/${index}/reviews`, {
method: 'POST',
body: JSON.stringify({ body, event, comments })
});
}
}
module.exports = GiteaClient;