This commit is contained in:
@@ -23,9 +23,21 @@ export class ListGiteaCommitsQuery {
|
||||
@IsString()
|
||||
sha?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
page?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
limit?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
since?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
until?: string;
|
||||
}
|
||||
|
||||
export class ListGiteaTreeQuery {
|
||||
|
||||
@@ -51,6 +51,15 @@ export class GiteaController {
|
||||
return this.giteaService.listCommits(owner, repo, query);
|
||||
}
|
||||
|
||||
@Get('repos/:owner/:repo/commits/:sha/diff')
|
||||
getCommitDiff(
|
||||
@Param('owner') owner: string,
|
||||
@Param('repo') repo: string,
|
||||
@Param('sha') sha: string,
|
||||
) {
|
||||
return this.giteaService.getCommitDiff(owner, repo, sha);
|
||||
}
|
||||
|
||||
@Get('repos/:owner/:repo/releases')
|
||||
listReleases(@Param('owner') owner: string, @Param('repo') repo: string) {
|
||||
return this.giteaService.listReleases(owner, repo);
|
||||
|
||||
@@ -97,9 +97,16 @@ export class GiteaService {
|
||||
async listCommits(owner: string, repo: string, query: ListGiteaCommitsQuery) {
|
||||
const params = new URLSearchParams();
|
||||
params.set('limit', query.limit ?? '20');
|
||||
params.set('page', query.page ?? '1');
|
||||
if (query.sha) {
|
||||
params.set('sha', query.sha);
|
||||
}
|
||||
if (query.since) {
|
||||
params.set('since', query.since);
|
||||
}
|
||||
if (query.until) {
|
||||
params.set('until', query.until);
|
||||
}
|
||||
|
||||
const commits = await this.request<unknown[]>(
|
||||
`/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/commits?${params.toString()}`,
|
||||
@@ -108,6 +115,17 @@ export class GiteaService {
|
||||
return { commits };
|
||||
}
|
||||
|
||||
async getCommitDiff(owner: string, repo: string, sha: string) {
|
||||
const encodedOwner = encodeURIComponent(owner);
|
||||
const encodedRepo = encodeURIComponent(repo);
|
||||
const encodedSha = encodeURIComponent(sha);
|
||||
const diff = await this.requestText(
|
||||
`/${encodedOwner}/${encodedRepo}/commit/${encodedSha}.diff`,
|
||||
);
|
||||
|
||||
return { diff };
|
||||
}
|
||||
|
||||
async listReleases(owner: string, repo: string) {
|
||||
const releases = await this.request<unknown[]>(
|
||||
`/api/v1/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/releases`,
|
||||
@@ -255,6 +273,26 @@ export class GiteaService {
|
||||
return (await response.json()) as T;
|
||||
}
|
||||
|
||||
private async requestText(path: string): Promise<string> {
|
||||
const response = await fetch(`${this.baseUrl}${path}`, {
|
||||
headers: {
|
||||
Accept: 'text/plain',
|
||||
...(this.token ? { Authorization: `token ${this.token}` } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
throw new BadGatewayException({
|
||||
message: 'Gitea API 요청이 실패했습니다.',
|
||||
status: response.status,
|
||||
detail: message,
|
||||
});
|
||||
}
|
||||
|
||||
return response.text();
|
||||
}
|
||||
|
||||
private assertToken() {
|
||||
if (!this.token) {
|
||||
throw new UnauthorizedException(
|
||||
|
||||
Reference in New Issue
Block a user