fix(core): Refactor fetch logic to use a unified fetchFunction for API calls

This commit is contained in:
Philipp Kunz 2024-12-14 22:53:42 +01:00
parent e5e0ceee78
commit 1e0ccec03e
3 changed files with 26 additions and 22 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 2024-12-14 - 1.6.4 - fix(core)
Refactor fetch logic to use a unified fetchFunction for API calls
- Consolidated API request logic in the CodeFeed class to use fetchFunction for improved maintainability.
## 2024-12-14 - 1.6.3 - fix(codefeed)
Refactor and fix formatting issues in the CodeFeed module

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@foss.global/codefeed',
version: '1.6.3',
version: '1.6.4',
description: 'The @foss.global/codefeed module is designed for generating feeds from Gitea repositories, enhancing development workflows by processing commit data and repository activities.'
}

View File

@ -20,13 +20,13 @@ export class CodeFeed {
* Load the changelog directly from the Gitea repository.
*/
private async loadChangelogFromRepo(owner: string, repo: string): Promise<void> {
const url = `${this.baseUrl}/api/v1/repos/${owner}/${repo}/contents/changelog.md`;
const url = `/api/v1/repos/${owner}/${repo}/contents/changelog.md`;
const headers: Record<string, string> = {};
if (this.token) {
headers['Authorization'] = `token ${this.token}`;
}
const response = await fetch(url, { headers });
const response = await this.fetchFunction(url, { headers });
if (!response.ok) {
console.error(
@ -80,8 +80,8 @@ export class CodeFeed {
}
private async fetchAllOrganizations(): Promise<string[]> {
const url = `${this.baseUrl}/api/v1/orgs`;
const response = await fetch(url, {
const url = `/api/v1/orgs`;
const response = await this.fetchFunction(url, {
headers: this.token ? { Authorization: `token ${this.token}` } : {},
});
@ -99,14 +99,14 @@ export class CodeFeed {
}): Promise<any[]> {
let rssUrl: string;
if (optionsArg.orgName && !optionsArg.repoName) {
rssUrl = `${this.baseUrl}/${optionsArg.orgName}.atom`;
rssUrl = `/${optionsArg.orgName}.atom`;
} else if (optionsArg.orgName && optionsArg.repoName) {
rssUrl = `${this.baseUrl}/${optionsArg.orgName}/${optionsArg.repoName}.atom`;
rssUrl = `/${optionsArg.orgName}/${optionsArg.repoName}.atom`;
} else {
throw new Error('Invalid arguments provided to fetchOrgRssFeed.');
}
const response = await fetch(rssUrl);
const response = await this.fetchFunction(rssUrl, {});
if (!response.ok) {
throw new Error(
`Failed to fetch RSS feed for organization ${optionsArg.orgName}/${optionsArg.repoName}: ${response.statusText}`
@ -135,11 +135,9 @@ export class CodeFeed {
const allRepos: plugins.interfaces.IRepository[] = [];
while (true) {
const url = new URL(`${this.baseUrl}/api/v1/repos/search`);
url.searchParams.set('limit', '50');
url.searchParams.set('page', page.toString());
const url = `/api/v1/repos/search?limit=50&page=${page.toString()}`;
const resp = await fetch(url.href, {
const resp = await this.fetchFunction(url, {
headers: this.token ? { Authorization: `token ${this.token}` } : {},
});
@ -164,17 +162,15 @@ export class CodeFeed {
const tags: plugins.interfaces.ITag[] = [];
while (true) {
const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/tags`);
url.searchParams.set('limit', '50');
url.searchParams.set('page', page.toString());
const url = `/api/v1/repos/${owner}/${repo}/tags?limit=50&page=${page.toString()}`;
const resp = await fetch(url.href, {
const resp = await this.fetchFunction(url, {
headers: this.token ? { Authorization: `token ${this.token}` } : {},
});
if (!resp.ok) {
console.error(
`Failed to fetch tags for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url.href}`
`Failed to fetch tags for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url}`
);
throw new Error(`Failed to fetch tags for ${owner}/${repo}: ${resp.statusText}`);
}
@ -207,17 +203,15 @@ export class CodeFeed {
const recentCommits: plugins.interfaces.ICommit[] = [];
while (true) {
const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/commits`);
url.searchParams.set('limit', '50');
url.searchParams.set('page', page.toString());
const url = `/api/v1/repos/${owner}/${repo}/commits?limit=50&page=${page.toString()}`;
const resp = await fetch(url.href, {
const resp = await this.fetchFunction(url, {
headers: this.token ? { Authorization: `token ${this.token}` } : {},
});
if (!resp.ok) {
console.error(
`Failed to fetch commits for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url.href}`
`Failed to fetch commits for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url}`
);
throw new Error(`Failed to fetch commits for ${owner}/${repo}: ${resp.statusText}`);
}
@ -369,4 +363,9 @@ export class CodeFeed {
return allCommits;
}
public async fetchFunction(urlArg: string, optionsArg: RequestInit): Promise<Response> {
const response = await fetch(`${this.baseUrl}${urlArg}`, optionsArg);
return response;
}
}