From d9ab609039429db26b7c872ea6b19d6cb4929c2b Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Sat, 14 Dec 2024 02:28:25 +0100 Subject: [PATCH] fix(codefeed): Refactor and fix formatting issues in the CodeFeed module --- changelog.md | 7 ++++ ts/00_commitinfo_data.ts | 2 +- ts/index.ts | 73 +++++++++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 24 deletions(-) diff --git a/changelog.md b/changelog.md index bc39c2d..cbfc356 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Changelog +## 2024-12-14 - 1.6.3 - fix(codefeed) +Refactor and fix formatting issues in the CodeFeed module + +- Refactored various method format and spacing. +- Fixed error handling formatting for readability. +- Improved consistency in JSON handling for API responses. + ## 2024-12-14 - 1.6.2 - fix(core) Fix sorting order of tagged commits by timestamp diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 8c053e9..4a53713 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@foss.global/codefeed', - version: '1.6.2', + version: '1.6.3', description: 'The @foss.global/codefeed module is designed for generating feeds from Gitea repositories, enhancing development workflows by processing commit data and repository activities.' } diff --git a/ts/index.ts b/ts/index.ts index 801427b..dd1e2df 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -11,7 +11,8 @@ export class CodeFeed { constructor(baseUrl: string, token?: string, lastRunTimestamp?: string) { this.baseUrl = baseUrl; this.token = token; - this.lastRunTimestamp = lastRunTimestamp || new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); + this.lastRunTimestamp = + lastRunTimestamp || new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); console.log('CodeFeed initialized with last run timestamp:', this.lastRunTimestamp); } @@ -28,7 +29,9 @@ export class CodeFeed { const response = await fetch(url, { headers }); if (!response.ok) { - console.error(`Could not fetch CHANGELOG.md from ${owner}/${repo}: ${response.status} ${response.statusText}`); + console.error( + `Could not fetch CHANGELOG.md from ${owner}/${repo}: ${response.status} ${response.statusText}` + ); this.changelogContent = ''; return; } @@ -91,8 +94,8 @@ export class CodeFeed { } private async fetchOrgRssFeed(optionsArg: { - orgName: string, - repoName?: string, + orgName: string; + repoName?: string; }): Promise { let rssUrl: string; if (optionsArg.orgName && !optionsArg.repoName) { @@ -105,7 +108,9 @@ export class CodeFeed { const response = await fetch(rssUrl); if (!response.ok) { - throw new Error(`Failed to fetch RSS feed for organization ${optionsArg.orgName}/${optionsArg.repoName}: ${response.statusText}`); + throw new Error( + `Failed to fetch RSS feed for organization ${optionsArg.orgName}/${optionsArg.repoName}: ${response.statusText}` + ); } const rssText = await response.text(); @@ -114,8 +119,8 @@ export class CodeFeed { } private async hasNewActivity(optionsArg: { - orgName: string, - repoName?: string, + orgName: string; + repoName?: string; }): Promise { const entries = await this.fetchOrgRssFeed(optionsArg); @@ -135,7 +140,7 @@ export class CodeFeed { url.searchParams.set('page', page.toString()); const resp = await fetch(url.href, { - headers: this.token ? { 'Authorization': `token ${this.token}` } : {}, + headers: this.token ? { Authorization: `token ${this.token}` } : {}, }); if (!resp.ok) { @@ -164,11 +169,13 @@ export class CodeFeed { url.searchParams.set('page', page.toString()); const resp = await fetch(url.href, { - headers: this.token ? { 'Authorization': `token ${this.token}` } : {}, + 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}`); + console.error( + `Failed to fetch tags for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url.href}` + ); throw new Error(`Failed to fetch tags for ${owner}/${repo}: ${resp.statusText}`); } @@ -191,7 +198,10 @@ export class CodeFeed { return taggedCommitShas; } - private async fetchRecentCommitsForRepo(owner: string, repo: string): Promise { + private async fetchRecentCommitsForRepo( + owner: string, + repo: string + ): Promise { const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000); let page = 1; const recentCommits: plugins.interfaces.ICommit[] = []; @@ -202,11 +212,13 @@ export class CodeFeed { url.searchParams.set('page', page.toString()); const resp = await fetch(url.href, { - headers: this.token ? { 'Authorization': `token ${this.token}` } : {}, + 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}`); + console.error( + `Failed to fetch commits for ${owner}/${repo}: ${resp.status} ${resp.statusText} at ${url.href}` + ); throw new Error(`Failed to fetch commits for ${owner}/${repo}: ${resp.statusText}`); } @@ -265,7 +277,10 @@ export class CodeFeed { continue; } } catch (error: any) { - console.error(`Error fetching activity for repository ${orgName}/${r.name}:`, error.message); + console.error( + `Error fetching activity for repository ${orgName}/${r.name}:`, + error.message + ); continue; } @@ -286,12 +301,14 @@ export class CodeFeed { org, repo, timestamp: c.commit.author.date, - prettyAgoTime: plugins.smarttime.getMilliSecondsAsHumanReadableAgoTime(new Date(c.commit.author.date).getTime()), + prettyAgoTime: plugins.smarttime.getMilliSecondsAsHumanReadableAgoTime( + new Date(c.commit.author.date).getTime() + ), hash: c.sha, commitMessage: c.commit.message, tagged: taggedCommitShas.has(c.sha), publishedOnNpm: false, - changelog: undefined + changelog: undefined, }; return commit; }); @@ -306,15 +323,23 @@ export class CodeFeed { }); if (correspondingVersion) { commitResult.publishedOnNpm = true; - const changelogEntry = this.getChangelogForVersion(versionCandidate); - if (changelogEntry) { - commitResult.changelog = changelogEntry; - } } } } catch (error: any) { console.error(`Failed to fetch package info for ${org}/${repo}:`, error.message); } + + try { + for (const commitResult of commitResults.filter((c) => c.tagged)) { + const versionCandidate = commitResult.commitMessage.replace('\n', '').trim(); + const changelogEntry = this.getChangelogForVersion(versionCandidate); + if (changelogEntry) { + commitResult.changelog = changelogEntry; + } + } + } catch (error: any) { + console.error(`Failed to fetch changelog info for ${org}/${repo}:`, error.message); + } } allCommits.push(...commitResults); @@ -326,7 +351,9 @@ export class CodeFeed { console.log(`Processed ${allCommits.length} commits in total.`); - allCommits = allCommits.filter(commitArg => commitArg.tagged).sort((a, b) => b.timestamp.localeCompare(a.timestamp)); + allCommits = allCommits + .filter((commitArg) => commitArg.tagged) + .sort((a, b) => b.timestamp.localeCompare(a.timestamp)); console.log(`Filtered to ${allCommits.length} commits with tagged statuses.`); @@ -337,9 +364,9 @@ export class CodeFeed { ${c.commitMessage} Published on npm: ${c.publishedOnNpm} ${c.changelog ? `Changelog:\n${c.changelog}\n` : ''} - `); + `); } return allCommits; } -} \ No newline at end of file +}