feat(core): Add tracking of commits published on npm

This commit is contained in:
2024-12-13 19:24:09 +01:00
parent 3392174169
commit 19ecb3f9a5
6 changed files with 63 additions and 12 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@foss.global/codefeed',
version: '1.0.2',
version: '1.1.0',
description: 'a module for creating feeds for code development'
}

View File

@ -1,6 +1,8 @@
// @push.rocks
import * as qenv from '@push.rocks/qenv'
import * as smartnpm from '@push.rocks/smartnpm'
export {
qenv,
smartnpm,
}

View File

@ -41,11 +41,13 @@ interface CommitResult {
hash: string;
commitMessage: string;
tagged: boolean;
publishedOnNpm: boolean;
}
export class CodeFeed {
private baseUrl: string;
private token?: string;
private npmRegistry = new plugins.smartnpm.NpmRegistry();
constructor(baseUrl: string, token?: string) {
this.baseUrl = baseUrl;
@ -160,6 +162,8 @@ export class CodeFeed {
public async fetchAllCommitsFromInstance(): Promise<CommitResult[]> {
const repos = await this.fetchAllRepositories();
const skippedRepos: string[] = [];
console.log(`Found ${repos.length} repositories`);
let allCommits: CommitResult[] = [];
for (const r of repos) {
@ -171,24 +175,59 @@ export class CodeFeed {
const taggedCommitShas = await this.fetchTags(org, repo);
const commits = await this.fetchRecentCommitsForRepo(org, repo);
console.log(`${org}/${repo} -> Found ${commits.length} commits`);
const commitResults: CommitResult[] = [];
for (const c of commits) {
const commit: CommitResult = {
baseUrl: this.baseUrl,
org,
repo,
timestamp: c.commit.author.date,
hash: c.sha,
commitMessage: c.commit.message,
tagged: taggedCommitShas.has(c.sha),
publishedOnNpm: false,
}
commitResults.push(commit);
}
const formatted = commits.map((c): CommitResult => ({
baseUrl: this.baseUrl,
org,
repo,
timestamp: c.commit.author.date,
hash: c.sha,
commitMessage: c.commit.message,
tagged: taggedCommitShas.has(c.sha)
}));
if (commitResults.length > 0) {
try {
const packageInfo = await this.npmRegistry.getPackageInfo(`@${org}/${repo}`);
for (const commit of commitResults.filter(c => c.tagged)) {
const correspondingVersion = packageInfo.allVersions.find(versionArg => {
return versionArg.version === commit.commitMessage.replace('\n', '');
});
if (correspondingVersion) {
commit.publishedOnNpm = true;
}
}
} catch (error: any) {
console.error(`Failed to fetch package info for ${org}/${repo}:`, error.message);
continue;
}
}
allCommits.push(...formatted);
allCommits.push(...commitResults);
} catch (error: any) {
skippedRepos.push(`${org}/${repo}`);
console.error(`Skipping repository ${org}/${repo} due to error:`, error.message);
continue;
}
}
console.log(`Found ${allCommits.length} relevant commits`);
console.log(`Skipped ${skippedRepos.length} repositories due to errors`);
for (const s of skippedRepos) {
console.log(`Skipped ${s}`);
}
for (const c of allCommits) {
console.log(`______________________________________________________
Commit ${c.hash} by ${c.org}/${c.repo} at ${c.timestamp}
${c.commitMessage}
Published on npm: ${c.publishedOnNpm}
`);
}
return allCommits;
}
}