77 lines
2.0 KiB
JavaScript
77 lines
2.0 KiB
JavaScript
/**
|
|
* 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;
|