This commit is contained in:
Philipp Kunz 2024-12-13 18:31:09 +01:00
commit 790f2371a1
15 changed files with 10132 additions and 0 deletions

View File

@ -0,0 +1,66 @@
name: Default (not tags)
on:
push:
tags-ignore:
- '**'
env:
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Install pnpm and npmci
run: |
pnpm install -g pnpm
pnpm install -g @ship.zone/npmci
- name: Run npm prepare
run: npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build

View File

@ -0,0 +1,124 @@
name: Default (tags)
on:
push:
tags:
- '*'
env:
IMAGE: code.foss.global/host.today/ht-docker-node:npmci
NPMCI_COMPUTED_REPOURL: https://${{gitea.repository_owner}}:${{secrets.GITEA_TOKEN}}@/${{gitea.repository}}.git
NPMCI_TOKEN_NPM: ${{secrets.NPMCI_TOKEN_NPM}}
NPMCI_TOKEN_NPM2: ${{secrets.NPMCI_TOKEN_NPM2}}
NPMCI_GIT_GITHUBTOKEN: ${{secrets.NPMCI_GIT_GITHUBTOKEN}}
NPMCI_URL_CLOUDLY: ${{secrets.NPMCI_URL_CLOUDLY}}
jobs:
security:
runs-on: ubuntu-latest
continue-on-error: true
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @ship.zone/npmci
npmci npm prepare
- name: Audit production dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --prod
continue-on-error: true
- name: Audit development dependencies
run: |
npmci command npm config set registry https://registry.npmjs.org
npmci command pnpm audit --audit-level=high --dev
continue-on-error: true
test:
if: ${{ always() }}
needs: security
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @ship.zone/npmci
npmci npm prepare
- name: Test stable
run: |
npmci node install stable
npmci npm install
npmci npm test
- name: Test build
run: |
npmci node install stable
npmci npm install
npmci npm build
release:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @ship.zone/npmci
npmci npm prepare
- name: Release
run: |
npmci node install stable
npmci npm publish
metadata:
needs: test
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
container:
image: ${{ env.IMAGE }}
continue-on-error: true
steps:
- uses: actions/checkout@v3
- name: Prepare
run: |
pnpm install -g pnpm
pnpm install -g @ship.zone/npmci
npmci npm prepare
- name: Code quality
run: |
npmci command npm install -g typescript
npmci npm install
- name: Trigger
run: npmci trigger
- name: Build docs and upload artifacts
run: |
npmci node install stable
npmci npm install
pnpm install -g @git.zone/tsdoc
npmci command tsdoc
continue-on-error: true

19
.gitignore vendored Normal file
View File

@ -0,0 +1,19 @@
.nogit/
# artifacts
coverage/
public/
# installs
node_modules/
# caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
#------# custom

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

18
npmextra.json Normal file
View File

@ -0,0 +1,18 @@
{
"gitzone": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"gitscope": "foss.global",
"gitrepo": "codefeed",
"description": "a module for creating feeds for code development",
"npmPackagename": "@foss.global/codefeed",
"license": "MIT",
"projectDomain": "foss.global"
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
}
}

47
package.json Normal file
View File

@ -0,0 +1,47 @@
{
"name": "@foss.global/codefeed",
"version": "1.0.1",
"private": false,
"description": "a module for creating feeds for code development",
"main": "dist_ts/index.js",
"typings": "dist_ts/index.d.ts",
"type": "module",
"author": "Task Venture Capital GmbH",
"license": "MIT",
"scripts": {
"test": "(tstest test/ --web)",
"build": "(tsbuild --web --allowimplicitany)",
"buildDocs": "(tsdoc)"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.25",
"@git.zone/tsbundle": "^2.0.5",
"@git.zone/tsrun": "^1.2.46",
"@git.zone/tstest": "^1.0.44",
"@push.rocks/tapbundle": "^5.0.15",
"@types/node": "^20.8.7"
},
"dependencies": {
"@push.rocks/qenv": "^6.1.0"
},
"repository": {
"type": "git",
"url": "git+https://code.foss.global/foss.global/codefeed.git"
},
"bugs": {
"url": "https://code.foss.global/foss.global/codefeed/issues"
},
"homepage": "https://code.foss.global/foss.global/codefeed#readme",
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
]
}

9562
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

3
qenv.yml Normal file
View File

@ -0,0 +1,3 @@
required:
- GITEA_TOKEN

3
readme.hints.md Normal file
View File

@ -0,0 +1,3 @@
# Project Readme Hints
This is the initial readme hints file.

7
readme.md Normal file
View File

@ -0,0 +1,7 @@
# @foss.global/codefeed
a module for creating feeds for code development
## How to create the docs
To create docs run gitzone aidoc.

30
test/test.ts Normal file
View File

@ -0,0 +1,30 @@
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
import * as codefeed from '../ts/index.js';
import * as qenv from '@push.rocks/qenv';
const testQenv = new qenv.Qenv('./', '.nogit/');
let testCodeFeed: codefeed.CodeFeed;
tap.test('first test', async () => {
const token = await testQenv.getEnvVarOnDemand('GITEA_TOKEN');
// console.log('token', token);
testCodeFeed = new codefeed.CodeFeed('https://code.foss.global', token);
expect(testCodeFeed).toBeInstanceOf(codefeed.CodeFeed);
});
tap.test('fetchAllCommitsFromInstance', async () => {
const commits = await testCodeFeed.fetchAllCommitsFromInstance();
expect(commits).toBeArray();
expect(commits.length).toBeGreaterThan(0);
// expect(commits[0]).toBeTypeofObject();
expect(commits[0].baseUrl).toBeTypeofString();
expect(commits[0].org).toBeTypeofString();
expect(commits[0].repo).toBeTypeofString();
expect(commits[0].timestamp).toBeTypeofString();
expect(commits[0].hash).toBeTypeofString();
expect(commits[0].commitMessage).toBeTypeofString();
expect(commits[0].tagged).toBeTypeofBoolean();
});
tap.start();

6
ts/codefeed.plugins.ts Normal file
View File

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

194
ts/index.ts Normal file
View File

@ -0,0 +1,194 @@
import * as plugins from './codefeed.plugins.js';
interface RepositoryOwner {
login: string;
}
interface Repository {
owner: RepositoryOwner;
name: string;
}
interface CommitAuthor {
date: string;
}
interface CommitDetail {
message: string;
author: CommitAuthor;
}
interface Commit {
sha: string;
commit: CommitDetail;
}
interface Tag {
commit?: {
sha?: string;
};
}
interface RepoSearchResponse {
data: Repository[];
}
interface CommitResult {
baseUrl: string;
org: string;
repo: string;
timestamp: string;
hash: string;
commitMessage: string;
tagged: boolean;
}
export class CodeFeed {
private baseUrl: string;
private token?: string;
constructor(baseUrl: string, token?: string) {
this.baseUrl = baseUrl;
this.token = token;
console.log('CodeFeed initialized');
}
private async fetchAllRepositories(): Promise<Repository[]> {
let page = 1;
const allRepos: Repository[] = [];
while (true) {
const url = new URL(`${this.baseUrl}/api/v1/repos/search`);
url.searchParams.set('limit', '50');
url.searchParams.set('page', page.toString());
const resp = await fetch(url.href, {
headers: this.token ? { 'Authorization': `token ${this.token}` } : {}
});
if (!resp.ok) {
throw new Error(`Failed to fetch repositories: ${resp.statusText}`);
}
const data: RepoSearchResponse = await resp.json();
allRepos.push(...data.data);
if (data.data.length < 50) {
break;
}
page++;
}
return allRepos;
}
private async fetchTags(owner: string, repo: string): Promise<Set<string>> {
let page = 1;
const tags: Tag[] = [];
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 resp = await fetch(url.href, {
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}`);
throw new Error(`Failed to fetch tags for ${owner}/${repo}: ${resp.statusText}`);
}
const data: Tag[] = await resp.json();
tags.push(...data);
if (data.length < 50) {
break;
}
page++;
}
const taggedCommitShas = new Set<string>();
for (const t of tags) {
if (t.commit?.sha) {
taggedCommitShas.add(t.commit.sha);
}
}
return taggedCommitShas;
}
private async fetchRecentCommitsForRepo(owner: string, repo: string): Promise<Commit[]> {
const twentyFourHoursAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
let page = 1;
const recentCommits: Commit[] = [];
while (true) {
const url = new URL(`${this.baseUrl}/api/v1/repos/${owner}/${repo}/commits`);
url.searchParams.set('limit', '1');
url.searchParams.set('page', page.toString());
const resp = await fetch(url.href, {
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}`);
throw new Error(`Failed to fetch commits for ${owner}/${repo}: ${resp.statusText}`);
}
const data: Commit[] = await resp.json();
if (data.length === 0) {
break;
}
for (const commit of data) {
const commitDate = new Date(commit.commit.author.date);
if (commitDate > twentyFourHoursAgo) {
recentCommits.push(commit);
} else {
// If we encounter a commit older than 24 hours, we can stop fetching more pages
return recentCommits;
}
}
page++;
}
return recentCommits;
}
public async fetchAllCommitsFromInstance(): Promise<CommitResult[]> {
const repos = await this.fetchAllRepositories();
let allCommits: CommitResult[] = [];
for (const r of repos) {
const org = r.owner.login;
const repo = r.name;
console.log(`Processing repository ${org}/${repo}`);
try {
const taggedCommitShas = await this.fetchTags(org, repo);
const commits = await this.fetchRecentCommitsForRepo(org, repo);
console.log(`${org}/${repo} -> Found ${commits.length} commits`);
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)
}));
allCommits.push(...formatted);
} catch (error: any) {
console.error(`Skipping repository ${org}/${repo} due to error:`, error.message);
continue;
}
}
return allCommits;
}
}

16
tsconfig.json Normal file
View File

@ -0,0 +1,16 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true,
"baseUrl": ".",
"paths": {}
},
"exclude": [
"dist_*/**/*.d.ts"
]
}